adding data after add column in jtable - java

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

Related

how do I determine which columns in a JTable are visible

I'm resizing my columns according to their data with the following:
/**
* Set the widths of the columns in the table according to the data in the table.
* #param table
*/
private static void setColumnWidths(JTable table)
{
int columnCount = table.getModel().getColumnCount();
int rowCount = table.getModel().getRowCount();
TableColumnModel columnModel = table.getColumnModel();
for (int col=0; col<columnCount; col++)
{
TableColumn column = columnModel.getColumn(col);
TableCellRenderer renderer = column.getCellRenderer();
if (renderer == null)
{
renderer = new DefaultTableCellRenderer();
}
int overallColumnWidth = 0;
for (int row = 0; row < rowCount; row++)
{
Object value = table.getValueAt(row, col);
Component component = renderer.getTableCellRendererComponent(table, value, false, false, row, col);
int componentWidth = (int) component.getPreferredSize().getWidth();
overallColumnWidth = Math.max(componentWidth, overallColumnWidth);
}
column.setPreferredWidth(overallColumnWidth);
}
}
This works when all the columns are visible, but I've got code that hides some of them (JTable.removeColumn(TableColumn column)). It appears that both the JTable instance and the TableColumnModel instance have the list of data columns, i.e., all columns regardless of visibility. How can I get a list of only columns that are visible, or test whether a given column is visible?
(I've searched for this, but get longs lists of articles on how to hide the columns, and no explanation of where the visible/invisible information is kept. I thought, since I asked the JTable to hide the column, that somewhere in there it would know which columns were hidden and I could get that info.)
Check if table.convertColumnIndexToView(col) returns -1.
From the documentation:
public int convertColumnIndexToView(int modelColumnIndex)
Maps the index of the column in the table model at modelColumnIndex to
the index of the column in the view. Returns the index of the
corresponding column in the view; returns -1 if this column is not
being displayed. If modelColumnIndex is less than zero, returns
modelColumnIndex.
Source
The problem with your code if that your loops is controlled by the values returned in the TableMdoel.
Instead you should be using values from the table:
int rowCount = table.getRowCount();
int columnCount = table.getRowCount();
Then there is no need for exception logic to determine if a column is visible or not because by default it will be visible because it is the job of the view (JTable) to only display visible columns.
You need to understand the difference between the "view" and the "model".
how do I determine which columns in a JTable are visible
So the better answer is to use the methods of the JTable. Your current solution confuses the view and model.
You could also use the Table Column Adjuster to do the column adjustment for you. It provides features like dynamically resizing the column as the data changes.

How do I recreate the width of columns in a JTable after changing the ColumnModel

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?

Tooltips on filtered rows in a table

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

CellRenderer and Editor reset when adding new columns

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

Retrieving JTable line content after user sorted content by clicking on column

I have a pane with two tables A and B. When a row is selected in A, the content of B should be upated.
My code detects row selections in A fine. But, when the user clicks on the column header to sort rows, it does not seem like this is taken into account in A's table model.
So, I can get the selected row number (which is correct considering the sorting), but when I try to retrieve row field content from A using its table model, it gives me the values as if rows had not been sorted.
How can I retrieve the content of the selected line from the selected row number?
Without any code, it is hard to say for sure what your problem is. However, it sounds like you are mixing up the row indices between the view and the model. You must be very clear about what co-ordinate system you are referring to (view or model) when you have a row number. See the JTable API for the convertRowIndexToModel and convertRowIndexToView methods.
You probably need something like this:
JTable table = ...;
TableModel model = ...;
int viewRow = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel(viewRow);
int viewColumn = table.getSelectedColumn();
int modelColumn = table.convertColumnIndexToModel(viewColumn);
Object cell = model.getValueAt( modelRow, modelColumn );

Categories

Resources