| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 931 人关注过本帖
标题:单元格颜色设置无效
只看楼主 加入收藏
mahayu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:160
注 册:2007-8-3
收藏
 问题点数:0 回复次数:1 
单元格颜色设置无效
package com.corejava.L613;

/**
 * 例题6-13颜色设置未完成!
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableCellRenderTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new TableCellRenderFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class TableCellRenderFrame extends JFrame {
    public TableCellRenderFrame() {
        setTitle("表格组合测试");
        setSize(600, 400);

        TableModel model = new PlanetTableModel();
        JTable table = new JTable(model);
        table.setRowSelectionAllowed(false); // 设置是否可以选择此模型中的行。

        //建立单元格渲染器与编辑器
        table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
        table.setDefaultEditor(Color.class, new ColorTableCellEditor());

        JComboBox moonCombo = new JComboBox();
        for (int i = 0; i <= 20; i++)
            moonCombo.addItem(i);

        TableColumnModel columnModel = table.getColumnModel();
        TableColumn moonColumn = columnModel
                .getColumn(PlanetTableModel.MOONS_COLUMN);
        moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
        moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
        moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));

        // 显示表格
        table.setRowHeight(100);
        add(new JScrollPane(table), BorderLayout.CENTER);
    }
}

class PlanetTableModel extends AbstractTableModel {
    public static final int PLANET_COLUMN = 0;
    public static final int MOONS_COLUMN = 2;
    public static final int GASEOUS_COLUMN = 3;
    public static final int COLOR_COLUMN = 4;

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

    public Class getColumnClass(int c) {
        return cells[0][c].getClass();
    }

    public int getColumnCount() {
        return cells[0].length;
    }

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

    public Object getValueAt(int r, int c) {
        return cells[r][c];
    }

    public void setValueAt(Object obj, int r, int c) {
        cells[r][c] = obj;
    }

    public boolean isCellEditable(int r, int c) {
        return c == PLANET_COLUMN || c == MOONS_COLUMN || c == GASEOUS_COLUMN
                || c == COLOR_COLUMN;
    }

    private Object[][] cells = {
            { "Mercury", 2440.0, 0, false, Color.yellow, new ImageIcon("a.gif") },
            { "Venus", 6052.0, 0, false, Color.yellow, new ImageIcon("a.gif") },
            { "Earth",6378.0, 1, false, Color.blue, new ImageIcon("a.gif") },
            { "Mars", 3397.0, 2, false, Color.red, new ImageIcon("a.gif") },
            { "Jupiter", 71492.0, 16, true, Color.orange, new ImageIcon("a.gif") },
            { "Saturn", 50268.0, 18, true, Color.orange, new ImageIcon("a.gif") },
            { "Uranus", 25559.0, 17, true, Color.blue, new ImageIcon("a.gif") },
            { "Neptune", 24766.0, 8, true, Color.blue, new ImageIcon("a.gif") },
            { "Pluto", 1137.0, 1, false, Color.black, new ImageIcon("a.gif") }};

    private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous",
            "Color", "Image" };
}

class ColorTableCellRenderer extends JPanel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        setBackground((Color) value);
        if (hasFocus)
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        else
            setBorder(null);
        return this;
    }
}

class ColorTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {
    private Color color;
    private JColorChooser colorChooser;
    private JDialog colorDialog;
    private JPanel panel;

    public ColorTableCellEditor() {
        panel = new JPanel();

        colorChooser = new JColorChooser();
        colorDialog = JColorChooser.createDialog(null, "色彩选择", false,
                colorChooser, new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        stopCellEditing();
                    }
                }, new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        cancelCellEditing();
                    }
                });
        colorDialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                cancelCellEditing();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelecte, int row, int column) {
        colorChooser.setColor((Color) value);
        return panel;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        colorDialog.setVisible(true);
        return true;
    }

    public void cancelCellEditing() {
        colorDialog.setVisible(false);
        super.cancelCellEditing();
    }

    public boolean stopCellEditing() {
        colorDialog.setVisible(false);
        super.cancelCellEditing();
        return true;
    }

    public Object getCellEditorValue() {
        return colorChooser.getColor();
    }
}
这段程序在Color列单击时会弹出一个颜色选择对话框,但是选择后,单元格颜色没有改变,为何?
搜索更多相关主题的帖子: 单元 颜色 
2008-03-15 13:44
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
建议你看看JAVA自带的DEMO,里面就有对JTable很详细的操作,源码里面也有

可惜不是你,陪我到最后
2008-03-15 21:33
快速回复:单元格颜色设置无效
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020478 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved