2014-10-09 159 views
0

IM使用JXTable並試圖基於價值以色行,但顯示空(但渲染運行,因爲它顯示在控制檯中syso)彩色細胞基於價值

public class MyCellRenderer extends JLabel implements TableCellRenderer { 
     public Component getTableCellRendererComponent(JTable table, Object value, 
       boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) { 
      SimpleTableModel mymodel = (SimpleTableModel) jxtableListar.getModel(); 
      ModelProtocolo actualModel= (ModelProtocolo) mymodel.getProtocolo(rowIndex) ; 

      if(actualModel.getValue() > 0) { 
       System.out.println("Yep the Render is working"); 

      } 
         return this; 
     } 
    } 


jxtableListar.setDefaultRenderer(Object.class, new MyCellRenderer()); 

我沒發現任何有用的「教程」如何使用JXTable渲染器,因爲他們談論有關Hightlight的一些信息,但所有教程都是「信息不足」要學習

有沒有什麼好的方法/教程來着色JXTable行的基礎上細胞的價值?

回答

0

您的渲染器正在返回尚未設置任何內容的JLabel(本身)。取而代之的是,延長DefaultTableCellRenderer

public class MyCellRenderer extends DefaultTableCellRenderer { 
    @override 
    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) { 
     SimpleTableModel mymodel = (SimpleTableModel) jxtableListar.getModel(); 
     ModelProtocolo actualModel= (ModelProtocolo) mymodel.getProtocolo(rowIndex) ; 

     JLabel label = (JLabel) super.getTableCellRendererComponent(/* pass in all params */); 
     label.setText(/*whatever the text should be*/); 
     label.setBackground(/*whatever the color should be*/); 
     return label; 
    } 
} 
+0

的工作,但我失去了在JTable – user2582318 2014-10-09 22:06:30

+0

「點擊」和「選擇」的能力沒關係,我所做的,如果(isSelected),感謝=) – user2582318 2014-10-09 22:11:00