简体   繁体   中英

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.

在此输入图像描述

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

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

I can't use ButtonColumn as well and I have found another way to do the job: add a mouseListener to the jTable and return clicked cell by its position.

Rectangle[] cells;
for(int i=0;i<row;i++){cells[i] = Table_Skill.getCellRect(i, 7, false);}

MouseListener ml = new MouseListener(){
    @Override
    public void mouseClicked(MouseEvent e){
        Point point_clicked = new Point(e.getX(),e.getY());
            for(int i=0;i<row;i++){
                if(cells[i].contains(point_clicked))
                {
                //some action like deleting the row
                break;
                }
            }
    }
    @Override
    public void mousePressed(MouseEvent e) {}
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
};
Table_Skill.addMouseListener(ml);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM