gpt4 book ai didi

java - 如何在 JTable 中挑选出一个单元格并更改其属性而不重置其余单元格

转载 作者:行者123 更新时间:2023-12-02 02:27:29 25 4
gpt4 key购买 nike

我想更改 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 键,第一个单元格的文本变为红色,就像我想要的那样。当我再次按 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;
}
}

关于java - 如何在 JTable 中挑选出一个单元格并更改其属性而不重置其余单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47598510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com