将颜色添加到JTable行 [英] Add color to JTable rows

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

问题描述

我想更改我的JTable行的颜色。

I Want change the color of my JTable rows.

当我在JTable中设置一行,第一行变成红色。然后当我想添加新第一列为黄色,第二列为红色;

When i set a row in JTable,The first row become RED color.And then when i want add new row the first row become yellow and the second row become red;


我不使用repaint()Becouse重绘所有的时间work.I只需一次。当我使用repaint或tablemodel.setfile ...表不断更新

I do not use repaint() Becouse repaint all time work.I want just one time.When i Use repaint or tablemodel.setfile... the table constantly update

推荐答案

这可以通过 custom cell renderer

单元格渲染器负责创建显示单元格的组件。 (JTable中的每个单元格实际上是一个组件。) DefaultTableCellRenderer 创建JLabel,以便您可以自由设置其背景和前景颜色。你不需要画画。

A cell renderer is responsible for creating the Components that display the cells. (Each cell in a JTable is actually a Component.) DefaultTableCellRenderer creates JLabels so you can set their background and foreground colors freely. You don't need to do painting.


多显示你想要的单元格。我不知道你想要它如何照顾两行,所以我猜。

You can pretty much display the cells however you want to. I wasn't sure how you wanted it to look after two rows so I guessed.

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class CustomCellRenderer implements Runnable, ActionListener {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CustomCellRenderer());
    }

    JTable table;

    @Override
    public void run() {
        JFrame frame = new JFrame("Custom Cell Renderer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        table = new JTable(new DefaultTableModel(0, 2) {
            @Override
            public Class<?> getColumnClass(int c) {
                return Object.class;
            }
        });

        class RedYellowRenderer extends DefaultTableCellRenderer {
            RedYellowRenderer() {
                setHorizontalAlignment(CENTER);
            }
            @Override
            public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected, boolean hasFocus,
                int row, int column
            ) {
                Component c = super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column
                );

                if(row == 0 && table.getRowCount() > 1) {
                    c.setBackground(Color.YELLOW);
                    c.setForeground(Color.BLACK);
                } else {
                    c.setBackground(Color.RED);
                    c.setForeground(Color.WHITE);
                }

                return c;
            }
        }

        table.setDefaultRenderer(Object.class, new RedYellowRenderer());
        table.setTableHeader(null);

        JButton btn = new JButton("Add Row");
        btn.addActionListener(this);

        JToolBar bar = new JToolBar();
        bar.setFloatable(false);
        bar.add(btn);

        JPanel content = new JPanel(new BorderLayout());
        content.add(bar, BorderLayout.NORTH);
        content.add(new JScrollPane(table), BorderLayout.CENTER);

        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        int nextRow = table.getRowCount();
        DefaultTableModel model = (DefaultTableModel)table.getModel();
        model.addRow(new Object[] { "NAME" + nextRow, "SPORT" + nextRow });
    }
}

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

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