I am trying to add a JComboBox to the last column of my JTable. The JComboBox isn't for editing purposes but for traversing the JTable itself. Each row can have 0-many elements that need to go in the JComboBox and when a value is selected from the box I need to scroll to a different row in the JTable.
All the research I have done points me specifically to editors and renderers with the down fall being that data in the JComboBox is set per column so that a user can select a value for the cell in the row. Where as I need values that are specific to the row.
So my question is, has anyone tried to do this before? and Can you point me to some good information on how to do this? or even better could you describe how you did this?
1/ simple example here, your job is only to move (hold) TableCellEditor to the last row in the TableView,
2/ if JComboBox's Item changed then search in TableModel for TableRow (if every TableColumns ends with JComboBox)
3/ then call myTable.changeSelection(row, column, false, false);
4/ possible fauls implemented and used RowSorter, RowFilter, then you have to get int row from TableView and convert that to the TableModel by using
int modelRow = convertRowIndexToModel(row);
Related
In three of the columns of my JTable that I have set up I would like to have a list in each cell of the column. I'm not sure where to start on possibly creating a custom cell renderer class if that's the best option? My goal is to list the group names each on their own line in each cell, and expand the cell height as new lines are added. Each group will have an AdmType and Admitted entry, so I will also need to figure out how to add another checkbox to the Admitted column cell for every new Group entry.
This below solution did not work for me, but it clearly worked for the person who posted a screen shot of their success. My problems may be due to how I have my TableModel set up.
https://stackoverflow.com/a/32793088/6867420
The only sample I have is rather large so I am hoping to post this more as a general question to get an understanding of internal flow.
JTable has two JComboBoxes as Cell Editors for two different columns.
The select list within JComboBox2 will depend on the what is selected from JComboBox1.
This works for the most part.
When an item is selected from JComboBox1 the list dynamically changes behind the scene for JComboBox2.
The problem is the data is the cell itself doesn't change until I explicitly click on the JComboBox2 cell.
If in the code where I dynamically change the data for JComboBox2 I also explicitly change the cell data via the following call it works fine.
myTable.getModel().setValueAt(comboBox2.getModel().getElementAt(0), row, col);
Is this the expected process that both the ComboBox and Table models be updated?
Is there a way to simply update the ComboBox2 and have it automatically update what is seen in the JTable?
I have seen this tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox
But it defines a single combo box for the entire column. I would like to define different combo boxes for the different cells of the column.
For simplicity's sake, let's assume that there are n rows in the JTable and the i'th column of the JTable will be a column containing combo boxes. If I have a ComboBox[] comboBoxes, where comboBoxes.length == n, how can I resolve that comboBoxes[0] will be the content of the [0][i]'th cell of the JTable, comboBoxes[1] will be the [1][i]'th cell of the JTable and so on, comboBoxes[n - 1] will be the [n - 1][i]'th cell of the JTable. How can I achieve this?
Thanks.
I still do not know how can I set the cell editor of a cell.
In the particular case of TableComboBoxByRow, the program first creates a series of editors in a List<TableCellEditor> named editors for later use. The program then overrides the getCellEditor() method of JTable, and it returns the desired editor for each row in the
combo column. Note how the first three rows each get a different editor from the List, while the row four editor is just the default text field provided by the superclass.
I have dynamic checkbox in jtable , I do it by this code
TableColumn tcolumnas = jTable1.getColumnModel().getColumn(3);
tcolumnas.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
tcolumnas.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
and I want if I check one of them I want to know the index of check box.
Hearing for your suggestion
http://i.stack.imgur.com/tskSw.png
UPDATE :
the solution is veeery simple as here
Java Getting JTable Value (Per row)
You can add a TableModelListener to the TableModel. An event will be fired any time the data in a cell is changed.
I want to programmatically deselect the currently selected row (or rows) in a JTable.
Basically I want the opposite of this:
JTable table = ...;
table.setRowSelectionInterval(x,x);
I tried (with little hope) using:
table.setRowSelectionInterval(-1,-1)
or
table.setRowSelectionInterval(1,0)
but it doesn't work.
There is a method on JTable called clearSelection. This, in turn calls clearSelection on the ListSelectionModel of the table and the column model.
I believe you can use this:
table.getSelectionModel().clearSelection().
The SelectionModel is what actually handles the selection. JTable just has a few shortcuts.