Can I change column header in Jtable tab from "Name" to "Surname" if I know column position? I want to change column name in second or first tab, not last one.
With this code I can change only column header in last tab. I have 4 tabs.
JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(0);
tc.setHeaderValue( "???" );
th.repaint();
You only have to replace your column index by the use of column identifier :
JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(tcm.getColumnIndex("Name")); // may not work, see note below
tc.setHeaderValue( "???" );
th.repaint();
The TableColumnModel#getColumnIndex(Object) works with a column identifier. Most of the time, column identifier and column header value are identical. However, in some cases, they can differ (typically when using i18n for column header). But in this case, i guess you have identified your column with a constant id.
Related
In my Jtable instance a column have the jComboBox , now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.
If i use the actionPerformed event of the jcombobox, and get the jtable.getSelectedrow and column. System give me last selected row and column instead of current row and column.
Please guide me what to do .. thank you
private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) jTable.getModel();
int selectedRow = jTable.getSelectedRow();
int selectedColumn = jTable.getSelectedColumn();
System.out.println("Row : " + selectedRow);
System.out.println("Column : " + selectedColumn);
}
now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.
Don't add an ActionListener to the combo box, that is not the way an editor of the table was designed to be used.
Instead you should be adding a TableModelListener to the TableModel of the JTable. An event will be generated whenever data is changed in the table. The event will contain the row/column of the cell that was changed.
For a working example check out: TableModelListener and multiple column validation
So I have a JTable with 24 columns. When the table is initialized, I make set the widths of the columns and the user interface looks great. No complaints. The user can click a button to only see 4 of the columns. I change the array of columns on the table model and then call
firetablestructurechanged()
which then removes all of the column widths I established (but now only shows the 4 columns, the intended behavior). When I try re-defining the column widths, nothing seems to change.
This is what I use to set column widths
TableColumnModel columnModel = table.getColumnModel();
for (int i = 1; i < columnModel.getColumnCount(); i++) {
columnModel.getColumn(i).setMinWidth(50);
columnModel.getColumn(i).setMaxWidth(50);
}
How do I get the columns to resize after firetablestructurechanged() gets called?
i have JTable with one column as default. my program can add columns with spesific value but when i try to add data in spesific row and column, an entire row filled by data. i just want to fill data to spesific row and column. its looks like this code still read number of column as default. i dont know how to solve this. thanks
for (int i = 0; i < NumberOfColumn.length; i++) {
TableColumn tbl = new TableColumn();
tbl.setHeaderValue(i);
table1.getColumnModel().addColumn(tbl);
}
table1.setValueAt(2014, 0, 14);
for example i want to fill data '2014' at row 0 column 14, but when i run this code, all column filled with '2014'
When you manually create a TableColumn you need to specify which column in the TableModel to get the data from, otherwise they all default to column 0.
TableColumn tbl = new TableColumn(i);
I have a JTable and any single row in it has associated a different tooltip when mouse hover a row. I have created a "filter" for this table; when it is applied it perfectly hides the rows need to be hidden but when I hover the mouse on the filtered rows, looks like the tooltip is referring to the row that occupied the same row position of the new current row.
For example:
Table
ROW 1 -> tooltip 1
ROW 2 -> tooltip 2
Apply Filter to Table:
ROW 2 -> tooltip 1
So ROW 2 is displaying the tooltip 1 instead of 2.
TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) table.getRowSorter();
sorter.setRowFilter(RowFilter.regexFilter(text));
My table that extends JTable has:
#Override
public String getToolTipText(MouseEvent e) {
final int rowIndex = rowAtPoint(e.getPoint());
TableModel model = getModel();
// take the value from the first column of the selected row
String tip = (String) getModel().getValueAt(rowIndex, 0));
return tip;
}
So it looks like using the model is not (quite obvious) updated respect to the filter. I tried using TableModel model = getRowSorter().getModel() too but without any luck.
How can I point to a correct "filtered model" to retrieve the correct row position?
UPDATE:
I have replaced the "rowIndex" code like this:
final int rowIndex = convertRowIndexToModel(rowAtPoint(e.getPoint()));
It partially solves the problem, but when some rows are added dynamically to the table with the filter applied and I hover new rows I get the exception (with relative API description):
IndexOutOfBoundsException -> if sorting is enabled and passed an index outside the range of the JTable as determined by the method getRowCount
You need to convert the views row index to the model's row index
Have a look at JTable#convertRowIndexToModel
You should not override that JTable#getToolTipText method. Just set the tooltip-text on the component returned by your renderer. The JTable will pick it up automatically. You can see this in the implementation of the getTooltipText method of the JTable
I have a table where my columns expand dynamically. Initially I set my table model to have 5 columns since basic information has 5 columns. Among the 5 columns column 2 and 3 are buttons (actually they are hyperlinks in a form of a button) which means that I have set column 2 and 3 to have its own renderer and editor.
table.getColumnModel().getColumn(2).setCellRender(new MyCellRender());
table.getColumnModel().getColumn(2).setCellEditor(new MyCellEditor(table));
//more code for column 3 initializatation
My problem is there are times that a row might have more than 5 colums so I check it by adding new columns everytime I need to add more columns in the model. I use
model.addColumn("ColumnName");
to add new columns. The problem is everytime I add new rows that is greater then the initial row my renderers and editors on column 2 and 3 are reset/gone and are rendered as default. What do I need so that column 2 and column 3 remains. BTW Column 2 and 3 are the only columns that are always rendered as buttons.
You can try to override createDefaultColumnsFromModel in JTable and set your special editors/renderers again. As you modify the model the JTable will re-create the columnModel if getAutoCreateColumnsFromModel is true. As you add new column I think you need this to remain true.
public void createDefaultColumnsFromModel() {
super.createDefaultColumnsFromModel();
getColumnModel().getColumn(2).setCellRender(new MyCellRender());
getColumnModel().getColumn(2).setCellEditor(new MyCellEditor(this));
}
To initially create the table you can use:
JTable table = new JTable(model);
table.setAutoCreateColumnsFromModel( false );
table.getColumnModel().getColumn(2).setCellRender(new MyCellRender());
table.getColumnModel().getColumn(2).setCellEditor(new MyCellEditor(table));
The TableColumnModel and TableColumns will be created automatically.
Now, if you want to add another column, because TableColumns are not automatically created from the model you won't lose your custom renderer/editor but now you need to create the TableColumns manually:
String columnName = "Column X";
model.addColumn( columnName );
// AutoCreate is turned off so create table column here
TableColumn column = new TableColumn( table.getColumnCount() );
column.setHeaderValue( columnNamer );
table.addColumn( column );