0

I am trying to create a JTable that would have a dynamic width (according to the largest element in the column) and I am using this code

JTable table = new JTable(){
    @Override
       public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
           Component component = super.prepareRenderer(renderer, row, column);
           int rendererWidth = component.getPreferredSize().width;
           TableColumn tableColumn = getColumnModel().getColumn(column);
           tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
           return component;
        }
    };
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

The dynamic width is working correctly, but then I have an event listener to chechboxes (1 chechbox = 1 column) and if they are unticked, the column should be hidden, if they are ticked again, the column should re-apear. For that I am using:

if (jCheckBox.isSelected())
{
    table.getColumn(columnName).setMinWidth(15);
    table.getColumn(columnName).setMaxWidth(2147483647);
    table.getColumn(columnName).setWidth(100);
}
else
{
    table.getColumn(columnName).setMinWidth(0);
    table.getColumn(columnName).setMaxWidth(0);
    table.getColumn(columnName).setWidth(0);
}

And this piece of code was working perfectly, but I think this line:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

breaks the functionality, now, hiding the column works, but re-appearing does not. Do you know what could be the problem? Thanks!

Sam333
  • 199
  • 14
  • 2
    Personally, I wouldn't be trying to hide the column by making it small, I'd physically remove it from the `TableColumnModel`. You could have a look at [Table Column Manager](https://tips4java.wordpress.com/2011/05/08/table-column-manager/) for one example – MadProgrammer Mar 08 '21 at 23:50
  • I see, removing seems easy then, but how do I add it back to the column? And is there a way that it remembers its location (so the re-appearing column wouldn't be added to the end of the table, but at the same position from where it disappeared? Thanks @MadProgrammer – Sam333 Mar 09 '21 at 00:08
  • 1
    *is there a way that it remembers its location* - The column appears in the right place for me. Post your [mre] demonstrating the problem. You were asked for an "MRE" with your last question: https://stackoverflow.com/questions/66521467/java-swing-table-doesnt-show-defaulttablemodels-content. An "MRE" should be included with every question. – camickr Mar 09 '21 at 00:30
  • 1
    [example](https://stackoverflow.com/questions/24174676/display-certain-columns-in-javas-abstract-table-model/24174910#24174910), [example](https://stackoverflow.com/questions/34193578/unable-to-get-hidden-jtable-column/34193726#34193726), [example](https://stackoverflow.com/questions/12000263/exclude-a-column-from-jtable-when-sorting/12000459#12000459). The basic idea is you have a "visible" model and a "invisible" model – MadProgrammer Mar 09 '21 at 02:15

2 Answers2

0

change

table.getColumn(columnName).setWidth(0);

to

table.getColumn(columnName).setPreferredWidth(0);
Ra Ven
  • 24
  • 3
  • wow, perfect! Thanks a lot, such an easy fix, working flawlessly – Sam333 Mar 09 '21 at 11:22
  • (1-) as suggested in the comments, this is NOT the way to hide a column. A column of 0 size is still part of the view. If the user tabs from column to column the "focused" column will disappear and the user will think this is a bug. The proper solution is to remove the TableColumn from the TableColumnModel. – camickr Mar 10 '21 at 19:16
  • if you want to remove column from tableColumnModel you will lose data when you want to show it again check my next solution [link](https://stackoverflow.com/a/66576004/13479297) – Ra Ven Mar 11 '21 at 03:29
0

when you hide column table focus sill has on column. fix it by Override Method on your custom Jtable class

@Override
public void changeSelection(int row, int col, boolean bln, boolean bln1) {
    int max = getColumnModel().getColumn(col).getMaxWidth();
    if (max != 0) {
        super.changeSelection(row, col, bln, bln1);
    } else {
        int nextCol = col + 1;
        int nextRow = row + 1;
        if (col == getColumnCount() - 1) {
            nextCol = 0;
        }
        if (row == getRowCount() - 1) {
            nextRow = 0;
        }
        changeSelection(nextRow, nextCol, bln, bln1);
    }
}
Ra Ven
  • 24
  • 3