繁体   English   中英

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

[英]Changing the color of a specific cell in 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 (除非您自己进行检查,否则不会检查单个单元格值的运行时类型)。 最好为TableColumn.setCellRenderer的相关列调用TableColumnModel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM