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

问题描述 投票:0回答:1

我正在做一门课程,我已经奋斗了三个星期才只改变特定单元格的颜色。根据某些条件,我需要红色和绿色:

    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),绿色表示可用的书籍。 到目前为止我所尝试的要么改变整列要么改变整行

谢谢你!

java jtable tablecellrenderer
1个回答
0
投票

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

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
(不会检查单个单元格值的运行时类型,除非您自己做)。最好致电
TableColumn.setCellRenderer
获取
TableColumnModel
的相关栏目。

© www.soinside.com 2019 - 2024. All rights reserved.