简体   繁体   English

为JTable行添加颜色

[英]Add color to JTable rows

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; 当我在JTable中设置一行时,第一行变成红色。然后当我想添加新行时,第一行变成黄色,第二行变成红色;

红色JTable行黄红JTable行

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 我不使用repaint()因为所有时间都需要重绘。我只想一次。当我使用repaint或tablemodel.setfile ...时,表会不断更新

This can be done with a custom cell renderer . 这可以使用自定义单元格渲染器来完成。

A cell renderer is responsible for creating a Component which the cells actually display. 单元格渲染器负责创建单元格实际显示的组件。 (The cell renderer Component is not actually added to the JTable. The JTable just uses it to do painting.) DefaultTableCellRenderer creates JLabels so you can set their background and foreground colors freely. (单元格渲染器Component实际上并未添加到JTable中。JTable只是使用它来进行绘制。) DefaultTableCellRenderer创建JLabel,因此您可以自由设置其背景色和前景色。 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 });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM