Is is possible to some how get the index of the selection corresponding to the non filtered table?
After the table is filter using a regexFilter. JTable getSelectedRow returns the index of the filtered table?
If you are using the built in TableRowSorter functionality from 1.6 you can use the convertRowIndexToModel() on the table. This is give you the unfiltered index of the selected row.
The javadoc for JTable gives a description of this:
Coordinate conversions will be
necessary when using the row based
methods of JTable with the underlying
TableModel. All of JTables row based
methods are in terms of the RowSorter,
which is not necessarily the same as
that of the underlying TableModel. For
example, the selection is always in
terms of JTable so that when using
RowSorter you will need to convert
using convertRowIndexToView or
convertRowIndexToModel.
store the row id in your datamodel, when you get the selected row from jtable, query that rows ID.
Related
I have a model(AbstracTableModel) which I use to build a JTable.
The thing is that the table cell values seen in the GUI are displayed from a database.
How can I add a new column with checboxes for each row of the table?
Is there a concrete answer to this?
The thing is that the table cell values seen in the GUI are displayed from a database.
Use a DefaultTableModel to store the data from the database.
See the TableFromDatabaseExample.java code found in Table From Database for simple code to load the DefaultTableModel.
How can I add a new column with checboxes for each row of the table?
You can modify the above code to add an extra column to the "columnNames" Vector. Then in the looping code you add a Boolean.FALSE object to the "row" Vector.
Or, after creating the DefaultTableModel with the data from the database you can use the addColumn(...) method of the DefualtTableModel to create your column of check boxes.
I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro.
I've written a desktop app, Swing based.
My app includes a JTable; it shows many records of type T, one row per record.
The table model points to Vector vect, named vect, containing all data to be shown in the JTable.
The app includes a combo, named sele, showing three values: 0, 1, 2.
When sele = 0, every record of vect has to be visible in the JTable.
When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.
So, here's my question: how can I make a row not visible in the JTable ?
I can't use the table model, because it points to vect that contains "all" data.
I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.
I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.
I could solve the problem by splitting the data, dividing vect in two, but I don't like that.
Does anybody have an idea ?
thanx in advance,
William
PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.
Mah, perhaps it's a good idea, but frankly I'm not able to understand it...
Use a TableRowSorter - which is:
An implementation of RowSorter that provides sorting and filtering using a TableModel. ..
See How to Use Tables & especially Sorting and Filtering for more info.
I have a JTable with a predefined model
how to ask the model to insert a specific column in a specific position ?
so i want something like : DefaultTableModel.insertRow(int,Object[]) for columns
There is no insertColumn method like DefaultTableModel.insertRow() for inserting rows. In order to insert a column at a particular position, you must append the column using DefaultTable.addColumn() and then move the new column into the desired position.
JTable table = new JTable(rows, cols);
table.setAutoCreateColumnsFromModel(false);
DefaultTableModel model = (DefaultTableModel)table.getModel();
TableColumn col = new TableColumn(model.getColumnCount());
col.setHeaderValue(headerLabel);
table.addColumn(col);
model.addColumn(headerLabel.toString(), values);
table.moveColumn(table.getColumnCount()-1, vColIndex);
Is it really necessary to add the column in your TableModel at a specific index ? You can more easily adjust the column order in the view (the JTable) as documented in the class javadoc of JTable
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.
This is achieved by using the JTable#moveColumn method.
Adding a column to your DefaultTableModel is done calling the DefaultTableModel#addColumn method
With DefaultTableModel:
At the end one has to call fireDataChanged. There is only an addColumn with several overloads. This is no hindrance as the order of display is independent. One may move a column to another position, and has to take care of view index != column index. To get the correct view index immediately after adding the column, one has to access the JTable and call moveColumn.
I found it at times easier to create a new TableModel and assign that. Or not use the DefaultTableModel.
this link may help
http://www.roseindia.net/java/example/java/swing/InsertColumn.shtml
public void positionColumn(JTable table,int col_Index) {
table.moveColumn(table.getColumnCount()-1, col_Index);
}
I am using JTable for displaying the information. After rendering the information if I drag the columns to reorder them, the information is displayed in the same fashion in the session. But when I try to capture the changes by checking the column names by iterating the column names, the sequence is same as the older one. Why is the latest view not available from the API?
As commented by Hovercraft Full Of Eels, the column indices in the view change independently of the column indices in the model. JTable's JavaDoc has this to say about it:
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.
JTable offers the methods convertColumnIndexToModel() and convertColumnIndexToView() that you can use to translate column numbers from one to the other. You can use these to figure out if (and how) the columns were rearranged.
To be notified of column changes as they happen, use a TableColumnModelListener:
myTable.getColumnModel().addColumnModelListener( new TableColumnModelListener() {
//etc.
} );
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);