Search code examples
javaswingjtablejbuttontablecellrenderer

JButton within JTable Icon/Animation Changing When Pressed


I don't really know how to explain this in words. So I have a JTable with a JButton Column. I have set an icon for this button but when I press or hold the button, it changes into the image you see below.

enter image description here

How can I disable this effect? I have tried the following to no avail:

  renderButton.setContentAreaFilled(false);
  renderButton.setIcon(new ImageIcon(ButtonColumn.class
                .getResource("/com/graphics/clear_left.png")));
  renderButton.setRolloverIcon(new ImageIcon(ButtonColumn.class
                .getResource("/com/graphics/clear_left.png")));
  renderButton.setRolloverSelectedIcon(new ImageIcon(ButtonColumn.class
                .getResource("/com/graphics/clear_left.png")));
  renderButton.setPressedIcon(new ImageIcon(ButtonColumn.class
                .getResource("/com/graphics/clear_left.png")));

Solution

  • Based on your code I think you use Rob Camick's ButtonColumn to get the buttons column decoration. That class implements both TableCellRenderer and TableCellEditor interfaces to provide a JButton as renderer and editor for the cells in the column you specify, in your case the last one.

    What you did is to customize the renderer button to show the icon you want but now you have to customize the editor button as well to override the default button's look.

    In both cases I wouldn't modify the source code directly but override both getTableCellXxxComponent(...) methods instead, just like any other custom renderer/editor. Something like this will make it:

    JTable table = new JTable(tableModel);
    
    Action action = new AbstractAction() {...};
    
    ButtonColumn buttonColumn = new ButtonColumn(table, action, 5) {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JButton button = (JButton)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            button.setContentAreaFilled(false);
            button.setBorder(BorderFactory.createEmptyBorder());
            // Customize the icon and whatever you want here
            return button;
        }
    
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            JButton button = (JButton)super.getTableCellEditorComponent(table, value, isSelected, row, column);
            button.setContentAreaFilled(false);
            // Customize the icon and whatever you want here
            return button;
        }
    };
    

    Notes

    Always include a link to non-standard libraries or classes. Otherwise people won't be a able to help you with classes they are not familiar with.

    As @mKorbel pointed out, the JSpinner used to render the 5th column isn't complete: the selection background color should be applied if the cell is selected (see the first row).