[英]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.