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 );
Related
I create a simple JTable with TableModel, and I hidden the first column.
I set at my JTable also the TableRowSorter
This is the code that I use to create a table
tableModelArticoliVendere = new MyTableModelDescrizioneArticoli();
tableArticoliVendere = new CustomTableArticoliDaVendereBar(tableModelArticoliVendere);
sorter = new TableRowSorter<MyTableModelDescrizioneArticoli>(tableModelArticoliVendere);
tableArticoliVendere.setRowSorter(sorter);
tableArticoliVendere.addMouseListener(new MyMouseAdapterArticoliDaVendere());
tableArticoliVendere.removeColumn(tableArticoliVendere.getColumnModel().getColumn(0));
If the user click on the one row of the table the mouse listenere are called.
This is the method:
public class MyMouseAdapterArticoliDaVendere extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
JTable t = (JTable)me.getSource();
if (me.getClickCount() == 1) {
String codiceArticolo =((JTable)tableArticoliVendere).getModel().getValueAt(t.getSelectedRow(), 0).toString();
inserisciProdotto(codiceArticolo);
}
}
}
The problem is this:
If I see the complete table and I clik on the one of the row table, I read the codiceArticolo right. If I use the row filter, and I try to click on the first row I have an error.
I have the table with 3 row for example:
TABLE
column 0| column 1
------------------
valore1 | 1
valore2 | 2
valore3 | 3
If I use the filter I have this situation:
TABLE
column 0| column 1
------------------
valore2 | 2
valore3 | 3
if I try to click on the first row, the value of codiceArticolo is valore1 and not valore2.
If I not hidden the column 0, I don't have this error.
When you have sorting or filtering enabled in your table, the indexes of table rows and columns stop lining up with indexes of the model rows and columns. You can account for this using convertRowIndexToModel and convertColumnIndexToModel.
For instance, when you use t.getSelectedRow(), you can adjust like this:
int tableRowIndex = t.getSelectedRow();
int modelRowIndex = t.convertRowIndexToModel(tableRowIndex);
It will also help if you indicate in your code when you are using view indexes and when you are using model indexes.
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
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.