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);
Related
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.
I have the following code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setLabelFor(vSuchenTextfield);
when I click on the vLblSuchfeld label with the mouse,
the text field vSuchenTextfield is not activated. What am I doing wrong?
If you want to bring focus to the textfield when you click on the label, I suggest you implement the following (untested) code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setOnMouseClicked(event -> {
vSuchenTextfield.requestFocus();
});
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.
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?
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);