tableMalzeme.setModel(DbUtils.resultSetToTableModel(resultSet));
tableMalzeme.setAutoCreateRowSorter(true);
It is sorting but model does not change. I called model of jtable. but it comes before sorting.
txtmalzeme.setText(tableMalzeme.getModel().getValueAt(tableMalzeme.getSelectedRow(), 1).toString());
How to update model after make setAutoCreateRowSorter is true ?
If you want to access the value from the selected row in the table then you need to use:
//txtmalzeme.setText(tableMalzeme.getModel().getValueAt(tableMalzeme.getSelectedRow(), 1).toString());
txtmalzeme.setText(tableMalzeme.getValueAt(tableMalzeme.getSelectedRow(), 1).toString());
That is you need to access the data via the table, since the table knows the current display order of the data. The data in the TableModel is never actually sorted, so you can't reference it by the selected row in the table.
If you want to access the data via the TableModel then you first need to convert the selectedRow value to the actual model row by using:
table.convertRowIndexToModel(...)
Related
I am having problem in getting the data when selecting a row from a JTable. This happens whenever I enable setAutoCreateRowSorter(true) of the table. So far this is what I did:
private void displayBooks(){
bookTable.setAutoCreateRowSorter(true);
bookTable.getTableHeader().setFont(new java.awt.Font("Century Gothic", 1, 14));
dtm = (DefaultTableModel) bookTable.getModel();
clearTable(dtm);
for(Book book: books){
dtm.addRow(new Object[]{book.getId(), ...//rest of the code
}
}
On the bookTableMouseClicked method this is what I did:
...
if(bookTable.getSelectedRow() >= 0){
Book book = books.get(bookTable.getSelectedRow());
setBook(book);
}...
I am now having ambiguous data when I clicked the header table to sort the data.
The selected row number on a JTable instance is always the selected row number on the view side.
If you activate row sorters this no longer matches the row number on the model side.
To transform between these two row numbers the JTable offers methods for converting from "view row index" to "model row index" and vice versa. These methods are named convertRowIndexToModel and convertRowIndexToView.
In your mouseClicked handler you need to call the function convertRowIndexToModel as follows:
if (bookTable.getSelectedRow() >= 0){
Book book = books.get(bookTable.convertRowIndexToModel(bookTable.getSelectedRow()));
setBook(book);
}
The problem is that you are storing data in two places:
in the TableModel
in an ArrayList
The data should only be stored in the TableModel. This way you don't need to worry about syncing the data since it is only in one place.
You could simply create a Book object from the selected row by using getValueAt(..) method of the JTable. You would need to invoke that method for each column in the table.
Or the other approach is to create a custom TableModel that holds Book objects, then you could just get the Book object directly from the table. This is a little more work, but it is the better approach.
Check out Row Table Model for a step-by-step approach on how to create a custom TableModel for a custom object.
I want to get the sum of all the values from jtable continuously. Like I have made a jtable where the user will put data continuously and whenever a new row will insert in the jtable then some code should execute for taking all the sum of values of jtable and then set it on a text filed. I have written the code but I don't that in which event I should use the code so that when a new row is inserted in the table then the code runs and get the new sum of all values and store in a text field. So now in which method of jtable I should use this code so when a new row is inserted so that code executes.
The code is here...
int rows=productionTable.getRowCount();
int totalBundles=0;
int totalBoxes=0;
//totalBundles
for(int i=0;i<rows;i++)
{
totalBundles=totalBundles+Integer.parseInt(productionTable.getValueAt(i, 1)+"");
}
this.totalBundlesTextField.setText(totalBundles+"");
//
//totalBoxes
for(int i=0;i<rows;i++)
{
totalBoxes=totalBoxes+Integer.parseInt(productionTable.getValueAt(i, 2)+"");
}
this.totalBoxesTextField.setText(totalBoxes+"");
//
So now in which method of jtable I should use this code so when a new row is inserted so that code execute
You do this when the data in the TableModel changes. So you can add a TableModelListener to the TableModel of your table. Then a TableModelEvent will be generated when you:
add rows of data
remove rows of data
change the value of existing data
In each of the above cases you could then iterate through the TableModel to calculate the new value.
Check out JTable -> TableModeListener for a simple example of use a TableModelListener.
The above approach assumes that you don't have a lot a data in your model, since it recalculates the value each time a change is made.
If you have a lot of data this is not very efficient so you may want to keep a running total of the value in your TableModel. So you would need to customize the TableModel to keep a total. And then in methods like setValueAt(...), insertRow(...), removeRow(...) you would need to add logic to update the total as the model is updated.
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 want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.
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.