zoukankan      html  css  js  c++  java
  • 一个关于JTabel的DefaultCellEditor渲染很棒的例子

    import java.awt.Color;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.DefaultCellEditor;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JOptionPane;
     
    import javax.swing.UIManager;
    import javax.swing.table.TableCellRenderer;
     
    public class JButtonTableExample extends JFrame {
     
        public JButtonTableExample() {
            super("JButtonTable Example");
     
            DefaultTableModel dm = new DefaultTableModel();
            dm.setDataVector(new Object[][] { { "button 1", "foo" },
                    { "button 2", "bar" } }, new Object[] { "Button", "String" });
     
            JTable table = new JTable(dm);
            table.setOpaque(true);
            table.setBackground(Color.red);
            table.getColumn("Button").setCellRenderer(new ButtonRenderer());
            table.getColumn("Button").setCellEditor(
                    new ButtonEditor(new JCheckBox()));
            JScrollPane scroll = new JScrollPane(table);
            getContentPane().add(scroll);
            setSize(400, 100);
            setVisible(true);
        }
     
        public static void main(String[] args) {
            JButtonTableExample frame = new JButtonTableExample();
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
     
    class ButtonRenderer extends JButton implements TableCellRenderer {
     
        public ButtonRenderer() {
            setOpaque(true);
        }
     
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                setBackground(table.getSelectionBackground());
            } else {
                setForeground(table.getForeground());
                setBackground(UIManager.getColor("Button.background"));
            }
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }
     
    class ButtonEditor extends DefaultCellEditor {
        protected JButton button;
        private String label;
        private boolean isPushed;
     
        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton();
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fireEditingStopped();
                }
            });
        }
     
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            if (isSelected) {
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } else {
                button.setForeground(table.getForeground());
                button.setBackground(table.getBackground());
            }
            label = (value == null) ? "" : value.toString();
            
            button.setText(label);
            isPushed = true;
            return button;
        }
     
        public Object getCellEditorValue() {
            if (isPushed) {
                JOptionPane.showMessageDialog(button, label + ": Ouch!");
            }
           isPushed = false;
            return new String(label);
        }
     
        public boolean stopCellEditing() {
           isPushed = false;
            return super.stopCellEditing();
        }
     
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }
    }

  • 相关阅读:
    【转】禁用chrome firefox 的 WebRTC功能防止真实IP泄漏
    这是我的主场
    【转】反编译获取任何微信小程序源码(完)
    【转】npm 安装express npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE
    查询最新的邮编地区
    【转】汇编语言入门教程
    Microsoft Windows远程桌面协议中间人攻击漏洞(CVE-2005-1794)漏洞解决方案(Windows server2003)
    IIS隐藏版本号教程(Windows Server 2003)
    Windows Server 2003添加防火墙策略教程
    Tomcat禁用SSLv3和RC4算法
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3285673.html
Copyright © 2011-2022 走看看