I have a suspicion that I have stumbled upon a bug in JavaFX.
I have several TableViews that hold information about different objects.
In this example, I have an Examiner object with a name and a corresponding Course object.
I have created a function selectExaminer() that populates the Examiner name TextField and the Course ChoiceBox upon clicking on the Examiner object from the TableView with it's corresponding values.
But as can be seen from the screenshot above, only the TextField examinerName is populated, while the ChoiceBox choiceBoxExaminer is not. Here is the method: (it is called in the initialize() method)
public void selectExaminer(){
examinerTableView.getSelectionModel().setCellSelectionEnabled(true);
ObservableList selectedCells = examinerTableView.getSelectionModel().getSelectedItems();
selectedCells.addListener(new ListChangeListener() {
#Override
public void onChanged(Change c) {
if(selectedCells.size()>0)
{
Examiner aux = (Examiner) selectedCells.get(0);
examinerName.setText(aux.getName());
choiceBoxExaminer.setValue(aux.getCourse()); //here is the issue
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());
lastExaminerSelectedName = examinerName.getText();
}
}
});
The ChoiceBox dropdown does work but doesn't display the value set through .setValue()
When printing to the console the value of the Course of the actual Examiner and the one from the TableView, both show that they are populated.
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());
But alas... the ChoiceBox is still blank.
This issue arose after implementing data storage to binary files (this is a college project, no db), although I'm not sure how it influences the particular issue
Thank you
Try choiceBoxExaminer.getSelectionModel().setSelectedItem(aux.getCourse());
But, honestly, you make a good point; you would think that setValue() would also do the trick.
aux.getCourse()
Make sure that the return value is exactly the same as an item in the list added to the choiceBox. Only choicebox.setValue() works:
String[] fruits = {"apple", "orange"};
choiceBox.setItems(FXCollections.observableArrayList(fruits);
choiceBox.setValue("grape");//this won't work since grape isn's in the list.
choiceBox.setValue("orange"); // choicebox will display the value.
Related
I'm writing a seating chart program using JavaFX. I have a table that keeps a list of students together that holds their name, grade, and whether they are present or absent (using a checkbox). I have a delete button that allows me to delete the students from the list. This works fine, however, whenever I delete the student object, the checkbox does not go along with it. I'm not sure what I would need to add to get that to work. Here is a snippet of the delete code. There are also two images below that show my problem. This is my first post so please let me know if I missed something. Please help! Thanks!
ObservableList<Student> items, sel;
items = currentTable.getItems();
sel = currentTable.getSelectionModel().getSelectedItems();
Student s = new Student("", "", 0, "");
for (Student p : sel) {
items.remove(p);
s = p;
}
Before Delete
After Delete
This has nothing to do with the delete or remove method. It has to do with what you did in TableColumn.setCellFactory().
To get the checkbox you shown, you should have used (in general) one of the two methods:
Overriding updateItem() in TableCell while setting Cell Factory
There is this empty parameter in updateItem() which indicates whether the row is empty or not. You need to use that to determine when not to show the checkbox.
column.setCellFactory(col -> {
return new TableCell<Foo, Boolean>() {
final CheckBox checkBox = new CheckBox();
#Override
public void updateItem(final Boolean selected, final boolean empty) {
super.updateItem(selected, empty);
if (!this.isEmpty()) {
setGraphic(checkBox);
setText("");
}
else {
setGraphic(null); // Remove checkbox if row is empty
setText("");
}
}
}
}
Using CheckBoxTableCell
JavaFX API has this convenient class CheckBoxTableCell that would do all these for you. Most people find this class hard to use because there are 2 things that you need to ensure to use it correctly:
The TableView that the column belongs to must be editable.
The TableColumn itself must be editable.
Example:
tableView.setEditable(true);
tableColumnSelected.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnSelected));
tableColumnSelected.setEditable(true);
As for whether which entry you want to be removed with the delete button, you just need to remove the correct items from the TableView.
I'm noob in javafx and scene builder. I want to populate tableview by selecting one item from combobox. It is possible?
i try with String val = combobox.getValue() and i put the string in SQL query in preparedStatement for directly sort but app stops at the null string value and tableview is not updated.
Thank you guys!
It is possible that the String is being initialized with the ComboBox value even before the ComboBox gets an input. In that case, the ComboBox will return a null value.
You should add an onAction event for the ComboBox which will update the string.
You can use the following code segment to do that
comboBox.setOnAction((event) -> {
val = comboBox.getValue();
//Any other action you want to carry out when an item of the combo box is selected
});
Or if you are using an FXML file and want to add the onAction event in the controller, you can use this.
public void comboBoxEvent(ActionEvent event){
val = comboBox.getValue();
} // Use this code when working with FXML files
Both these examples assume that the String var was defined globally. Just to be on the safer side, when you are comparing var to another value or storing it somewhere else, you should put it under an if condition
if(var != null)
//Code segment here
i am stuck with a new problem, don't know if this works but here i have list of JCombobox as follow.
JCombobox comboBox = new JComboBox();
comboBox.addItem("UserName");
comboBox.addItem("Password");
comboBox.addItem("DLNo 20 b");
comboBox.addItem("DLNo 20 b");
i want to print my database column names which are more than 40!
when i select the Combobox it must internally print my custom item here.
Here i tried with this code but i am not satisfied with this
if(comboBox.getSelectedIndex()==0)
{
System.out.println("U_NAME");
}
if(comboBox.getSelectedIndex()==1)
{
System.out.println("P_NAME");
}
if(comboBox.getSelectedIndex()==2)
{
System.out.println("DL_NO_20_b");
}
if(comboBox.getSelectedIndex()==3)
{
System.out.println("DL_NO_20_b");
}
is there any better way to over come this, like mapping objects
You could create a class ComboBoxItem with a name- and a columnName-attribute.
Use instances of this class for the ComboBox.
In the ComboBoxItem-class, overwrite the toString()-method to return the name, so it gets displayed as wished in the ComboBox. Add a getColumnName()-method to return the columnName, so you could invoke getSelectedItem().getColumnName().
It is hard to explain so I'll use an example:
#Override
public void start(Stage primaryStage) throws Exception
{
final VBox vbox = new VBox();
final Scene sc = new Scene(vbox);
primaryStage.setScene(sc);
final TableView<Person> table = new TableView<>();
final TableColumn<Person, String> columnName = new TableColumn<Person, String>("Name");
table.getColumns().add(columnName);
final ObservableList<Person> list = FXCollections.observableArrayList();
list.add(new Person("Hello"));
list.add(new Person("World"));
Bindings.bindContent(table.getItems(), list);
columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
vbox.getChildren().add(table);
final Button button = new Button("test");
button.setOnAction(event ->
{
final Person removed = list.remove(0);
removed.setName("Bye");
list.add(0, removed);
});
vbox.getChildren().add(button);
primaryStage.show();
}
public static class Person
{
private String name = "";
public Person(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
}
In this example, I show a TableView with a single column named "Name". Running this sample code, you will get two rows: first row with "Hello" in "Name" column; and second row with "World" in "Name" column.
Additionally, there is a button, this button removes the first Person object from the list, then makes some changes to the object, then adds it back in at the same index. Doing so would cause any ListChangeListener added to the ObservableList to be triggered, and I have tested this to be true.
I would expect the row with "Hello" to be replaced with "Bye", but it seems like the TableView continues to show "Hello". If I used a TimeLine to add delay before I add the removed Person object back to the list, it would change to "Bye".
final Timeline tl = new Timeline(new KeyFrame(Duration.millis(30), ae -> list.add(0, removed)));
tl.play();
Is there something weird with the API? Is there any way to do this without this problem?
This is essentially expected behavior.
Note that (and I'm guessing you are trying to work around this issue), if you simply called
list.get(0).setName("Bye");
which has the same effect in terms of the underlying data, the table would not update as it has no way of being notified that the String field name in the element of the list has changed.
The code
Person removed = list.remove(0);
removed.setName("Bye");
list.add(0, removed);
is really equivalent to list.get(0).setName("Bye");: you just temporarily remove the item from the list before changing it, and then add it back. As far as the list is concerned, the net result is the same. I guess you are doing this in the hope that removing and replacing the item from the list will persuade the table to notice the state of the item has changed. There's no guarantee this will be the case. Here is what's happening:
The binding between your two lists:
Bindings.bindContent(table.getItems(), list);
works like any other binding: it defines how to get the value of the binding (the elements of list), and marks the data as invalid if list is invalidated at any time. The latter happens when you add and remove elements from list.
The TableView will not perform layout every time the binding to the list changes; instead, when then binding is invalidated (add or remove an element), then the table view marks itself as potentially needing to be redrawn. Then, on the next rendering pulse, the table will check the data and see if it really needs to be redrawn, and re-render if needed. There are obvious performance-saving features of this implementation.
So what happens with your code is that an item is removed from the list, causing the binding to be marked as invalid. The item is then changed (by calling setName(...)), and the same item is then added back into the list at the same position. This also causes the binding to be marked as invalid, which has no effect (it is already invalid).
No rendering pulse can occur between the removal and re-addition of this element. Consequently, the first time the table actually looks at the changes that were made to the list has to be after the entire remove-change-add process. At that point, the table will see that the list still contains the exact same elements in the exact same order that it previously contained. (The internal state of one of the elements has changed, but since this is not an observable value - not a JavaFX property - the table is unaware of this.) Consequently, the table sees no changes (or sees that all the changes have cancelled each other out), and doesn't re-render.
In the case where you add the pause, then a rendering frame (or two) occurs between the removal of the item and its re-addition. Consequently, the table actually renders one or two frames without the item, and when it is added back in, it adds it back and renders the current value. (You might, possibly, be able to make the behavior unpredictable, by pausing for 16 or 17 milliseconds, which is right on the cusp of the time for one rendering frame.)
It's not clear what you really intend to do. If you are trying to persuade the table to update without using JavaFX properties, you can do
list.get(0).setName("Bye");
table.refresh();
though this is not a very satisfactory solution.
Note too that
list.remove(0);
list.add(0, new Person("Bye"));
will also work (since now the added element is not the same as the removed element).
The better approach is to implement your model class with JavaFX properties:
public static class Person
{
private final StringProperty name = new SimpleStringProperty("");
public Person(String n)
{
setName(n);
}
public StringProperty nameProperty() {
return name ;
}
public final String getName()
{
return nameProperty().get();
}
public final void setName(String n)
{
nameProperty().set(n);
}
}
and then simply calling
list.get(0).setName("Bye");
will update the table (because the cell will be observing the property).
Requirement:
I have a list of strings displayed in the ComboBox. Each of these Strings can have some properties. These properties are displayed in PropertyTable. ComboBox's selected Item's properties are displayed in the table. In addition, we use PropertyTable for editing or setting property values to the selected item in the comboBox.
Problem:
The moment I de-select the comboBox Item,say item1, all the existing property values in the PropertyTable are set as new property values to item1. Again, when I select this item1 back, I should get above property values(i.e values before item1 is Deselected) back in to the PropertyTable?
Current Implementation Logic:
I am having TableCellListner for each PropertyTableCell, whenever cell content is changed, it takes the cell's new value and assigns this as new property value to the combo box's selected item. whenever new item is selected, table is refreshed with the selected Item's property values.
//before is Table initialization code
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener table = (TableCellListener)e.getSource();
String selectedItem=(String)ComponentPropComboBox.getSelectedItem();
if(table.getColumn()==1 && selectedItem!=null)
{
Property property=propertyMap.get(selectedItem);
else if(table.getRow()==0)
{
property.setProperty("MIN_LENGTH", (String)table.getNewValue());
propertyMap.put(selectedItem, property);
}
else if(table.getRow()==1)
{
property.setProperty("STARTS_WITH_STRING", (String)table.getNewValue());
propertyMap.put(selectedItem, property);
}
}
}
};
TableCellListener tcl = new TableCellListener(PropertiesTable, action);
How do i implement this requirement by overcoming the above challenge?
PS:
TableCellListner is a Not a java generic library. You can view code and its explanation at the following links:
http://www.camick.com/java/source/TableCellListener.java
http://tips4java.wordpress.com/2009/06/07/table-cell-listener/
I believe the question is obvious! Pls do let me know if question is not clear.Thanks in advance for your help & donating the knowledge!
In the code that listens for JComboBox selections. At its start have it set a boolean that the item is being changed. Then have your table refresh code ignore events that come while the boolean is set. After you are finished refreshing then set the boolean back.