I'm doing a GUI in JavaFX 8.
My inputs go through either
TextFields - all the same size & styling
ComboBoxes - all the same size & styling and same as that on TextFields
Sliders - these are an alternative input method to 2 of the TextFields where the user may want to quickly see the effect of varying 2 angles on the eventual graphic. A "Sliders On/Off" button allows the user to allow slider input.
There's no problem defining just one inner class for all my TextField inputs and applying all the styling, sizing, etc within that, e.g.
// Inner Class To Configure GUI TextField Nodes:
class SMTextField extends TextField
{
public SMTextField()
{
super();
super.setPrefWidth(150);
super.setPrefHeight(40);
super.setBackground(new Background(new BackgroundFill(Color.rgb(255,140,0),
CornerRadii.EMPTY, Insets.EMPTY))); // Orange background
super.setBorder(new Border(new BorderStroke(Color.rgb(255, 255, 0), // Yellow border
BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2.0))));
super.setStyle("-fx-text-fill: blue;"
+ "-fx-prompt-text-fill: white;"
+ "-fx-font-family: 'Calibri';"
+ "-fx-font-size: 11pt;"
+ "-fx-alignment: center");
}
}
But when I try to do something similar for the ComboBox inputs, it's not allowing me to do so the way that I want it. I want to avoid having String as the type of object within my ComboBoxes - I want the security of having a different enum type for each box. If I write this code:
// Inner class for generic ComboBox
class IComboBox extends ComboBox<Object>
{
.....
// Sizing & styling code
.....
}
I get no error. But I see no way to cast a ComboBox of Objects to a ComboBox of enums . . .
The only way I can work this so far is to have 5 separate inner classes - one for each ComboBox, with 95% of the code in each being the exact same. This clearly violates the "write it once" principle of OOP.
Can anyone show a better way of doing a series of enum ComboBoxes with identical styling code without such repetition ?
EDIT
Okay so, for the inner class to define my GUI ComboBox, I'm doing something like:
// Inner class to define combo box layout:
class SMComboBox<T extends Enum> extends ComboBox<T>
{
public SMComboBox()
{
super();
super.getStyleClass().add("sm-combo-box");
}
}
where the styling, sm-combo-box, resides in a CSS file referenced in the beginning of the start method.
The above inner class shows a warning which I don't understand :-
- The type parameter, T, is hiding the type, T.
- Enum is a raw type. References to generic type Enum should be parametrized.
I've thrown all sizing, bordering & coloring into the CSS file, so the inner class merely allows me to avoid referencing sm-combo-box for each instance of ComboBox that I use (5 so far).
All seems to work well, despite the warning.