Dynamically changing the column header text in JTable - java

I have a table with 3 columns which have the following values in the headers: 'No.', 'X [mm]', 'Y [mm]'. This table contain the coordinates of points in millimeters. I have a checkbox on checking of which the table should repopulate to show the coordinates in inches. Moreover, the column header values should be: 'No.', 'X [in]', 'Y [in]'.
In short I want to dynamically change the header text of the table.
In detail:
The table is a subclass of JTable. Moreover, a subclass of 'DefaultTableModel' has been set as the model for the table. I have provided the header values in the constructer of the datamodel subclass.
Any idea? My application is compatible with only jdk v1.4 so it would be good if the solution is compatible with the verion :)

You can update the TableColumnModel directly:
JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(0);
tc.setHeaderValue( "???" );
th.repaint();

If you have column number use that code
jtable.getColumnModel().getColumn(5).setHeaderValue("newHeader");

I can't test here but familiar that this method '[DefaultTableModel.setColumnIdentifiers(...)][1]' should do what you want.
Basically, you run 'DefaultTableModel.getColumnCount()' to find out how many column (unless you already know). Then you run 'DefaultTableModel.getColumnName(int ColumnIndex)' to get the name of each, change it the way you want and put it in an array. After thatn, you set them back using 'DefaultTableModel.setColumnIdentifiers(...)'.
Hope this helps.

Related

Create JTable header without using new JTable(data,columnNames)

I've read how to create a JTable on JScrollPane with data in code, but how can I create a empty table with column header without adding data to it?
I want to add data to it from a form - not from my code.
If you use Netbeans for Development, it will be quite easy to work with JTables.
Using Netbeans, simply select Table from the palate and put it wherever you wish it to display.
Then in properties, you can change column names, add new columns and delete older. Also there only you will get an option of rows to display, simply put that to zero (0).
Voila! Your table is ready with column names and No Rows.

JTable clone / not the model

I have a JTable and delete some columns of it. Once I deleted the columns, I need to copy this JTable.
I can not copy the model, because the model still include the deleted columns. So how can I copy the "Visible Model"?
I do not really need to copy the graphical Swing-Component of a JTable, I just need the "Visible Model" of it.
TableColumn col = jtable.getColumnModel().getColumn(0);
jtable.getColumnModel().removeColumn(col);
jtable.setAutoCreateColumnsFromModel(false);
jtable.getmodel();
the thing is you have to call jtable.setAutoCreateColumnsFromModel(false);
other wise you'll get a new column again after deleting the column. try this
way. then you can pass the model the it wont contain the removed column.note that its a property in the jtable so calling the method setAutoCreateColumnsFromModel(false) one time is enough.
OMG, it´s so simple. You need to place also the ColumnModel in the new JTable:
JTable jtb = new JTable(ptable.getModel(), ptable.getColumnModel());

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.

JTable model column

I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.

How to hide a particlar column in DefaultTableModel from displaying it in table?

I am using Java Swingx framework. I have 4 columns in my DefaultTableModel object. I wish to display only 3 of the columns. But, I need all four for computing.
Actual data model
S.No. | ID | GDC ID | Decsription
What I want to display in table
S.No.| GDC ID | Decsription
Is it possible to hide or omit only one column from rendering? Please guide me.
by default minimum size is 10 pixels widht,
you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
No need to adjust your model, or to try to make that column very small. JTable has built-in functionality for this: the removeColumn method. As stated in the javadoc of that method
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
Also note the existence of the following methods:
JTable#convertColumnIndexToModel
JTable#convertColumnIndexToView
Since the column order and column count in the view (the JTable) might be different from the one in the model you need those methods to switch between view and model
You can hide it by setting its width to 0.
_table.getColumn("ID").setPreferredWidth(0);
_table.getColumn("ID").setMinWidth(0);
_table.getColumn("ID").setWidth(0);
_table.getColumn("ID").setMaxWidth(0);
try this to remove a single column:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
myTable.getColumnModel().removeColumn(myTableColumn1);
Then to show the column again keeping the order:
// 1- remove all the existent columns
myTable.getColumnModel().removeColumn(myTableColumn0);
myTable.getColumnModel().removeColumn(myTableColumn2);
myTable.getColumnModel().removeColumn(myTableColumn3);
// 2- add all the columns again in the right order
myTable.getColumnModel().addColumn(myTableColumn0);
myTable.getColumnModel().addColumn(myTableColumn1);
myTable.getColumnModel().addColumn(myTableColumn2);
myTable.getColumnModel().addColumn(myTableColumn3);
Sorry, but that's the best way I know.
You manipulate the getValueAt , getColumnCount to achieve this.
So for example the getColumnCount you give it as 3
and on getValueAt - to skip if the column index is above the skipped column
JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.
NOTE: Ignore this and see link by trashgod below.

Categories

Resources