In working with the JComboBox in Swing and reading up all the interfaces and classes and their various properties, I am not confused between the ComboBoxEditor's getItem method, and the JComboBox's getSelectedItem.
I am talking from the point of view of an editable combo box. When I call both these methods after editing some text in the combo box and pressing enter, both methods return the same value. Of course, that is expected, but then what are the specific uses of these two methods ? Is there any difference in the order in which they are called from within the combo box's code ?
The ComboBoxEditor's getItem() of function returns the edited item
And JComboBox getSelectedItem() returns the selected item. However to be specific to your question:
Is there any difference in the order in which they are called from
within the combo box's code ?
An editable Combo Box uses an editor to display the selected item. As soon as an item is edited, an action event is fired on registered ComboBoxEditor instance of the JComboBox . The implemented actionPerformed function of editor's action listener, gets the edited item from editor and then set as selected using the model's setSelectedItem(item) function which is evident from the following source code:
public void actionPerformed(ActionEvent e) {
Object newItem = getEditor().getItem(); // get edited's item
setPopupVisible(false);
getModel().setSelectedItem(newItem); // model set the item as selected
String oldCommand = getActionCommand();
setActionCommand("comboBoxEdited");
fireActionEvent();
setActionCommand(oldCommand);
}
Then, when we ask to get getSelectedItem() it returns the selected item by asking the model.
Why would you ever want to use the ComboBoxEditor's getItem() method? It is extra work to get the editor in order to get its value.
In general you should only ever get the data from the components model. The model is responsible for tracking the data in the model as well as the selected item in the model. The getSelectedItem() method of the combobox is a convenience method to get the selected item from the model.
It is the editors responsibility to update the model when the data is changed. So I would say the getItem() method is used by the editor to update the model at the appropriate time and you should not really be using it.
Related
Consider the following Jtable, and more precisely the first column which has a JComboBox in it:
When I try to save the value of the first JComboBox (the one that has "auth2" as its value) without clicking on it first, when I check the database, I find an empty String.
However, when I click on it first and then save, I get the right value stored in the databse.
Using the debugger, I found out that the method getCellEditorValue() of CellEditor.java is only called when you click on the JComboBox itself first.
This explains why in the database, I get the right value when I click on the JComboBox first and when I don't click on it, I get an empty String.
So my question is, is there a way to call the getCellEditorValue() method every time I save, regardless of whether or not I click on the JComboBox?
Thank you
I get the right value when I click on the JComboBox first and when I don't click on it, I get an empty String.
You should NOT try to access a value from the combo box. The combo box is shared as an editor for all rows in the table.
is there a way to call the getCellEditorValue() method every time I save
Data is stored in the TableModel, not the combo box.
You can use the getValueAt(…) method of the JTable or TableModel at any time to get the value from the model.
Note: it is possible (depending on what you are doing) that the data has not been saved from the editor to the TableModel. If this is the case then check out: JTable stop cell editing without user click for solutions.
I have an issue with my jComboBox not reflecting the changes in the model ...
In the model I have Box class that keeps an array list of Items. I have a combo box model defined this way :
myCombo.setModel(new javax.swing.DefaultComboBoxModel(box.items().toArray()));
(I use NetBeans 'design' mode). So as far as I understand, after setting the model this way, the combo box should reflect any changes in items list and at the start of the application, it indeed correctly shows the elements of the item list.
I also have a button Add to add a random instance of Item to the items list.
private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {
box.addRandomItem();
}
The method addRandomItem() simply adds some new instance of Item to the items list. So when I click the Add button, the new random item is correctly added to the items list (I can see it printed at the console) but at the GUI level, the list in combo box is not being updated so I cannot see the newly added item in it.
You should add the new item manually to the model using addElement(E element) or insertElementAt(E element, int index).
Internally, the DefaultComboBoxModel makes a copy of the items you provide as a parameter in the constructor, so there isn't any way to know that the original array has changed.
I am working on a UI design project in java swing where in I am expected to query a database based on the value selected from the Combo box. I have five combo boxes and a text field. I use .getSelectedItem() to include the value selected from the combo box in the where condition. If I run the query, apparently the first entry in the combo box which according to my design is Select.. gets selected and it returns nothing.I want the first entry to be a null value so that unless and until I select a value from combo box it remains null. Please help me out. Thank you.
I want the first entry to be a null value so that unless and until I select a value from combo box it remains null.
You should not use a null item in the combo box. However, after you load the data into the combo box you can use:
comboBox.setSelectedIndex(-1);
and no item will be selected in the combo box.
You may also want to consider using the Combo Box Prompt class. It will display a "hint" for the usage of the combo box until the user selects an item.
You can create a wrapper class that wrap the actual value like:
public class MyComboItem {
public MyComboItem(Object actualValue) {
this.actualValue = actualValue;
}
public Object getActualValue() {
return this.actualValue;
}
}
You may need to override toString() in this class so the combo box knows how to render the items property.
I've created a simple JTable with check box like below:
DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);
model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");
TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);
TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);
jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true);
It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.
For an example, when my check box is checked I need to pop up a
confirmation message.
You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:
Add a new TableModelListener to your TableModel
Validate if the updated cell value is a Boolean and its value is true.
Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
See TableModelEvent API as well.
Edit
Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.
Take a look to #kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.
I have a JTable with the required values. After editing a cell, if I use table.getvalue(row,column), I get the previous unaltered values. But if I select another cell before clicking the save button I get the modified values. Can anyone help me to remove this problem??
PS: I have not yet added any actionlisteners for the table
The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change
You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
or add following code to your table,
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Try TableModel.
Every table object uses a table model object to manage the actual table data. A table model object must implement the TableModel interface. If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.
A table model can have a set of listeners that are notified whenever the table data changes. Listeners are instances of TableModelListener.
Have you tried this
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
table.getValue(row,Column)
If Yes you need to use TableModel