簡體   English   中英

如何在JTable中用367個單元格進行一行,其中只有第一個單元格具有文本,其余單元格具有顏色

[英]How to make a row in JTable with 367 cells where only the first one has text and the rest have colors

我正在開發一個基於顏色的數據庫程序,該程序可根據顏色顯示住宿內的預訂。 我一直遇到的問題是該表有367列(住宿的名稱,然后是一年中的所有天。)我一直遇到的問題是我不知道如何制作第一列的行顯示字符串,其余的顯示顏色。

我制作了一個基本的渲染器,應該顯示顏色,但是我不知道如何使用它。

public class MyRenderer extends DefaultTableCellRenderer{
   public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
   {
       Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
       if (! table.isRowSelected(row))
       { 
           if(row == 2 && column == 2)
               c.setBackground(new java.awt.Color(0, 0, 255));
           else
               c.setBackground(table.getBackground());  
       } 
       return c;
   } 
}

如何將其與JTable集成?

PS我有一個要顯示的標題,但行開頭應該是空的。 然后,當按下按鈕時,應該添加一行。 最后一個按鈕,我可以自己動手,我只需要cellrenderer的幫助

現在,我的JTable像這樣初始化: JTable table = new JTable();

有人有提示嗎?

我想一天有兩種狀態,有沒有預定。 因此該value布爾值。 您可以按類類型設置渲染器。 例如:

table.setDefaultRenderer(Boolean.class, new MyRenderer());

這樣,僅當value為布爾value ,才會使用渲染器。

public class MyRenderer extends DefaultTableCellRenderer{
   public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
   {
       Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

           if(value)
               c.setBackground(/*Color for booked days*/ );
           else
               c.setBackground(table.getBackground());  

       return c;
   } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM