如何在不重置其余部分的情况下挑出 JTable 中的一个单元格并更改其属性

How to single out a cell in a JTable and change its properties without resetting the rest

我想更改 JTable 中特定单元格的属性。我可以更改前景和背景,但每次更改其中一个时,其他的都会重置。这是我的代码:

import java.awt.Color;
import java.awt.Component;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Main {
    static JTable table;
    static Color newColor = Color.white;
    static int rowToEdit = 0;
    static int columnToEdit = 0;
    static char bOrF = ' ';

    public static void Gui(){
        JFrame frame = new JFrame("Window");
        frame.setSize(800,600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        String[] columnNames = {"1","2","3"};
        Object[][] data = {
                {"A","B","C"},
                {"D","E","F"},
                {"H","I","J"}
        };

        TableModel model = new DefaultTableModel(data,columnNames){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };

        table = new JTable(model);
        JScrollPane sPane = new JScrollPane(table);

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setColumnSelectionAllowed(false);
        table.getTableHeader().setReorderingAllowed(false);
        table.setCellSelectionEnabled(false);
        table.setDefaultRenderer(Object.class, new Renderer());
        panel.add(sPane);
        frame.setContentPane(panel);
        frame.setVisible(true);

        Scanner in = new Scanner(System.in);
        System.out.print(in.nextLine());
        changeCellColor(0,0,Color.red,'f');
        table.repaint();
        System.out.print(in.nextLine());
        changeCellColor(1,0,Color.yellow,'b');
        table.repaint();

    }
    public static void changeCellColor(int row, int column, Color color, char backgroundOrForeground){
        rowToEdit = row;
        columnToEdit = column;
        newColor = color;
        bOrF = backgroundOrForeground;
    }
    public static void main(String[] args){
        Gui();
    }
}
class Renderer implements TableCellRenderer{
    Main m = new Main();
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
         JTextField editor = new JTextField();
            if (value != null)
              editor.setText(value.toString());
            if(row==m.rowToEdit && column==m.columnToEdit){
                if (m.bOrF == 'b'){
                    editor.setBackground(m.newColor);
                }
                else if(m.bOrF == 'f'){
                    editor.setForeground(m.newColor);
                }
            }
            return editor;
    }

}

我按了一次回车键,第一个单元格的文本变成了红色,就像我希望的那样。当我再次按下 enter 键时,列背景中的第二个单元格变为黄色,如我所愿,但第一个单元格的文本又恢复为默认的黑色。我如何才能保存更改?有没有更好的方法来挑出特定的细胞来改变它们的属性?

您可以保留一个包含每个单元格信息的对象,而不是使用字符串作为 table 的数据,包括要显示的字符串、颜色以及颜色是背景还是前景。请参阅下面我实现 CellInfo 的位置,并使用其中的信息来呈现每个单元格。

public class Main {
    static JTable table;

    static CellInfo[][] data = {
            {new CellInfo("A"),new CellInfo("B"), new CellInfo("C")},
            {new CellInfo("D"),new CellInfo("E"), new CellInfo("F")},
            {new CellInfo("H"), new CellInfo("I"), new CellInfo("J")}
    };

    public static void Gui(){
        JFrame frame = new JFrame("Window");
        frame.setSize(800,600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        String[] columnNames = {"1","2","3"};


        TableModel model = new DefaultTableModel(data,columnNames){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };

        table = new JTable(model);
        JScrollPane sPane = new JScrollPane(table);

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setColumnSelectionAllowed(false);
        table.getTableHeader().setReorderingAllowed(false);
        table.setCellSelectionEnabled(false);
        table.setDefaultRenderer(Object.class, new Renderer());
        panel.add(sPane);
        frame.setContentPane(panel);
        frame.setVisible(true);

        Scanner in = new Scanner(System.in);
        System.out.print(in.nextLine());
        changeCellColor(0,0,Color.red,'f');
        table.repaint();
        System.out.print(in.nextLine());
        changeCellColor(1,0,Color.yellow,'b');
        table.repaint();

    }

    public static void changeCellColor(int row, int column, Color color, char backgroundOrForeground){
        data[row][column].color = color;
        data[row][column].bOrF = backgroundOrForeground;
    }
    public static void main(String[] args){
        Gui();
    }
}

class Renderer implements TableCellRenderer{
    Main m = new Main();
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
         JTextField editor = new JTextField();
            if (value instanceof CellInfo)
            {
                CellInfo info = (CellInfo) value;
                editor.setText(info.display);

                if (info.bOrF == 'b'){
                    editor.setBackground(info.color);
                }
                else if(info.bOrF == 'f'){
                    editor.setForeground(info.color);
                }
            }
            return editor;
    }
}

class CellInfo
{
    String display;
    char bOrF = ' ';
    Color color = Color.black;

    public CellInfo(String display)
    {
        this.display = display;
    }

    public void setColor(Color color)
    {
        this.color = color;
    }

    public void setBorF(char bOrF)
    {
        this.bOrF = bOrF;
    }
}