更改JTABLE中特定单元格的颜色



我正在做一门课程,3周来我一直在努力只更改特定单元格的颜色。我需要红色和绿色取决于某些条件:

private static void showGUI(){
JTable table = new JTable() ;
class BoardTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasocus, int row, int col)
{
this.setOpaque(true);
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasocus, row, col);
Color redColor = Color.RED;
Color greenColor = Color.GREEN;
//books color
for(int i=0;i<bookCount;i++){
if (library.bookArray[i].getAvailability()==1){
c.setForeground(redColor);
}else{
c.setForeground(greenColor);
}}
return c;
}
}
String columnNames[] = {"Book", "DVD"};
Object[][] data = new String[library.bookCount+1][library.dvdCount+1];
if(bookCount>0){
for(int i=0; i<bookCount;i++){
data[i][0]=(i+1)+". ISBN: "+library.bookArray[i].getISBN()+" / Title: "+library.bookArray[i].getTitle()+" / Author name: " + library.bookArray[i].getAuthorName() + " / Author Origin: " + library.bookArray[i].getAuthorOrigin() + " / Sector: " + library.bookArray[i].getSector()+" / Publication Date: " + library.bookArray[i].getPubDate() + " / Publisher: " + library.bookArray[i].getPublisherName()+" / Total pages: " + library.bookArray[i].getTotalPages();
}
}
//DVD
if (dvdCount>0){
for(int i=0; i<dvdCount;i++){
data[i][1]=(i+1)+". ISBN: "+library.dvdArray[i].getISBN()+" / Title: "+library.dvdArray[i].getTitle()+"Language: "+library.dvdArray[i].getLanguage()+" Subtitle: "+library.dvdArray[i].getSubtitle()+" Sector: "+library.dvdArray[i].getSector()+"Publication Date: "+library.dvdArray[i].getPubDate()+" Producer: "+library.dvdArray[i].getProducer()+" Actors: "+library.dvdArray[i].getActors();
}
}
table.setDefaultRenderer(String.class, new BoardTableCellRenderer());
table.setFocusable(false);
table.setRowMargin(0);
table.setIntercellSpacing(new Dimension(0, 0));
table.setRowSelectionAllowed(false);
table.setVisible(true);

TableModel model = new DefaultTableModel(data, columnNames);
table.setModel(model);
JScrollPane scrollPane = new JScrollPane(table);
table.setGridColor(Color.BLACK);
JLabel lblName = new JLabel("Enter Title to search:");
JTextField tfName= Library.createRowFilter(table);
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
panel.add(lblName);
panel.add(tfName);
tfName.setSize(30,30); 
JFrame frame = new JFrame("Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane,BorderLayout.SOUTH);
frame.add(lblName,BorderLayout.BEFORE_FIRST_LINE);
frame.add(tfName,BorderLayout.LINE_START);
frame.validate();
frame.setSize(1700, 500);
frame.setVisible(true);

正如你所看到的,我正在使用另一个类的数组,我需要红色用于不可用的书(.getAvailability((==1(,绿色用于可用的书。到目前为止,我所尝试的要么改变整列,要么改变整行

谢谢!

突出的代码有几个明显的问题(除了格式和长方法体(。

this.setOpaque(true);
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasocus, row, col);

应该是

Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
c.setOpaque(true);

你应该只为你正在处理的书设置前景。

Color redColor = Color.RED;
Color greenColor = Color.GREEN;
//books color
for(int i=0;i<bookCount;i++){
if (library.bookArray[i].getAvailability()==1){
c.setForeground(redColor);
}else{
c.setForeground(greenColor);
}}

应该是:

c.setForeground(
library.bookArray[row].getAvailability()==1 ?
Color.RED :
Color.GREEN
);

编辑:接下来,我注意到您已经将String类型的列的自定义渲染器设置为默认值,但没有设置列的Class(除非您自己检查,否则不会检查单个单元格值的运行时类型(。对于TableColumnModel的相关列,最好调用TableColumn.setCellRenderer

最新更新