I have a very odd issue with JTable.
I put data in JTable from DB. When user double clicks on any cell, it copies the cell contents of the first column of the row where user double clicked. So far, it works perfect.
The problem arises when the user sorts the JTable by clicking on the header. when the table has been sorted and now when the user double clicks on any row, it doesn't what is currently stored on that row's first column. It copies what was originally stored on the first column of that row when the JTable was not sorted.
Any ideas?
Problem:
The problem here is that you are getting the initial rows indexes in the JTable TableModel, and not the relevants row indexes shown in the table view.
Solution:
You can map the shown indexes of a sorted jTable with their relevants ones in the dataModel using convertRowIndexToModel(index) method which takes in input the row index in the view and returns the index of the corresponding row in the model.
Let's say that you have the following jTable:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
And then loop throught model indexes and use this method with each index to get its corresponding one in the TableModel:
table.getRowSorter().convertRowIndexToModel(0); // index 0 here
As suggested in How to Use Tables: Sorting and Filtering, "When using a sorter, always remember to translate cell coordinates." Most likely, you have neglected this in your event handler.
Try Sorting your JTable TableModel data too. Jtable -> TableModel is the one which hold the actual data. JTable is just a view.
Related
I have a JTable and a DefaultTableModel, I want to delete all the rows of JTable every 3 secondes and add new rows to the JTable. Considering the performance, What's the best choice to do that, recreate Jtable or remove all rows and add rows?
What's the best choice to do that, renew Jtable or remove all rows and addrows?
If you recreate the JTable, all the renderers and editors will be recreated for the table.
If you change the TableModel, then the table will need to recreate the TableColumnModel, and all the TableColumns based on the new TableModel.
There will be many more objects that need to be created to support the JTable/TableModel relationship.
Removing rows and adding rows, will just cause the table to repaint the new data using all the current renderers. I would say this is more efficient. And instead of adding rows one at a time, you should add all the rows at once
You can create a tablemodel that digs into your data object. As soon as you change the underlying data object, you call fireTableDataChanged().
From the JavaDoc for AbstractTableModel#fireTableDataChanged():
Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.
I'm working on a java desktop application and I have a Swing JTable for displaying data. I need to have a column for displaying row index for each row in the table. I don't want to add index in the grid data. Here is the way that I'm using to fill my table:
private DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.setDataVector(dataArray, headerArray);
jTable.setModel(defaultTableModel);
JTable displays what the TableModel tells it to display. Instead, extend AbstractTableModel. In your implementation of getValueAt(), return the row number for column zero and return the appropriate element of dataArray otherwise. Related examples may be found here and here.
You can add a row header to the scroll pane to display numbers for each row in the table.
Any component can be added to the row header. So one solution is to add a second (custom) JTable that just displays row numbers with a custom renderer so that the number look like the column headers.
Check out Row Table Number for an example of this approach.
wondering how I would get around populating a two-dimensional array after the user manually selects data from a JTable i.e. if you select two cells from the JTable which are (Football, Rugby) then they will be added to the new array the two array should populate in relation to [row][column]. I am asking this so I'm able to pass this array through to another class which will create visualizations. The code for my ListSelectionModel is:
http://pastebin.com/eNYz3us2
If i were to highlight the two cells (Football, Rugby) it would print out to the command line "FootballRugby" so just need to store this in the array!
Thanks for any help :)
This will give you the selected row and the selected column from the JTable. If no cell is selected then getSelectedXXX method will return -1.
table.getSelectedColumn(); // Gives the selected column.
table.getSelectedRow(); // Gives the selected row.
After getting the row and the column call getValueAt method to get the value at that position.
Now you have the required data with you and you can store it in the array and pass it to another class for visualization.
I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.
This is my problem. Can any one give me an idea how to move rows downwards...
Regards...
Upendra.S
You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.
For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.
Consult the JTable tutorial for more information
have look at JTable tutorial
part TableModel
method from DefaultTableModel insertRow()
I have a swing layout with some labels and a jTable.
On the left side, I have the jTable, and on the right side I have the labels.
I want to set the page up as a master / detail page, where the jTable is the master, while the labels act as details.
I have found a way to get the index from the jTable - my question. What kind of listener can I use to tell if the user selects a row in the table?
You need to get the JTable's selection model (using getSelectionModel()) and add a ListSelectionListener to this model.
Make sure to use convertRowIndexToModel to convert the index of the selected row to an index in the table model: if the table is sorted or filtered, the view and model indices won't be the same.