Combo box setValue JavaFX - java

I have a combo box and I set the default value for this combo box at the initialisation of the node. However, once there is some data retrieved from a database I want to update this default value to something else.
initialise() {
businessDateComboBox.setItems(config.retrievedPositionsData().getDistinctBusinssDate());
businessDateComboBox.setValue(config.retrievedPositionsData().getCurrentBusinessDate().toString());
}
The setItems is an ObservableList and the setValue is an ObservableList to but ive converted it to string.
Now I use a separate thread to retrieve items from database.
public void readPositionsFromDataBase() throws Exception {
Task<Integer> task = new Task<Integer>() {
#Override protected Integer call() throws Exception {
config.positionViewPersister().readDataFromDataBase(null,null);
return 0;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
config.retrievedPositionsData().setCurrentBusinessDate("56")
}
Once this finishes I want to update the User Interface with the latest value retrieved for default combo box value. I do this by reloading the FXML and the corresponding controller of the FXML which consists of the initialise method - currently the initialise method is run again but the user interface does not get updated with the latest value. Does anyone know why?
The default value in combo box in user interface should now be 56 as ive set it. When I print businessDateComboBox.getValue() it gives 56 it just isn't updating the User Interface.
Is there any equivalent of the swing redraw or something?

businessDateComboBox.setValue is meant for the edit component of an editable ComboBox. I guess yours is not, so the right way to go should via the SelectionModel:
businessDateComboBox.getSelectionModel().select(...)

Related

JavaFX Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException when updating search field

I'm writing a small program for a project for Uni and it's basically a library program to manage books anr read/write to JSON file instead of using a database cause it'd be simpler since it's my first proper Java application.
I'm utilizing a TextField to filter a ListView with all the books' titles, and it works, it shows the correct book in the list and updates the corresponding informations on screen when that book is selected, the issue is that even if the program works as intended, it throws an error everytime I update the search field I get an ArrayIndexOutOfBoundsException. The full stack is as follows:
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 7
at javafx.base#19-ea/javafx.collections.transformation.FilteredList.get(FilteredList.java:172)
at com.libraryproject.javalibrary/com.libraryproject.javalibrary.MainViewController.populateDetails(MainViewController.java:200)
at com.libraryproject.javalibrary/com.libraryproject.javalibrary.MainViewController.lambda$initialize$3(MainViewController.java:127)
at javafx.graphics#19-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics#19-ea/com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)
at javafx.graphics#19-ea/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
at javafx.graphics#19-ea/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#19-ea/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
After some googling, some people suggested that when updating the GUI from user input, one should do it in the Application Thread, which to be honest I'm not absolutely sure what that means, but anyway I followed the advice and wrapped the functions that would then update the UI variables in a Platform.runLater(() -> {} , but the issue still remains, and it's the stack above, at this point I have absolutely no idea what the problem could be, so, following the stack posted, let's see the code of the parts that are shown:
I'm using a FilteredList to, well, filter the listrView using the search, here's the code managing that and most of the initialize method:
private FilteredList<Book> filteredBooks;
...
...
// inside the initialize method
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// Populate the variable we use throughout the program with the data from the JSON file
filteredBooks = new FilteredList<Book>(handleJSON.getBooks());
// Then update the list view for the first time
populateView(filteredBooks);
...
...
// section of code responsible to check for search changes, when found, fires populateView once more, this time with the variable updated.
searchField.textProperty().addListener((obs, oldText, newText) -> {
filteredBooks.setPredicate(book -> {
if(newText == null || newText.isEmpty() || newText.isBlank()) {
return true;
}
String lowerCaseCompare = newText.toLowerCase();
if(book.getTitle().toLowerCase().contains(lowerCaseCompare)) {
return true;
}
return false;
});
Platform.runLater(() -> populateView(filteredBooks));
}); // Listener
...
...
...
// This one handles the selection of an item in the list, when selected, the fields on the other side of the windows will get populated with the respective data from the book based on the id from the list, since they essentialy share the same FilteredList
listView.getSelectionModel().selectedItemProperty().addListener((obs, oldSel, newSel) -> {
Platform.runLater(() -> {
populateDetails(listView.getSelectionModel().selectedIndexProperty().getValue(), filteredBooks);
editButton.setDisable(false);
});
As you can see I wrapped all of the function that will update the ListView and fields in the window with Platform.runLater, but it doesn't seem to help.
Now for the populateView function that fires the first time the program is opened and everytime there's a change in the searchfiield:
public void populateView(FilteredList<Book> booksList) {
// clears the listview to avoid old elements stacking in the list.
listView.getSelectionModel().clearSelection();
listView.getItems().clear();
ObservableList<String> rawTitles = FXCollections.observableArrayList();
for(Book book: booksList) {
rawTitles.add(book.getTitle());
}
listView.setItems(rawTitles);
} // populateView()
And last but not least the populateDetails function that fills the fields about a book based on the list selection:
public void populateDetails(Integer selectedBookID, FilteredList<Book> books) {
Book currentBook = books.get(selectedBookID);
titleValue.setText(currentBook.getTitle());
authorValue.setText(currentBook.getAuthor());
languageValue.setText(currentBook.getLanguage());
genreValue.setText(currentBook.getGenre());
pagesValue.setText(currentBook.getPages().toString());
yearValue.setText(currentBook.getRelease().toString());
if (currentBook.getAvailable()) {
availableRadio.setSelected(true);
} else {
unavailableRadio.setSelected(true);
}
} // populateDetails
Thats basically I tried to use the runLater in different places just to be sure, I still get the same stack, any idea what could cause this?
The stack trace tells you exactly what the problem is. The ArrayIndexOutOfBoundsException occurs when you call get(..) on a FilteredList with the value -1, which you do on line 200 of MainViewController.java, in the populateDetails(...) method. Looking at your code, this line must be the line
Book currentBook = books.get(selectedBookID);
so selectedBookID must be the culprit, having the value -1.
selectedBookID is a parameter passed to the method, and you call the method from line 127 of MainController.java, in a lambda expression in the initialize() method. (Again, this information is in the stack trace.) The value you pass is
listView.getSelectionModel().selectedIndexProperty().getValue()
The documentation tells you explicitly when this happens:
The selected index is either -1, to represent that there is no selection, or an integer value that is within the range of the underlying data model size.
So your populate details needs to handle the case where nothing is selected (probably by clearing the text fields). I think it's cleaner to listen to the selectedItemProperty() instead of the selectedIndexProperty(), as it directly gives you the selected Book (or null if nothing is selected), and you don't have to retrieve the Book from the list:
public void populateDetails(Book currentBook) {
if (currentBook == null) {
titleValue.setText("");
authorValue.setText("");
languageValue.setText("");
genreValue.setText("");
pagesValue.setText("");
yearValue.setText("");
availableRadio.setSelected(false);
unavailableRadio.setSelected(false);
} else {
titleValue.setText(currentBook.getTitle());
authorValue.setText(currentBook.getAuthor());
languageValue.setText(currentBook.getLanguage());
genreValue.setText(currentBook.getGenre());
pagesValue.setText(currentBook.getPages().toString());
yearValue.setText(currentBook.getRelease().toString());
if (currentBook.getAvailable()) {
availableRadio.setSelected(true);
} else {
unavailableRadio.setSelected(true);
}
}
}
Your code is overkill; there is basically no need for the populateView() method. The filtered list will update its contents when you change the predicate, and notify observers that its content has changed. So you should just set the list view's items list to the filtered list directly. Then your listener for the search field only has to update the predicate, and the list view will automatically update.
Delete the populateView() method and update the initialize() method as:
public void initialize(URL arg0, ResourceBundle arg1) {
// Populate the variable we use throughout the program with the data from the JSON file
filteredBooks = new FilteredList<Book>(handleJSON.getBooks());
listView.setItems(filteredBooks);
// ...
// ...
// section of code responsible to check for search changes, when found, fires populateView once more, this time with the variable updated.
searchField.textProperty().addListener((obs, oldText, newText) -> {
filteredBooks.setPredicate(book -> {
if(newText == null || newText.isEmpty() || newText.isBlank()) {
return true;
}
String lowerCaseCompare = newText.toLowerCase();
return book.getTitle().toLowerCase().contains(lowerCaseCompare)
});
}); // Listener
// ...
// This one handles the selection of an item in the list, when selected, the fields on the other side of the windows will get populated with the respective data from the book based on the id from the list, since they essentialy share the same FilteredList
listView.getSelectionModel().selectedItemProperty().addListener(
(obs, oldSel, newSel) -> populateDetails(newSel)
);
}

JavaFX tableview Filtering by combobox selected item

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

Manipulate combobox using dictionary in java

I am consuming a web service which is returning me result of type "ArrayOfKeyValueOfintstring"
I am confused how to add this data to my combo box in java.
Here is my code
org.tempuri.ThirdPartyService service = new org.tempuri.ThirdPartyService();
org.tempuri.IThirdPartyService port = service.getBasicHttpBindingIThirdPartyService();
// TODO initialize WS operation arguments here
java.lang.String key = line.trim();
// TODO process result here
String>)port.getTests(key).getKeyValueOfintstring();
com.microsoft.schemas._2003._10.serialization.arrays.ArrayOfKeyValueOfintstring result = port.getVulnerabilities(key);
EDIT
for(int i=0;i<=result.getKeyValueOfintstring().size();i++)
{
result.getKeyValueOfintstring().get(i).getKey();
result.getKeyValueOfintstring().get(i).getValue();
JOptionPane.showMessageDialog(null, "key is"+result.getKeyValueOfintstring().get(i).getKey());
JOptionPane.showMessageDialog(null, "Value is"+result.getKeyValueOfintstring().get(i).getValue());
model.addElement(new Item(key, value));
}
I have tried to get the key pair in dialog box and i got it correctly. But now i am not getting how to add them to my ComboBox. I have created table "Vector model = new Vector();" and adding it to the combo box like this "cbTestName = new JComboBox(model);"
Is it the correct way or do i need to apply anything else to add the key value pair to my combo box.
If you'll go to declaration of
port.getTests(key).getKeyValueOfintstring(),
you'll probably find its implemented as
List<KeyValuePairOfintstring>
and KeyValuePairOfintstring is looks like
...
protected Integer key;
...
protected String value;
So one of the ways you can do - is run over port.getTests(key).getKeyValueOfintstring() in the loop, and build your map with your java business objects, you want to display in Combo Box.
You can override your object's toString method as a simplest way to control how will they look in the ComboBox.

Wicket - AjaxFormComponentUpdatingBehavior and backspace

I have a TextField where I have added an AjaxFormComponentUpdatingBehavior to get the current value when user write some string.
filterByObject = new TextField<String>("filterByObject", true, new PropertyModel<String>(searchParams, "objectFilter"));
AjaxFormComponentUpdatingBehavior changeFilterBinded = new AjaxFormComponentUpdatingBehavior ("onkeyup") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
target.addComponent(componentToUpdate);
}
};
filterByObject.add(changeFilterBinded);
When I put some chars inside textfield, onUpdate method is correctly called and my component, based on the current state of searchParams, changes correctly.
Unfortunally when I use Backspace to cancel what I have inserted, the onUpdate is not called.
I tried changing event (onkeypress, onkeydown, onchange etc...) but it doesn't work. Only onChange works but I have to change focus to another component.
How can I save the day?
Is the input in the field invalid (according to setRequired or IValidators added to the field) as a result of pressing the backspace key? If it is, the onError method will be called instead of onUpdate, because user input will be invalid and therefore will not reach the ModelObject of the component with the AjaxFormComponentUpdatingBehavior.
AjaxFormComponentUpdatingBehavior changeFilterBinded =
new AjaxFormComponentUpdatingBehavior ("onkeyup") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
// Here the Component's model object has already been updated
target.addComponent(componentToUpdate);
}
#Override
protected void onError(AjaxRequestTarget target, RuntimeException e){
// Here the Component's model object will remain unchanged,
// so that it doesn't hold invalid input
}
};
Remember that any IFormValidator involving the ajax-ified component will not execute automatically, so you might be interested in checking the input for yourself manually before updating model objects if it's the case. You can tell AjaxFormComponentBehavior not to update model objects automatically by overriding getUpdateModel(). Then, in the onUpdate method, get the component's new input by means of getConvertedInput().
As a side note, onkeyup should be getting fired when pressing the backspace key. At least it does in this fiddle, and onchange is generally triggered on an <input type="text"> when focusing out of it.
Also, HTML5 introduces the oninput event handler, which may better suit your needs. It will get fired even when copying/pasting in the text field. See the following link for more information: Using the oninput event handler with onkeyup/onkeydown as its fallback.

CheckboxCellEditor shows text and not a check box

I'm using the following
org.eclipse.jface.viewers.CheckboxCellEditor.CheckboxCellEditor(Composite parent)
I'm creating a table viewer with cellEditors and doing the following
CellEditor[] editors = new CellEditor[columnNames.length];
editors[7] = new CheckboxCellEditor(table);
I have a CellModifier that has the following
public Object getValue(Object element, String property) {
Object result = null;
...
result = Boolean.valueOf(task.isDfRequested());
return result;
}
public void modify(Object element, String property, Object value) {
item.isSelected(((Boolean)value).booleanValue());
}
Finally I have a LabelProvider that has the following
public String getColumnText(Object element, int columnIndex) {
String result = "";
try {
result = Boolean.toString(item.isSelected());
} catch (Exception ex) { }
break;
However, in my UI instead of having a check box I have the word true or false && clicking it results in switching state to false or true. Any ideas on why I don't have a checkbox??
I've searched in the source code of CheckboxCellEditor class and in the constructor the control associated to the CellEditor is created in the createControl(Composite parent) method. This method is abstract in CellEditor class and it's implemented like this in CheckboxCellEditor:
protected Control createControl(Composite parent) {
return null;
}
So a control is not created, that's why you don't see the checkbox. In the documentation of the Class you can read:
Note that this implementation simply
fakes it and does does not create any
new controls. The mere activation of
this editor means that the value of
the check box is being toggled by the
end users; the listener method
applyEditorValue is immediately called
to signal the change.
I solved this using a ComboBoxCellEditor with yes and no items.
Regards.
Well, I have no idea how SWT works or what component you are even talking about.
But I do know that when using Swing you can have custom editors for a column in a JTable. If you don't tell the table the class of data for the column then the toString() method of the data is invoked. But if you tell the table that Boolean data is displayed in the column then the table will use the check box editor.
Sounds like a similiar symptom, but I don't know your particular solution.
What I've decided to do is to just implement a dirty hack others have been using.
Create two images of check boxes, one checked the other not checked. Switch the state between the two based on the boolean.
It's not perfect, but for now it gets the job done

Categories

Resources