I am using a Bonded JComboBox. What I want to know is how do I check if that JComboBox has any items in it or not.
Thank you.
To test if the JComboxBox reference is null, you can compare it with null using the == operator. To test if the combo-box contains any items, you can use the instance method getItemCount, which returns the number of items it contains.
JComboxBox box = ...
boolean boxIsNull = (box == null); // answers the title of the question
boolean boxHasItems = (box.getItemCount() > 0);
Related
I want to bind a MenuItem of my contextMenu to an specific value of the selectedValue of the tableView.
Like you can see in the picture: if status is "anwesend" what is specified with an int = 2 in the Controller, then the option should be disabled.
I thought about this here, but it throws a NullPointer because the selection is empty:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> !loggedIn || tableView.getSelectionModel().getSelectedItem().getExcused() == 2));
But how to check first if it's not empty and then check that it's not that specific value?
I got the answer:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> tableView.getSelectionModel().getSelectedItems().isEmpty() || tableView.getSelectionModel().getSelectedItem().getExcused() == 2, tableView.getSelectionModel().getSelectedItems()));
I'm currently trying to get this part of my code to compare which button the user chooses to the button it self. At the moment it automatically displays the else which is Test2.
Object usersChoice;
Object[] options = { "Go on a journey!", "Exit, i'm tired." };
usersChoice = JOptionPane.showOptionDialog(null, "Hello Melissa :)", "Sunshine program",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
if(usersChoice == options )
{
JOptionPane.showMessageDialog(null, "Test1");
} else {
JOptionPane.showMessageDialog(null, "Test2");
}
The problem is with this code (I've moved the declaration of usersChoice for clarity):
Object[] options = [...]
Object usersChoice = JOptionPane.showOptionDialog([...])
The method JOptionPane.showOptionDialog() returns an int. Since that's a primitive, it gets autoboxed to an Integer.
Now you have this code:
if(usersChoice == options )
You are comparing an Object[] to an Object (more specifically, an Integer). That will always be false because they are different types.
Also remember that in Java, using == checks for equality on primitives, but Objects compared this way will be compared by their memory locations. Use .equals() instead to compare Objects.
I have a JComboBox that displays 2 columns. Now I would like to enable to search by key on all columns. Example:
Column1 | Column2
A1 | S1
A2 | B1
A3 | P1
The search by key on the first column works fine with default Implementation of KeySelectionManager for JComboBox. However, I would also like to be able to search for the second column as well, meaning when I press 'B' the second item is selected.
I have taken a look at the KeySelectionManager but didn't find anything useful. I have attached a screenshot of the ComboBox to show what I mean.
Thanks for any pointers.
Check KeySelectionManager implemetation in JComboBox's source code
class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
public int selectionForKey(char aKey,ComboBoxModel aModel) {
....
for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
Object elem = aModel.getElementAt(i);
if (elem != null && elem.toString() != null) {
v = elem.toString().toLowerCase();
if ( v.length() > 0 && v.charAt(0) == aKey )
return i;
}
}
So try to override your model's Element's class toString() method to include both columns.
If you display two columns in the combo box then you must be using a custom renderer.
So maybe the approach presented in Combo Box With Custom Renderer can help. The solution in the blog combines the renderer and KeySelectionManager into a single class. All you need to do is implement the getDisplayValue() method to return the text to renderer.
In your case you have two pieces of text to render and search, so you might be able to just change the getDisplayValue() method to return List of text you want to display.
Then the renderer can use both items in the List and the getNextMatch() method of the class would also be modified to check each item in the list for a match.
I currently have 2 checkboxes with an edit text field beside them. There are 4 checkboxes total and I have the logic to check each one and uncheck others if they are checked (almost radio button style). These 2 however could both be checked if they have values in the edit text fields.
However, if the EditText field (which is set to numeric) has a value of 0 or is blank I want it to uncheck the check box and set the value to 0.
Here is the code I have to do this
if (etBase.getText().toString() == "0" || etBase.getText().toString() == ""){
etBase.setText("0");
cbBase.setChecked(false);
} else {
cbBase.setChecked(true);
}
if (etField.getText().toString() == "" || etField.getText().toString() == "0"){
etField.setText("0");
cbField.setChecked(false);
} else {
cbField.setChecked(true);
}
As it sits right now I default the two fields to be "0" when it starts. When this logic runs, it is setting both checkboxes to checked.
I must be missing something here.
To compare string, you must use .equals() function. ( Or .compareTo() but that's no the today's deal)
Ex :
if (etBase.getText().equals("0") || etBase.getText().equals("")){
...
}
var1 == var2 is used to get if these both variable have the same object
I got a combobox, and a submit button, when the button is submitted, i want to check if the combobox value was null. Im using this code:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem().equals(null)) {
infoLabel.setText("Combo box value was null");
}
i am getting this error when i press the submit button:
java.lang.NullPointerException
how can i fix this?
You can not call equals on null. Instead simply use == null.
Something like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
Should work.
You can not give the null reference to equals(), do it like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
And a remark that has nothing to do with your question: I suggest using the Java Naming Convention, which would lead to your combo box being named comboBox (and not ComboBox).
The condition should be :
ComboBox.getSelectedItem() != null
or
ComboBox.getSelectedItem().toString().equals("")
This checks if what is selected in the Combobox is null or empty
Another way of doing this is leaving the first item empty, then check for the selected index against 0 i.e
ComboBox.getSelectedIndex() != 0
Thanks