JavaFX put RadioButtons into GridPane - javafx

I have the following piece of code:
GridPane gp = new GridPane();
// filling GridPane with other nodes...
RadioButton maschio = new RadioButton("M");
RadioButton femmina = new RadioButton("F");
final ToggleGroup tg = new ToggleGroup();
maschio.setToggleGroup(tg);
femmina.setToggleGroup(tg);
gp.add(tg, 1, 3);
I got an error on the last line saying: ToggleGroup cannot be converted to Node.
What can I do? I also tried with Vbox, Hbox but it didn't work.
Tried to Google but didn't find the solution. Any suggestions?

ToggleGroup tg = new ToggleGroup();
RadioButton male = new RadioButton("Male");
male.setToggleGroup(tg);
RadioButton female = new RadioButton("Female");
female.setToggleGroup(tg);
HBox box = new HBox(20, male,female);
gp.add(box,1,3);
Toggle a control that can be toggled between selected and non-selected states. In addition, a Toggle can be assigned a ToggleGroup, which manages all assigned Toggles such that only a single Toggle within the ToggleGroup may be selected at any one time.

I found the following solution:
ToggleButton maschio = new RadioButton("M");
ToggleButton femmina = new RadioButton("F");
final ToggleGroup tg = new ToggleGroup();
HBox rbContainer = new HBox(maschio, femmina);
maschio.setToggleGroup(tg);
femmina.setToggleGroup(tg);
gp.add(rbContainer, 1, 3);
Is it ok? or you have better solutions?

Related

How to change contextMenu default mouse button?

I have a contextMenu and I added it to a button. I want the contextMenu to be displayed when I left-click(PRIMARY) on the button. How can I do this? Because by default just right-clicking does this.
I tried this way but it did not work
Button sortBy = new Button();
ContextMenu sortByMenu = new ContextMenu();
sortByMenu.addEventFilter(MouseEvent.MOUSE_PRESSED, ev -> {
if (ev.getButton() == MouseButton.PRIMARY) {
//does not do anything
}
});
sortBy.setContextMenu(sortByMenu);
MenuButton
MenuButton is a button which, when clicked or pressed, will show a ContextMenu.
Example code from the javadoc:
MenuButton m = new MenuButton(
"Eats"
);
m.getItems().addAll(
new MenuItem("Burger"),
new MenuItem("Hot Dog")
);
ChoiceBox
You could also consider a ChoiceBox, depending on what you are trying to do.
The ChoiceBox is used for presenting the user with a relatively small set of predefined choices from which they may choose.
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll(
"item1",
"item2",
"item3"
);
To select a sortBy sorting field from a list of choices, a ChoiceBox would probably be a good fit.

JFX TableMenu context menu label empty

I wanted to add textfields to individual columns to allow individual column filtering. But after applying the following code, the table menu button on the top right corner when clicked, only displays 4 ticks(I have 4 columns), the label is missing. I assume this is because the header now is no longer just a label, but label with textfields. Anyone have a solution to this?
TextField headerTextField = new TextField();
Label label = new Label("First Name");
VBox headerGraphic = new VBox();
headerGraphic.setAlignment(Pos.CENTER);
headerGraphic.getChildren().addAll(label, headerTextField);
TableColumn<Person, String> firstNameCol = new
TableColumn<>();
firstNameCol.setGraphic(headerGraphic);

Is there any Mouse click event for TableColumn in JavaFX

Is there any Mouse click event for TableColumn in Java FX ?
I can see it in tableView but not in TableColumn. Please suggest.
There's no particularly nice way to do this. Probably the best option is not to set text on the column, but to set the graphic to a Label containing the desired text instead. Then register the mouse listener with the Label.
Using the standard Oracle tutorial as an example, you could do:
TableColumn<Person, String> firstNameColumn = new TableColumn<>();
Label firstNameColHeader = new Label("First Name");
firstNameColHeader.setOnMouseClicked(e -> System.out.println("Click on header"));
firstNameColumn.setGraphic(firstNameColHeader);
Note that this will break the table menu button, if you use it, because the options in the menu use the text from the TableColumn, which is no longer present.

How customize the chart from another window in JavaFX

I'm new at Stackoverflow and Javafx (and Java at all), so my question can be stupid.
I have a .fxml file with main window (it's include LineChart) and Controller.java, binded to it. And I need make new window with chart options (color and thickness of line). I'm making window with options by event, binded on menuItem in main window.
So, how I can change properties of chart in main window from options window?
I don't understand at all how to get access to LineChart in main window!
This is how I open options window:
void showSettings(ActionEvent event)
throws IOException {
Group root = new Group();
Stage stage = new Stage();
AnchorPane frame = FXMLLoader.load(getClass().getResource("options.fxml"));
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Can anybody help me?
P.S. Sorry for my bad English level :c
Edit1: I need pass params from new window(options) to already existing (main)!

Javafx Create custom multilabel button

How can I create a custom button in javafx. The button must have 3 label. An Upper Label a center LAbel and a lower label
Button button = new Button();
VBox threeLabels = new VBox();
threeLabels.setAlignment(Pos.CENTER);
threeLabels.getChildren.setAll(
new Label("upper"),
new Label("center"),
new Label("lower")
);
button.setGraphic(threeLabels);

Resources