2012-02-21 168 views
3

我有一個帶有自定義TableCellRenderer的JTable。在外觀更新後刷新JTable

public class DateCellRenderer extends DefaultTableCellRenderer { 

    private static final long serialVersionUID = 58L; 

    public DateCellRenderer() { 
     super(); 
     setHorizontalAlignment(CENTER);  
     setOpaque(true); 
    } 
    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {   
     if (value instanceof Date) { 
      String date = new SimpleDateFormat("dd-MM-yyyy").format((Date) value); 
      setText(date); 
     } 
     return this; 
    } 
} 

另外在我的應用程序中,我有一個下拉菜單,我可以改變外觀和感覺。此下拉菜單位於父框架中,表格位於對話框中。當對話框打開時,父框架不可訪問。所以要改變外觀,我必須先關閉對話框。

現在在特定的皮膚中,如果表格由一些數據填充,並且我從父框架更改外觀並再次打開對話框,那麼添加了TableCellRenderer的列保持舊外觀和感覺。它不會更新,而其他列會以新的外觀呈現自己。

我無法找到問題及其解決方案。任何幫助是可觀的。

注:外觀和感覺的應用程序的更新是由下面的代碼片段提前做出

javax.swing.UIManager.setLookAndFeel(uiProperties.getThemeModel().getThemeClass()); 
ComponentFactory.getLibraryFrame().getRootPane().updateUI(); 
for (int i = 0; i < Frame.getWindows().length; i++) { 
    SwingUtilities.updateComponentTreeUI(Frame.getWindows()[i]); 
} 
for (int i = 0; i < Frame.getFrames().length; i++) { 
    SwingUtilities.updateComponentTreeUI(Frame.getFrames()[i]); 
} 

感謝。

在高保真主題第一選自: enter image description here

然後我改變主題,以快速,和第二列 「發行日期」 未更新它的UI: enter image description here

JTable的是:

public class MovieSearchResultTable extends BaseTable { 

    private static final long serialVersionUID = 45L; 

    public MovieSearchResultTable(TableModel tableModel) { 
     super(tableModel); 
     LibraryLogger.initMessage(getClass().getSimpleName()); 
    } 

    @Override 
    public void initialize() { 
     setFillsViewportHeight(true); 
     setAutoResizeMode(AUTO_RESIZE_OFF); 
     getColumnModel().getColumn(1).setCellRenderer(new DateCellRenderer());//if I comment out this line then no problem. but without CellRenderer how could I format a Date, if I use formatted String instead of Date, then the column will not sort!! 
    } 

    @Override 
    public boolean getScrollableTracksViewportWidth() { 
     return getPreferredSize().getWidth() < getParent().getWidth(); 
    } 
} 
+0

從外觀和感覺 – mKorbel 2012-02-21 17:45:39

+0

我使用jtatoo - http://www.jtattoo.net/外觀和感覺。它的外觀和感覺像HiFi,Fast,Acryl,Aero等。該下拉列表包含這些主題列表。 – 2012-02-21 17:54:25

+0

現在是你的問題清楚,但沒有代碼生成一些問題.....或圖片是你的問題相當un_answerable,你只是試圖改變JTatoo○s主題飛,nothins其他? – mKorbel 2012-02-21 17:58:56

回答

2

的解決方案,你需要重寫public Component prepareRenderer(TableCellRenderer renderer, int row, int column)

這裏該類:

public class MovieSearchResultTable extends BaseTable { 

    private static final long serialVersionUID = 45L; 

    private int rolloverRowIndex = -1; 

    public MovieSearchResultTable(TableModel tableModel) { 
     super(tableModel); 
     LibraryLogger.initMessage(getClass().getSimpleName()); 
    } 

    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
     Component component = super.prepareRenderer(renderer, row, column); 
     Color foreground = getForeground(); 
     Color background = getBackground(); 
     if (isRowSelected(row)) { 
      foreground = getSelectionForeground(); 
      background = getSelectionBackground(); 
     } 
     else if (row == rolloverRowIndex) { 
      foreground = getSelectionForeground(); 
      background = ColorHelper.brighter(getSelectionBackground(), 40); 
     } 
     else if (row % 2 == 0) { 
      background = ColorHelper.brighter(getParent().getBackground(), 20); 
     } 
     component.setForeground(foreground); 
     component.setBackground(background); 
     return component; 
    } 

    private class RolloverListener extends MouseInputAdapter { 

     public void mouseExited(MouseEvent e) { 
      rolloverRowIndex = -1; 
      repaint(); 
     } 

     public void mouseMoved(MouseEvent e) { 
      int row = rowAtPoint(e.getPoint()); 
      if (row != rolloverRowIndex) { 
       rolloverRowIndex = row; 
       repaint(); 
      } 
     } 
    } 

    @Override 
    public void initialize() { 
     setFillsViewportHeight(true); 
     setAutoResizeMode(AUTO_RESIZE_OFF); 
     TableColumnModel tableColumnModel = getColumnModel(); 
     for(ComponentConstant.ColumnName columnName : ComponentConstant.Column.MOVIE_SEARCH_RESULT_TABLE) { 
      int order = columnName.getOrder(); 
      TableColumn tableColumn = tableColumnModel.getColumn(order); 
      if(order == 0) { 
       continue; 
      } 

      tableColumn.setCellRenderer(RendererFactory.getMovieSearchResultTableCellRenderer()); 
     } 
     RolloverListener listener = new RolloverListener(); 
     addMouseMotionListener(listener); 
     addMouseListener(listener); 
    } 

    @Override 
    public boolean getScrollableTracksViewportWidth() { 
     return getPreferredSize().getWidth() < getParent().getWidth(); 
    } 
} 

謝謝。

+0

我覺得太複雜了,請不要準備好囉嗦+1,是另一個問題,JTable,TableModel如何工作 – mKorbel 2012-02-21 22:12:20

3

我認爲這不太好L & F,JTable看起來像...好吧,但其他複合JComponents不是......,不知道我沒有浪費我的時間,我樂AVING測試,也許是有

有非常簡單的方法有關自己的論壇或文檔或BugParades描述的東西,但是從你的問題沒什麼,你可以隨時檢查

1)去Insubstantial

2)下載代碼源,

3)導入的所有類的IDE(2-15分鐘取決於PC硬件)

4)搜索文件夾的測試,有Check.java,

5)運行,並嘗試在JMenu的一切外觀和感覺,這需要前下載API的每一個Custom Java Swing Look and Feels

enter image description here

+0

我已經下載了Insubstantial-insubstantial-insubstantial_7.0-21-gefb2aaf,但找不到Check.java。 :( – 2012-02-21 18:32:35

+0

eeeerggth :-)我可以在https://github.com/Insubstantial/insubstantial/tree/master/substance/src/test/java/test – mKorbel 2012-02-21 18:36:25

+0

看到這一個我花了2周半拿起我最喜歡的L&F(原來的失眠症) – mKorbel 2012-02-21 18:38:56