我通过提供一些示例代码来澄清我之前的问题
import java.awt.Color;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable extends javax.swing.JFrame
{
private JButton jButton1;
private JScrollPane jScrollPane1;
private JTable jTable1;
public TestTable()
{
initComponents();
}
private void initComponents(){
///////////////////////////////////////////////
//Initializing components
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
String data[][] = { {"A","B","C"},
{"R","S","T"},
{"U","V","W"}
};
String col[] = {"X","Y","Z"};
jTable1 = new JTable(new DefaultTableModel(data, col));
jScrollPane1.setViewportView(jTable1);
jButton1.setText("jButton1");
///////////////////////////////////////////////
//positioning button and table
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(15, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(160, 160, 160))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(47, 47, 47)
.addComponent(jButton1)
.addContainerGap(102, Short.MAX_VALUE))
);
///////////////////////////////////////////////
//create event callback for the button
jButton1.setText("Change data & color");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
///////////////////////////////////////////////
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//change the data in the table
String data[][] = { {"a","b","c"},
{"r","s","t"},
{"u","v","w"} };
String col[] = {"x","y","z"};
DefaultTableModel model= new DefaultTableModel(data,col);
jTable1.setModel(model);
//change the header in the table
jTable1.getTableHeader().setBackground(Color.red);
// IS IT POSSIBLE TO INSERT CODE HERE, WITHOUT REINITIALIZING jTable1,
// SO THAT CLICKING THE BUTTON WILL TURN THE MIDDLE ROW GREEN?
//change button text
jButton1.setText("Changed");
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestTable().setVisible(true);
}
});
}
}
当我在 main 中运行 TestTable 时,我已经能够更改行的颜色,但是我在从回调中更改行颜色时遇到了问题(如上所述)。我的猜测是插入代码
jTable1.getCellRenderer(0, 0).getTableCellRendererComponent(jTable1, String.class, false,false, 0,0).setBackground(Color.green);
由于我在这段代码中指定了表的条目(0,0),我希望这段代码会变成条目(0,0),但是,这会将整个表格变成绿色!注意,我可以毫无问题地更改模型中的数据和标题的颜色。
真诚感谢任何帮助。谢谢克里斯