I want to create a JTable with a header inside some cells. But I want to associate a text with this header. Is it possible? How to do?
All my best!
Leandro Lima
If I understand the question then you can create a panel using a BorderLayout. You add the table to the CENTER and the table header to the NORTH.
table.getColumnModel().getColumn(x).setHeaderValue( "text" );
I only way to do it I found was to create a table, without cells and with a column with a header title. So I get this header and put it inside other table by cell renderer.
Related
I'm working with JTable, and I want two things:
I would like to add another table header to my JTable, is this possible? Both of them in horizontal.
I want it's add a separator when the one of the attributes change its value, is this possible?
How can I do these things? See here the table as I want it.
Yes, you can add a second header. You can also add a separator by using the setBorder() method.
table.add(new JScrollPane(table));
table.setBorder(BorderFactory.createLineBorder(Color.RED,1));
I'm trying to add a JMenu component for every column in my JTable. The tricky bit is that I'd like the menu to be unique for each column e.g. right clicking on a column header displays a menu which is unique for the column. Anyone has an idea of how to achieve such behavior?
add a MouseListener to the table header
use the columnAtPoint(...) method of JTableHeader to get the column that was clicked
display your menu for the specific column
The tools MouseListener, MouseEvent.isPopupTrigger() and MouseEvent.getPoint() give you all the information you need for the user actions.
Likewise JTable.getTableHeader()/getColumnModel(), JTableHeader/TableColumn give you all the information about the columns and header.
You just need to wire it up.
For some reason my header row is not visible. I am using SwingBindings.createJTableBinding to bind a pojo to the table. My table is showing all the rows however the header row is not visible.
If I inspect the JTableHeader in the table is there and its columns have names as expected. The JTable header properties: isEnabled and isVisible are set to true.
Is there something I am missing?
I don't know what SwingBindings.createJTableBinding is, but when you use Swing you add the table to a JScrollPane. The table header is then used as the column header view of the scrollpane.
If you are not using a JScrollPane, then you need to create a JPanel using a BorderLayout. The table is added to the CENTER and the table header is added to the NORTH.
is possible to turn header text bold when I select a cell of its column? How to do it?
Thanks
Leandro
Set custom renderer into table header. If current column is selected column set font to bold.
JTable table = new JTable()
table.getTableHeader().setDefaultRenderer(new MyRenderer());
class MyRenderer implements TableCellRenderer {
//todo implement
}
It is partially possible. What you have to do is write your model is such way that on table's cell selection your model executes fireTableStructureChanges method( assuming you use AbstractTableModel as a base). This will repaint the whole table including column header. All you have to do is keep the state of which column is selected.
In the beginning I said "partially" possible. That is because calling fireTableStructureChanges will revalidate the whole table and you will lose your current column model state - column widths and sequence.
To make your text bold you can use HTML - something like <html><b>your text</b></html>, but it has to change dynamically based on your model's internal state
UPDATE: Also column table header text can be set directly but model change or tableStructureChanged event will make the table to reread from the model.
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.