2012-10-12 222 views
2

有人能給我一個如何獲得JTable中特定單元格的背景顏色的例子嗎?我無法找到如何做到這一點的例子。大量關於如何獲取單元格中的值的示例,而不是單元格的背景顏色。JTable單元格顏色

+0

閱讀答案後,我注意到我誤解了你的問題;-)即使是現在,這仍然是一個不理想的問題:沒有上下文和更多細節(f.i.爲什麼,你想要達到什麼樣的效果,你究竟在哪裏/如何獲得顏色......)很難正確回答...... – kleopatra

回答

1

有人能給我一個如何獲得JTable中特定單元格的背景顏色的例子嗎?我無法找到如何做到這一點的例子。

大量關於獲取單元格中值的示例,但不是單元格的背景色。

  • 我無法抗拒,請在這個論壇或者

  • 我希望幫助你....

1

要在單元格0, 0得到JTable顏色你可以得到細胞成分的背景顏色:

TableCellRenderer cellRenderer = table.getCellRenderer(0, 0); 
Component rendererComponent = cellRenderer.getTableCellRendererComponent(table, null, false, true, 0, 0); 
Color cellColor = rendererComponent.getBackground(); 
+0

如果使用帶有一些棘手的基於值的邏輯的自定義渲染器,或者使用自定義渲染器或行被選中。 –

+1

它會工作,如果你通過正確的行/列到table.getCellRenderer – MadProgrammer

+0

錯誤...這將不會讓你配置後的顏色_table_,請參閱[我的評論](http://stackoverflow.com/a/ 12862835/203657) – kleopatra

7

它應該是類似以下內容(根據所有評論固定):

重要提示:使用table.prepareRenderer(...)讓JTable中爲你做

public Color getTableCellBackground(JTable table, int row, int col) { 
    TableCellRenderer renderer = table.getCellRenderer(row, col); 
    Component component = table.prepareRenderer(renderer, row, col); 
    return component.getBackground(); 
} 

完全各項工作演示:

public class TableRenderDemo extends JPanel { 

    public TableRenderDemo() { 
     super(new GridLayout(1, 0)); 

     final JTable table = new JTable(new MyTableModel()); 
     table.setPreferredScrollableViewportSize(new Dimension(600, 200)); 
     table.setFillsViewportHeight(true); 
     table.setDefaultRenderer(Object.class, new MyRenderer()); 

     table.addMouseListener(new MouseAdapter() { 

      public void mouseClicked(MouseEvent e) { 
       int row = table.getSelectedRow(); 
       int col = table.getSelectedColumn(); 

       JOptionPane.showInternalMessageDialog(TableRenderDemo.this, 
         "Color: " + getTableCellBackground(table, row, col)); 

       System.out.println("Color: " + getTableCellBackground(table, row, col)); 
      } 
     }); 

     JScrollPane scrollPane = new JScrollPane(table); 
     add(scrollPane); 
    } 

    public Color getTableCellBackground(JTable table, int row, int col) { 
     TableCellRenderer renderer = table.getCellRenderer(row, col); 
     Component component = table.prepareRenderer(renderer, row, col);  
     return component.getBackground(); 
    } 

    class MyRenderer implements TableCellRenderer { 

     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
       boolean hasFocus, int row, int column) { 
      JTextField editor = new JTextField(); 
      if (value != null) { 
       editor.setText(value.toString()); 
      } 
      editor.setBackground((row % 2 == 0) ? Color.white : Color.BLUE); 
      return editor; 
     } 
    } 

    class MyTableModel extends AbstractTableModel { 

     private String[] columnNames = {"First Name", 
      "Last Name", 
      "Sport", 
      "# of Years", 
      "Vegetarian"}; 
     private Object[][] data = { 
      {"Kathy", "Smith", 
       "Snowboarding", new Integer(5), new Boolean(false)}, 
      {"John", "Doe", 
       "Rowing", new Integer(3), new Boolean(true)}, 
      {"Sue", "Black", 
       "Knitting", new Integer(2), new Boolean(false)}, 
      {"Jane", "White", 
       "Speed reading", new Integer(20), new Boolean(true)}, 
      {"Joe", "Brown", 
       "Pool", new Integer(10), new Boolean(false)} 
     }; 
     public final Object[] longValues = {"Jane", "Kathy", 
      "None of the above", 
      new Integer(20), Boolean.TRUE}; 

     public int getColumnCount() { 
      return columnNames.length; 
     } 

     public int getRowCount() { 
      return data.length; 
     } 

     public String getColumnName(int col) { 
      return columnNames[col]; 
     } 

     public Object getValueAt(int row, int col) { 
      return data[row][col]; 
     } 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("TableRenderDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     TableRenderDemo newContentPane = new TableRenderDemo(); 
     newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

我試過這個例子,我遵循邏輯,但由於某種原因,我得到255,255,255爲每個單元格,但我有備用行着色,所以我不知道這裏發生了什麼。 – user1154644

+0

user1154644您是否搜索過,有XxxCellRenderer和PrepareRenderer,並且此代碼沒有設置任何Color,也沒有設置Cell BackGround ...,只有 – mKorbel

+2

的骨架如何應用行突出顯示? – MadProgrammer

0

使用的TableCellRenderer

jTable1 = new javax.swing.JTable(6,6){ 
public Component prepareRenderer(
    TableCellRenderer renderer, int row, int column) 
{ 

    Component c = super.prepareRenderer(renderer, row, column); 
    if(column==2 && row==4) 
    { 
     Color color= c.getBackground();// use setBackground to set color and get background to get background of a particular cell 
     System.out.println("Color of row=0 and column=0 is "+color); 

    } 

    else 
    { 
     c.setBackground(Color.GREEN); 
     setShowGrid(true); 
    } 
    return c; 

} 
}; 
+0

沒有回答這個問題(這不是太清楚,承認:-) – kleopatra

+0

您是否注意到System.out.println(); statment執行無限?我相信prepareRenderer方法以某種方式遞歸調用。即使您使用「純」JTable類的對象而不是定製版本的對象,這種預期行爲是否也會發生? – PrashanD

+0

@Prashan我回答了很久很久,我明白答案並不完美。我現在不接觸揮杆。是的,我知道答案並不完美。我在鞦韆上失去了聯繫,現在我在android上編程。 – Raghunandan