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?
Related
I am working with javafx2.2 and want to this thing don't know whether something already exist or not.
I need to add combobox to column header of a tableview but I want to retain functionality of clicking header and sort my data.
I know I can attach combobox using TableColumn.setgraphic(ComboBox); and use width of combobox and tablecol accordingly and have combobox on top of my column header but I don want this.
I want combobox to take complete space of my columnheader but with functionality of sorting on click.
Is it achievable? If yes then how?
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 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);
The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.
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.