2012-04-18 59 views
3

我有一個JTable,爲此我提供了一個自定義的TableCellRenderer,它根據數值(< 0,0,> 0)以紅色/灰色/綠色對數字單元格進行着色。自定義TableCellRenderer被Look&Feel忽略

然而,當我使用雨雲大號&樓的label.setForeground()方法被忽略了:調用label.getForeground()當我可以看到數字有正確的顏色,例如紅,但在屏幕上談到黑色。如果我刪除L & F它工作正常。

有沒有辦法輕輕要求L & F接受使用我的顏色爲該細胞?

ps:我知道javadoc of setForeground()很清楚L & F可能會忽略這個調用,所以我正在尋找解決方法。

+1

最後我聽說,Nimbus被認爲是非常有問題的。 – 2012-04-18 16:45:42

+0

@AndrewThompson你能否闡述/提供參考? – assylias 2012-04-18 16:54:02

+0

請嘗試我的結果和kleopatra對[此線程](http://stackoverflow.com/q/8624040/418556)的評論。 – 2012-04-18 16:58:12

回答

3

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.table.*; 

public class TablePrepareRenderer extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JTable table; 

    public TablePrepareRenderer() { 
     Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"}; 
     Object[][] data = { 
      {"Buy", "IBM", new Integer(1000), new Double(80.50), false}, 
      {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true}, 
      {"Sell", "Apple", new Integer(3000), new Double(7.35), true}, 
      {"Buy", "Nortel", new Integer(4000), new Double(20.00), false} 
     }; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames) { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public Class getColumnClass(int column) { 
       return getValueAt(0, column).getClass(); 
      } 
      /*@Override 
      public Class getColumnClass(int column) { 
      switch (column) { 
      case 0: 
      return String.class; 
      case 1: 
      return String.class; 
      case 2: 
      return Integer.class; 
      case 3: 
      return Double.class; 
      default: 
      return Boolean.class; 
      } 
      }*/ 
     }; 
     table = new JTable(model) { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
       Component c = super.prepareRenderer(renderer, row, column); 
       int firstRow = 0; 
       int lastRow = table.getRowCount() - 1; 
       if (row == lastRow) { 
        ((JComponent) c).setBackground(Color.red); 
       } else if (row == firstRow) { 
        ((JComponent) c).setBackground(Color.blue); 
       } else { 
        ((JComponent) c).setBackground(table.getBackground()); 
       } 
       return c; 
      } 
     }; 
     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 
    } 
    /*private static String[] suffix = new String[]{"", "k", "m", "b", "t"}; 
    private static int MAX_LENGTH = 4; 

    private static String format(double number) { 
    String r = new DecimalFormat("##0E0").format(number); 
    r = r.replaceAll("E[0-9]", suffix[Character.getNumericValue(r.charAt(r.length() - 1))/3]); 
    return r.length() > MAX_LENGTH ? r.replaceAll("\\.[0-9]+", "") : r; 
    }*/ 

    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     } catch (Exception fail) { 
     } 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       TablePrepareRenderer frame = new TablePrepareRenderer(); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
     /*long[] numbers = new long[]{1000, 5821, 10500, 101800, 2000000, 7800000, 92150000, 123200000, 99999900}; 
     for (long number : numbers) { 
     System.out.println(number + " = " + format(number)); 
     }*/ 
    } 
} 
+0

這不是問題,但它幫助我解決了問題!看到我的答案。 – assylias 2012-04-18 17:49:59

2

好,感謝mKorbel's answer,我意識到,我是用ColorUIResource而不是Color。換句話說:

label.setForeground(Color.red); //works 
label.setForeground(new ColorUIResource(Color.red)); //doesn't work 

我不知道我理解爲什麼一個作品,而不是其他(ColorColorUIResource直接超類),但問題就迎刃而解了。

+0

+ +1爲答案,但錯誤的方向,爲更安全的方式,你必須重寫[NimbusDefault正確](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html#primary),否則你可能是下一個討論Buggy Nimbus的人,注意我同意Nimbus不好,因爲開發進度在第一季度結束了:-) – mKorbel 2012-04-18 18:06:44

相關問題