我有一个方法应该改变一个特定的单元格 ia JTable 的颜色,但是所有的单元格都会改变颜色。
这是我试图实现的方法:
public Component cellBG(JTable table, int row, int column)
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(renderer, row, column);
Object steak = table.getValueAt(row,column);
if(((String) steak).startsWith("+"))
{
c.setBackground(Color.GREEN);
}
else if(((String) steak).startsWith("-"))
{
c.setBackground(Color.RED);
}
else if(((String) steak).startsWith("~"))
{
c.setBackground(Color.YELLOW);
}
else if(((String) steak).equals(""))
{
c.setBackground(Color.WHITE);
}
return c;
}
我的表包含 10 列,每行 8 行都包含 +、-、~ 或“”,这应该会导致不同颜色的分散图案,但是在读取的最后一个条目中,表格是纯色的。
根据请求提供 SSCCE
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class ptf_viewer extends JFrame {
static JTable table;
static DefaultTableModel model;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ptf_viewer frame = new ptf_viewer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ptf_viewer(){
getContentPane().setBackground(Color.WHITE);
setTitle("Portable test file viewing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 1024, 768);
getContentPane().setLayout(null);
final String[] columnNames = {"Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"};
final Object[][] data = {
{"+","+","","","","~","","","",""},
{"-","+","","","","","","","",""},
{"+","+","","","~","","","","",""},
{"+","+","","","","","~","","",""},
{"+","-","","~","","","","","",""},
{"+","-","","","","","","","",""},
{"+","-","","","","","","","",""},
{"-","+","~","","","","","","","+"}
};
model = new DefaultTableModel(data, columnNames);
table = new JTable(data, columnNames);
table.setModel(model);
for(int i = 0 ; i < 8 ; i++){
cellBG(table, i, 9);
}
JTableHeader header = table.getTableHeader();
header.setFont(new Font("Times New Roman", Font.BOLD, 13));
header.setBackground(Color.black);
header.setForeground(Color.white);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setSize(988, 618);
scrollPane.setFont(new Font("Times New Roman", Font.BOLD, 13));
scrollPane.setLocation(10, 60);
getContentPane().add(scrollPane);
}
public Component cellBG(JTable table, int row, int column)
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(renderer, row, column);
Object bøf = table.getValueAt(row,column);
if(((String) bøf).startsWith("+"))
{
c.setBackground(Color.GREEN);
}
else if(((String) bøf).startsWith("-"))
{
c.setBackground(Color.RED);
}
else if(((String) bøf).startsWith("~"))
{
c.setBackground(Color.YELLOW);
}
else if(((String) bøf).equals(""))
{
c.setBackground(Color.WHITE);
}
return c;
}
}