Jose Ramon Jose Ramon - 1 month ago 8
Java Question

Togglegroup for buttons in javafx

I have the following code which is a grouping of buttons in javafx:

ToggleGroup groupLevelQ = new ToggleGroup();

class MyLevelButton extends ToggleButton {
public MyLevelButton(String name) {
super(name);
setPrefWidth(50.0);
setPrefHeight(50.0);
setStyle("-fx-font: 20 cornerstone; -fx-base: #17499F;");
setToggleGroup(groupLevelQ);
}
}

oneLevelButton = new MyLevelButton("1");
twoLevelButton = new MyLevelButton("2");
threeLevelButton = new MyLevelButton("3");
fourLevelButton = new MyLevelButton("4");
fiveLevelButton = new MyLevelButton("5");
sixLevelButton = new MyLevelButton("6");
sevenLevelButton = new MyLevelButton("7");
eightLevelButton = new MyLevelButton("8");
nineLevelButton = new MyLevelButton("9");

oneLevelButton.setUserData("1");
twoLevelButton.setUserData("2");
threeLevelButton.setUserData("3");
fourLevelButton.setUserData("4");
fiveLevelButton.setUserData("5");
sixLevelButton.setUserData("6");
sevenLevelButton.setUserData("7");
eightLevelButton.setUserData("8");
nineLevelButton.setUserData("9");

groupLevelQ.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
if (new_toggle != null) {
textLevelQ = (String) groupLevelQ.getSelectedToggle().getUserData();
}

}
});

addQButtonPane.add(oneLevelButton, 1, 9);
addQButtonPane.add(twoLevelButton, 2, 9);
addQButtonPane.add(threeLevelButton, 3, 9);
addQButtonPane.add(fourLevelButton, 4, 9);
addQButtonPane.add(fiveLevelButton, 5, 9);
addQButtonPane.add(sixLevelButton, 6, 9);
addQButtonPane.add(sevenLevelButton, 7, 9);
addQButtonPane.add(eightLevelButton, 8, 9);
addQButtonPane.add(nineLevelButton, 9, 9);


In fact I am creating buttons and I add them in a GridPane. I am trying to figure out how can I define the distance between the buttons. Basically as they are right now they have a default distance them and I want to change that.

EDIT:

addQButtonPane = new GridPane();
addQButtonPane.setHgap(10);
addQButtonPane.setVgap(10);
addQButtonPane.setPadding(new Insets(0, 50, 0, 50));
addQButtonPane.setStyle("-fx-background-color: #95CBE5;");


This is the way that my gridpane is formatted. But still I want to change this formation just for the specific mentioned buttons.

EDIT2:

enter image description here

Answer

Maybe the kind of layout you're trying to achieve could be better achieved by placing HBoxes containing the options and Text elements inside a VBox.

But if you're looking for a way to "take the buttons out of the usual layout", you could simply place them inside a HBox use a columnSpan that covers all remaining columns:

double buttonDistance = ...
int gridPaneColumnCount = ...
HBox buttonBox = new HBox(buttonDistance,
    oneLevelButton,
    twoLevelButton,
    threeLevelButton,
    fourLevelButton,
    fiveLevelButton,
    sixLevelButton,
    sevenLevelButton,
    eightLevelButton,
    nineLevelButton);

addQButtonPane.add(buttonBox, 1, 9, gridPaneColumnCount-1, 1);