如何在jtable中单元单元格并更改其属性而不重置其余的



我想更改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推动一次,第一个单元格的文本更改为红色。当我再次推动输入时,列的背景中的第二个单元格变为黄色,但是第一个单元格的文本返回到默认的黑色。我该如何进行更改,以保存?是否有更好的方法来挑选特定的细胞来改变其性质?

而不是使用字符串作为表数据,您可以保留一个对象,其中包含每个单元格的信息,包括要显示的字符串,颜色以及颜色是背景还是前景。请参阅下面我实施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;
    }
}

最新更新