gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:38 24 4
gpt4 key购买 nike

我正在做一门类(class),我已经奋斗了三周,只为改变特定单元格的颜色。根据某些条件,我需要红色和绿色:

    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

关于java - 更改 JTABLE 中特定单元格的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53539158/

24 4 0
文章推荐: python - 合并两个 Pandas 交叉表 : Index and Col Name Issues
文章推荐: html - Mozilla Firefox CSS 无法在
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com