Java 如何在JTable中挑出一个单元格并更改其属性,而不重置其余单元格
我想更改JTable中特定单元格的属性。我可以更改前景和背景,但每次更改一个都会重置其他。这是我的代码:Java 如何在JTable中挑出一个单元格并更改其属性,而不重置其余单元格,java,swing,jtable,Java,Swing,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
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;
}
}
不使用字符串作为表的数据,您可以保留一个对象,其中包含每个单元格的信息,包括要显示的字符串、颜色以及颜色是背景还是前景。请参见下面我实现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;
}
}
由于每次调用changeCellColor时都使用成员变量(而不是本地变量),因此会覆盖以前指定哪个单元格是哪个颜色的值。整个表都会被重新绘制,因此在渲染每个单元格时,只有存储在newColor、rowToEdit等中的当前值才会被应用。这可能会对您有所帮助。它显示了如何根据单元格中的数据以不同方式呈现单元格。
TableCellRenderer
是一种共享资源,即,表中的每个单元格都使用相同的TableCellRenderer
实例呈现(对于特定的数据类型)。每次调用时都需要重置TableCellRenderer
状态,否则,它仍将设置为上次使用时的状态,因为每次调用changeCellColor时都使用成员变量(而不是本地变量),您将覆盖指定哪个单元格是哪个颜色的先前值。整个表都会被重新绘制,因此在渲染每个单元格时,只有存储在newColor、rowToEdit等中的当前值才会被应用。这可能会对您有所帮助。它显示了如何根据单元格中的数据以不同方式呈现单元格。TableCellRenderer
是一种共享资源,即,表中的每个单元格都使用相同的TableCellRenderer
实例呈现(对于特定的数据类型)。每次调用时都需要重置TableCellRenderer
状态,否则,它仍将设置为上次使用时的状态