简体   繁体   English

JTable图标中的JButton /按下时动画变化

[英]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. 所以我有一个带JButton Column的JTable。 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. 根据你的代码,我想你使用罗布Camick的ButtonColumn得到的按钮栏装饰。 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. 该类实现了TableCellRendererTableCellEditor接口,以便为您指定的列中的单元格提供JButton作为渲染器和编辑器,在您的情况下是最后一个。

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. 在这两种情况下,我都不会直接修改源代码 ,而是覆盖两个getTableCellXxxComponent(...)方法,就像任何其他自定义渲染器/编辑器一样。 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). 正如@mKorbel所指出的那样,用于渲染第5列的JSpinner并不完整:如果选择了单元格,则应该应用选择背景颜色(参见第一行)。

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. 我也不能使用ButtonColumn,我发现了另一种完成这项工作的方法:向mouseListener添加mouseListener jTable其位置返回clicked单元格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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