⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demouserenderaddeditor.java

📁 java 开发工具书上的一些代码
💻 JAVA
字号:
/*
 * DemoUseRenderAddEditor.java
 *
 * Created on 2006年4月9日, 下午4:39
 */

package org.netbeans.swing.table;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.border.*;

/**
 *
 * @author  boyingking
 */
public class DemoUseRenderAddEditor extends javax.swing.JFrame {
    
    /** Creates new form DemoUseRenderAddEditor */
    public DemoUseRenderAddEditor() {
        initComponents();
        this.initialJTable();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMyTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u8868\u683c\u7f16\u8f91\u5668\u4e0e\u7ed8\u5236\u5668\u793a\u4f8b");
        jTableMyTable.setModel(new MyTableModel());
        jScrollPane1.setViewportView(jTableMyTable);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DemoUseRenderAddEditor().setVisible(true);
            }
        });
    }
    private void initialJTable()
    {
        this.jTableMyTable.setDefaultRenderer(Color.class,new ColorRenderer());
        this.jTableMyTable.setDefaultEditor(Color.class,new ColorEditor());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMyTable;
    // End of variables declaration//GEN-END:variables
 class ColorEditor extends AbstractCellEditor implements TableCellEditor,ActionListener {
    Color currentColor;
    JButton button;
    JColorChooser colorChooser;
    JDialog dialog;
    protected static final String EDIT = "edit";
    public ColorEditor() {
        button = new JButton();
        button.setActionCommand(EDIT);
        button.addActionListener(this);

        //创建颜色选择器对象,并使用该颜色选择器创建对话框
        colorChooser = new JColorChooser();
        dialog = JColorChooser.createDialog(button,"选择颜色",true,  colorChooser,this, null); 
    }

    public void actionPerformed(ActionEvent e) {//该方法根据用户的选择确定背景颜色
        if (EDIT.equals(e.getActionCommand())) {
            button.setBackground(currentColor);
            colorChooser.setColor(currentColor);
            dialog.setVisible(true);

            //通知绘制器编辑完成
            fireEditingStopped();

        } else { //用户按下了“确定”按钮
            currentColor = colorChooser.getColor();
        }
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {//实现getCellEditorValue方法
        return currentColor;
    }

    //实现getTableCellEditorComponent方法
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {
        currentColor = (Color)value;
        return button;
    }
}

 class ColorRenderer extends JLabel implements TableCellRenderer {
    public ColorRenderer() {
        setOpaque(true); //该方法用来显示背景颜色
    }

    public Component getTableCellRendererComponent(//重写父类方法,
                            JTable table, Object color,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
        Color newColor = (Color)color;
        setBackground(newColor);   
        return this;
    }
}
    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"姓名",
                                        "喜欢的颜色",
                                        "爱好",
                                        "年龄",
                                        "从事该职业"};
        private Object[][] data = {
            {"王政", new Color(153, 0, 153),
             "玩耍", new Integer(2), new Boolean(false)},
            {"伊飞", new Color(51, 51, 153),
             "上课", new Integer(22), new Boolean(true)},
            {"王烨", new Color(51, 102, 51),
             "聊天", new Integer(2), new Boolean(false)},
            {"阿森", Color.red,
             "写程序", new Integer(19), new Boolean(true)},
            {"小伟", Color.pink,
             "玩扑克", new Integer(24), new Boolean(false)}
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        //表格使用该方法决定默认的单元格的编辑器,如果不重写该方法,则最后一列包含“true”或者“false”
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {//确定单元格的可编辑性
            if (col < 1) {
                return false;
            } else {
                return true;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);

        }


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -