Java Swing : get index of combobox inside table - java

I need to have combo boxes (JComboBox) in the first column of JTable.
JTable table = new JTable(5,10);
JCheckBox checkbox = new JCheckBox();
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));
Now, how can I get selected index of combo box in a certain cell ?
table.getModel().getValueAt(i, 0) returns a String. That's not what I need.

Why don't you use checkbox.getSelectedIndex()? If you declare checkbox in different cells they will all contain a copy of the same. So if you want different comboboxes in different cells you should declare them all with different names. And then you get the selected index like I said before.

The way all this works under the covers is that the JComboBox is treated as a simple delegate editor and the value of the JComboBox is what's kept in the table, not the JComboBox instance (otherwise you'd need an instance per cell). (You can read the gory details in the source)
What this means is that all that's available to you from the JTable is the currently selected value, which will be of type of the parameterized type E in JComboBox<E>, in your case, evidently, a String.
If you want to get the index of a particular value, you need to pull it using the ComboBoxModel<E> backing your JComboBox<E> -- if you've implemented your own ComboBoxModel<E> you'll have to implement your own method, but assuming you've used the default (DefaultComboBoxModel<E>), you can do something like:
int index = ((DefaultComboBoxModel<String>) comboBox.getModel()).getIndexOf(value)

Related

Displaying just one value in JComboBox when one is selected

Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.
You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.
That's the code snippet where the ComboBox gets initialized.
optionsInteger = new JComboBox<String>();
optionsInteger.addItem("Ja");
optionsInteger.addItem("Nein");
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
optionsInteger.setSelectedIndex(1);
optionsInteger.setName("optionsInteger");
The ComboBox gets inserted to a JTable in this method:
public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
JComboBox<String> optionsInteger) {
model.setRowCount(xAmount);
th = table.getTableHeader();
tcm = th.getColumnModel();
tcs = tcm.getColumns();
tcs.nextElement().setHeaderValue("");
tcs.nextElement().setHeaderValue("Lower");
tcs.nextElement().setHeaderValue("Upper");
tc = tcs.nextElement();
tc.setHeaderValue("Integer");
tc.setCellEditor(new DefaultCellEditor(optionsInteger));
for(int i=0;i<xAmount;i++)
{
model.setValueAt("X"+(i+1), i, 0);
}
}
Thank you very much for your help.
In your code, this line
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
sets the default selection to the zeroth element (Ja). This line
optionsInteger.setSelectedIndex(1);
sets the default selection to the first element (Nein).
Either set the selected item or the selected index. There's no need to do both.
A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?
If you really want to do this, you'll have to create your own version of a JComboBox.
. If "Ja" is selected there should just be "Nein" as an option and the other way around.
So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.
Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.
It would also be cool if "Nein" is set per default.
This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.

How to set jcombo box to what i want

I am doing a home work for my class in java, i am using NetBeans.
When the frame opened i want that my combo box load data that is exactly to my database column.
Exp... On my 7 column on my data base there's a column name that's name Color, and on the list of column there's yellow. I want that my jCombobox load with Yellow and get's all the others color on the model.
Here is my code
private void formWindowOpened(java.awt.event.WindowEvent evt) {
txt_id.setText(user);
SQLiteConnection DB = new SQLiteConnection ();
String question = DB.getQuestionUser();
DB.getUtilisateur(user);
cbx_question.addItem(question);
}
It keep on add item on the list of my model but do not show what is on the database column.
Hope that you will understand
The easiest way to populate a JComboBox is to supply the data when you call the constructor.
There are three different constructors you can call (supplying your data structure):
JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes its items from an existing ComboBoxModel.
JComboBox(E[] items)
Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector items)
Creates a JComboBox that contains the elements in the specified Vector.
You need to check what return type the database query has and convert it into one of the three previous data structures.
Later, if you wish to use the same JComboBox object to display other data (same type as before) you can do so by calling setModel( ComboBoxModel<E> model )
Source: Oracle documentation page

Is a custom TableCellEditor constructor only called once?

I have a JTable. One column in the JTable is assigned an extended TableCellEditor that displays an extended JComboBox.
There is a fixed list of 100 String objects that populates the comboboxes.
The challenge:
Design the JComboBoxes so that any selection is unique relative to other boxes? That is, if "A" is slected from the combobox in the first row, it is automatically removed from the list of each other combobox.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
The problem:
My comboboxes work beautifully. I can select items at will. I even have made some progress in eliminating already used items from the lists. But I can't figure out how to correctly auto-populate.
I am very confused because it appears that my combobox constructor is only called once when the table is created, not once for each row.
Is this the case? Is the constructor for a TableCellEditor only called once ever? If so, how do I modify the behavior of each combobox as it come into existence?
Thanks for your help!
If you would like specific code, please let me know. I don't know if you want me to paste in the whole classes.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
When you add a new row of data to the TableModel you must add the values of all columns in the row. This should not be a function of the editor. The editor allows you to change values in the cell.
I was able to get around my problem by creating an abstract superclass for my combobox that can be accessed from the tablemodel extension when it sets up its data.

Selecting item on JComboBox contained on a cell of JTable

I have created a JComboBox and I am using it on a column of a JTable - see code below.
Object[] items = {"1", "2", "3", "4"};
JComboBox comboBox = new JComboBox(items);
comboBox.setRenderer(new customComboBoxRenderer());
myTable.getColumnModel().getColumn(Table.CONF_COL).setCellEditor(new DefaultCellEditor(comboBox));
Now, my problem is that I cannot find a way to make each combo box in the column of the table with the combo boxes to have an initial value (might be different on each combo box) which will also be the selected item of the particular combo box when the program starts.
I think the problem is being created because the JTable is using only one JComboBox instance for all the cells of the column and therefore I can never get a different instance of combo box for each of the cells on the column and set the selected item to them.
Any help would be really appreciated.
Many thanks,
Soc
read Oracle tutorial How to use Tables - Using a Combo Box as an Editor for working code example
everything depends of value stored in DefaultComboBoxModel, in your case I'd be store Integer not String value for JComboBoxes model
value stored in XxxTableModel representing selected or inital value in JComboBox as TableCellEditor,
there you can store Integer value too, to avoiding any parsing between String and Integer
simple use (JTable or XxxTableModel).setValueAt to display desired value in JTable (painted by TableCellRenderer)
more info about TableCellRenderer and TableCellEditor in Oracle tutorial
In your customComboBoxRenderer
call setSelectedItem(same type as your jcombobox elements, in this case String)
inside your
getTableCellRendererComponent(.. ) method

Related to checkbox in jtable

I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.

Categories

Resources