简体   繁体   中英

Add color to JTable rows

I Want change the color of my JTable rows.

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行

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

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. 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 });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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