java swing jtable - display row index for each row - java

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.

Related

Selected Row in JTable along with Sorting

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.

Javax Swing JTable::getModel vs JTable::getColumnModel

Hello I have been working with javax swing and I came across a weird problem and got me questioning.
I can for example:
JTable table = new JTable();
// Indeed, 2 different objects:
// The TableModel (which, i think is supposed to contain rows and columns?
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
// And the column model, supposed to define the columns of a table?
TableColumnModel tcm = table.getColumnModel();
// I can add columns to my table in two different manners
dtm.addColumn("A Column");
// or
TableColumn column = new TableColumn();
column.setHeaderValue("Another column");
column.setWidth(120);
column.setMinWidth(120);
column.setMaxWidth(120);
tcm.addColumn(column);
// And notice that both commands will add a column in the table
// So our table model should now have 2 columns.
// But does it?
System.out.println(dtm.getColumnCount()); // outputs 1;
System.out.println(tcm.getColumnCount()); // outputs 2;
System.out.println(table.getColumnCount()); // outputs 2;
// The visual shows 2 columns, but the model has only 1.
From that I can tell JTable uses tableColumnModel and tableColumnModel gets all the columns added into tableModel, but, when I add a column to the TableModel it gets added to the table, but the tableModel remains outdated.
Now, the problem is: it's really interesting to add a column via columnModel because I can define the sizes, layout, editable options there, but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel.
Any thoughts on this?
The TableModel is used to contain data. The data is accessible in row/columns.
The TableColumnModel is used by JTable to control the View of the data. That is it controls the columns that are displayed in the JTable. You can also reorder the columns to display the data in a different order.
...but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel
That is correct. The purpose of the TableColumnModel is to simply customize the view, not manipulate data.
Maybe you have an application that contains many columns of data, but access to specific columns is limited by "security level". In this case the data is always stored in the TableModel, but you need to change the view to control which columns of data are visible. So you can remove/add columns from the TableColumnModel.
When you add a column to the TableModel, the JTable gets notified and it recreates all the TableColumns for you. This can be a good or bad thing because when the TableColumnModel is recreated you lose any custom renderers and editor that you may have added to a TableColumn. You can prevent this from happening buy using:
table.setAutoCreateColumnsFromModel( false );
Now the TableColumnModel will not be updated and it is your responsibility to manually create and add the TableColumn to the TableColumnModel.
But in general you:
add/change data through the TableModel.
change the view through the TableColumnModel.

Moving JTable rows downwards when new row is added to the table

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()

Master detail with a JTable

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.

JTable how to hide column in view but keep it in model for use it in background

I have a JTable and am passing a data array into the TableModel. I wish to retain all columns in the data model as I need them all for background data processing, but I wish to show only some of the columns in the JTable.
How do I achieve this -- to remove a data column from the view (the visualized JTable) but not from the model (the TableModel)?
JTable has
public void removeColumn(TableColumn aColumn)
Override getColumnCount of the model to return less columns that there is. Then put your hidden columns as last columns of the model.

Categories

Resources