开关按钮的颜色JTable中 [英] Switching color of Buttons in a JTable

查看:215
本文介绍了开关按钮的颜色JTable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上点击变色JTable中的细胞内按钮实施。我已经做了颜色改变的onClick但我不明白的是,背景色是如何停留在按钮切换到颜色。每当我通过单击另一个单元格或表格内的按钮松动的焦点再次白色。我认为这是与渲染,也许你能支持我。

I'm trying to implement an on click color changing button inside a cell of a JTable. I did already that the color is changed onClick but what i don't get is, how the background color stays in the color the button is switched to. Anytime i loose the focus by clicking in another cell or inside the table the Button is again white. I think it has something to do with the renderers, maybe you can support me.

    signalTable = new JTable();
    signalTable.setModel(new DefaultTableModel(
        new Object[][] {
            {"AAA", "A_SIGNAL", null, null, null, null, null, null, null, null, "Example", Boolean.TRUE},
        },
        new String[] {
            "ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
        }
    ) {
        Class[] columnTypes = new Class[] {
            String.class, String.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, String.class, Boolean.class
        };
        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    });
    TableColorRenderer renderer = new TableColorRenderer();
    signalTable.getColumn("0").setCellRenderer(renderer);
    signalTable.getColumn("1").setCellRenderer(renderer);
    signalTable.getColumn("2").setCellRenderer(renderer);
    signalTable.getColumn("3").setCellRenderer(renderer);
    signalTable.getColumn("4").setCellRenderer(renderer);
    signalTable.getColumn("5").setCellRenderer(renderer);
    signalTable.getColumn("6").setCellRenderer(renderer);
    signalTable.getColumn("7").setCellRenderer(renderer);
    signalTable.getColumn("0").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("1").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("2").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("3").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("4").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("5").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("6").setCellEditor(new ButtonEditor(new JCheckBox()));
    signalTable.getColumn("7").setCellEditor(new ButtonEditor(new JCheckBox()));

ButtonEditor.java:

ButtonEditor.java:

public class ButtonEditor extends DefaultCellEditor
{
    protected JButton button;

    public ButtonEditor(JCheckBox checkBox) 
    {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                button.setBackground(Color.GREEN);
                button.repaint();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) 
    {
        return button;
    }

}

TableColorRenderer.java:

TableColorRenderer.java:

public class TableColorRenderer extends JLabel implements TableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    {
        return this;
    }

当隐形按钮点击

点击后,另一行

推荐答案

您显然是误解了编辑器和渲染器的作用。一个编辑器允许用户编辑一个单元的状态,渲染器渲染单元的状态。

You're clearly misunderstanding the roles of the editor and the renderer. A editor allows a user to edit the state of a cell, a renderer renders the state of the cell.

他们这样做是通过使用的TableModel 的。通过在看看概念开始:编辑器和渲染器的和使用其他编辑了解更多详情。

They do this through the use of the TableModel. Start by having a look at Concepts: Editors and Renderers and Using Other Editors for more details.

要开始与你做错了的事情...

To start with the things you're doing wrong...

signalTable.setModel(new DefaultTableModel(
    new Object[][] {
        {"AAA", "A_SIGNAL", null, null, null, null, null, null, null, null, "Example", Boolean.TRUE},
    },
    new String[] {
        "ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
    }
) {
    Class[] columnTypes = new Class[] {
        String.class, String.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, JButton.class, String.class, Boolean.class
    };
    public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
    }
});

有关宜真正(也可以是其他任何上的颜色细胞中的单元格的值/关闭值你想要的,只要编辑器和渲染器知道如何对付他们,但是在这个例子中,我使用布尔 S)

The cell values for the "color" cells should true or false (they can be any other "on"/"off" value you want, so long as the editor and renderer know how to deal with them, but for this example, I'm using booleans)

columnType S表示这些细胞也应该是 Boolean.class

The columnTypes for these cells should also be Boolean.class

signalTable.setModel(new DefaultTableModel(
        new Object[][]{
            {"AAA", "A_SIGNAL", false, false, false, false, false, false, false, false, "Example", Boolean.TRUE},},
        new String[]{
            "ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
        }
) {
    Class[] columnTypes = new Class[]{
        String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, String.class, Boolean.class
    };

    public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
    }
});

所以,现在,如果你什么也不做,你就必须 JCheckBox的 S IN这些细胞,但是这不是我们想要的。

So, now, if you did nothing else, you would have JCheckBoxs in those cells, but that's not what we want.

public class ButtonEditor extends DefaultCellEditor
{
    protected JButton button;

    public ButtonEditor(JCheckBox checkBox) 
    {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                button.setBackground(Color.GREEN);
                button.repaint();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) 
    {
        return button;
    }

}

在这里,你忽略了 JCheckBox的被传递到你的编辑和创建自己的,但有两个问题。在 DefaultCellEditor 是派生从 JCheckBox的,而不是其他一些国家(所以它总是假),你永远不配置按钮时,它的要求重新present电池的当前状态。

Here, you are ignoring the JCheckBox which is passed to your editor and creating your own, but there are two problems with this. The DefaultCellEditor is deriving the cells value from the JCheckBox, not some other state (so it's always false) and you never configure the button to represent the current state of the cell when it's requested.

由于我不想处理其他组件,我打算做的东西有点不同。

Because I don't want to deal with other components, I'm going to do something a little different.

public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {

    private JLabel editor;

    public ButtonEditor() {
        editor = new JLabel();
        editor.setBackground(Color.GREEN);

        editor.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                editor.setOpaque(!editor.isOpaque());
                stopCellEditing();
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        if (value instanceof Boolean) {
            boolean result = (boolean) value;
            editor.setOpaque(!result);
        }
        return editor;
    }

    @Override
    public Object getCellEditorValue() {
        return editor.isOpaque();
    }

    @Override
    public boolean isCellEditable(EventObject e) {
        return true;
    }

}

这仅仅是一个的JLabel 点击它时,会切换它的不透明状态。我们也利用这个状态,以确定单元格的值时,编辑器停止

This is just a JLabel which when clicked, will switch it's opaque state. We also use this state to determine the cell value when the editor is "stopped"

public class TableColorRenderer extends JLabel implements TableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    {
        return this;
    }
}

坦白地说,这是一个有点用处,它永远不会做任何事情与单元格的值

Well, frankly, that's a bit useless, it never does anything with the value of cell

public class TableColorRenderer extends JLabel implements TableCellRenderer {

    public TableColorRenderer() {
        setBackground(Color.GREEN);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value instanceof Boolean) {
            boolean result = (boolean) value;
            setOpaque(result);
        } else {
            setOpaque(false);
        }
        return this;
    }
}

再次pretty简单。我们检查单元格(如传递给我们)的值,并相应修改不透明状态

Again, pretty simple. We check the value of the cell (as passed to us) and change the opaque state accordingly

和,因为从来没有人相信我,一个可运行的例子

And because no one ever believes me, a runnable example

编辑

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class Example {

    public static void main(String[] args) {
        new Example();
    }

    public Example() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final JTable signalTable;

        public TestPane() {
            signalTable = new JTable();
    signalTable.setModel(new DefaultTableModel(
            new Object[][]{
                {"AAA", "A_SIGNAL", false, false, false, false, false, false, false, false, "Example", Boolean.TRUE},},
            new String[]{
                "ID", "Message Identifier", "0", "1", "2", "3", "4", "5", "6", "7", "Description", ""
            }
    ) {
        Class[] columnTypes = new Class[]{
            String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class, String.class, Boolean.class
        };

        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    });
            TableColorRenderer renderer = new TableColorRenderer();
            signalTable.getColumn("0").setCellRenderer(renderer);
            signalTable.getColumn("1").setCellRenderer(renderer);
            signalTable.getColumn("2").setCellRenderer(renderer);
            signalTable.getColumn("3").setCellRenderer(renderer);
            signalTable.getColumn("4").setCellRenderer(renderer);
            signalTable.getColumn("5").setCellRenderer(renderer);
            signalTable.getColumn("6").setCellRenderer(renderer);
            signalTable.getColumn("7").setCellRenderer(renderer);
            signalTable.getColumn("0").setCellEditor(new ButtonEditor());
            signalTable.getColumn("1").setCellEditor(new ButtonEditor());
            signalTable.getColumn("2").setCellEditor(new ButtonEditor());
            signalTable.getColumn("3").setCellEditor(new ButtonEditor());
            signalTable.getColumn("4").setCellEditor(new ButtonEditor());
            signalTable.getColumn("5").setCellEditor(new ButtonEditor());
            signalTable.getColumn("6").setCellEditor(new ButtonEditor());
            signalTable.getColumn("7").setCellEditor(new ButtonEditor());

            setLayout(new BorderLayout());
            add(new JScrollPane(signalTable));
        }

    }

    public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {

        private JLabel editor;

        public ButtonEditor() {
            editor = new JLabel();
            editor.setBackground(Color.GREEN);

            editor.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    editor.setOpaque(!editor.isOpaque());
                    stopCellEditing();
                }
            });
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            if (value instanceof Boolean) {
                boolean result = (boolean) value;
                editor.setOpaque(!result);
            }
            return editor;
        }

        @Override
        public Object getCellEditorValue() {
            return editor.isOpaque();
        }

        @Override
        public boolean isCellEditable(EventObject e) {
            return true;
        }

    }

    public class TableColorRenderer extends JLabel implements TableCellRenderer {

        public TableColorRenderer() {
            setBackground(Color.GREEN);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof Boolean) {
                boolean result = (boolean) value;
                setOpaque(result);
            }
            return this;
        }
    }
}

这篇关于开关按钮的颜色JTable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆