javaswingswingxjxtable

Remove column from column control popup menu


Is it possible to control whether a column should be available in a column control popup menu? I'm aware of toggling (Disable/enable using CheckBoxList) and gray-out the column. But I do not want column entry in popup menu as The column is must-have column in Jtable. I'm using the JXTable. Anyone that have any hints?


Solution

  • A TableColumnExt has a property hideable which effectly disables the hiding. It is still shown in the popup and you can toggle the checkbox (that's a bug, just filed - the menu item should be disabled ;), but at least the column isn't hidden. To work around the bug, you can implement a custom column control (as Robin correctly suggested) which doesn't add the checkbox, something like:

    JXTable table = new JXTable(new AncientSwingTeam());
    // here the hideable property is configured manually, 
    // in production code you'll probably have a custom ColumnFactory
    // doing it based on some data state 
    table.getColumnExt(0).setHideable(false);
    ColumnControlButton columnControl = new ColumnControlButton(table) {
    
        @Override
        protected ColumnVisibilityAction createColumnVisibilityAction(
                TableColumn column) {
            if (column instanceof TableColumnExt
                    && !((TableColumnExt) column).isHideable())
                return null;
            return super.createColumnVisibilityAction(column);
        }
    
    };
    table.setColumnControl(columnControl);
    table.setColumnControlVisible(true);
    

    As to not including the menu item: when introducing the hideable property, we decided to go for keeping the item in the list but disable it because users might get confused not seeing all columns in the control. So once the bug will be fixed (just done, committed as of revision #4315), I would recommend to remove the custom column control again. Just my 2 euro-cents, though :-)