2017-06-13 187 views
-1

我有一个Java表格,其中包含目录中的内容。我创建了5个组合框,将用户导航到文件夹中,然后使用选定文件夹的内容(如文件名,日期,创建者等)创建一个JTable。在表格的第6列中,我有一些字符串值,并且取决于每个值我想改变这个单元格的背景颜色。 这里是我启动JTable的最后一个comboBox。在动态JTable上更改单元格的背景颜色

+1

快速注:'抓(NullPointerException异常N){}'?你永远不应该赶上NPE,这表明你有这个代码严重错误。 –

+2

如果您需要进一步的帮助,您需要创建并发布有效的[mcve],一个作为代码格式文本发布在您的问题中的新小程序。 –

+2

我现在看到你的[以前没有回答的问题](https://stackoverflow.com/questions/44279125/add-selected-data-from-jtable-to-lst-file)中请求了一个MCVE,但是你永远不会提供了一个(就像你不在这里)。请不要忽视这些要求,如果你想得到一个体面的答案你的问题。我们要求提供这些信息是有原因的 - 这样我们才能完全理解您的代码,您的问题和您的问题。 –

回答

3

对不起,你试图抓住NPE catch(NullPointerException n){} - 是非常错误的 - 从不这样做,而是修复可能导致NPE发生的错误。

至于你的问题,你没有超载的实际方法。你的方法签名:

public Component prepareRenderer(TableCellRenderer renderer, int rowIndex) 

不符合实际的方法签名:

public Component prepareRenderer(TableCellRenderer renderer, 
          int row, 
          int column) 

按照该JTable API,你错过了第三个参数,列int参数。

这就是为什么你应该总是预置重写的方法与@Override注释。如果你这样做:

@Override // this will cause the compiler to complain that this isn't an override 
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex) 

而你在这个方法循环看起来相当可疑。我会摆脱它,因为渲染器应该仅渲染由行和列索引指定的单元格。

例如,

DefaultTableModel listModel = new DefaultTableModel(); 
JTable table1 = new JTable(listModel){ 

    @Override // don't forget this! 
    public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int columnIndex) { 
     JComponent component = (JComponent) super.prepareRenderer(renderer, i, columnIndex); 
     int lastRow = listModel.getRowCount(); 
     // this will likely set the whole row. If you only want to set only a specific cell, then 
     // you'll need to first check the columnIndex. 
     if (getValueAt(rowIndex, 6).toString().contains("yellow")) { 
      component.setBackground(Color.RED); 
     } else { 
      component.setBackground(null); // turn color back to default 
     } 
     return component; 
    } 
}; 
3

你不应该延长JTableDefaultTableCellRenderer,并设置为你的表的默认渲染:

public class TableRendererExample { 
    public static void main(String[] args) { 
     TableCellRenderer renderer = new DefaultTableCellRenderer(){ 
      @Override 
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
        boolean hasFocus, int row, int column) { 
       Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
       rendererComponent.setBackground("value2".equals(value)?Color.RED:Color.WHITE); 
       return rendererComponent; 
      } 
     }; 
     TableModel tableModel= new DefaultTableModel(10, 3){ 
      @Override 
      public Object getValueAt(int arg0, int arg1) { 
       return "value"+new Random().nextInt(4); 
      }  
     }; 
     JTable jTable = new JTable(tableModel); 
     jTable.setDefaultRenderer(Object.class, renderer); 
     JOptionPane.showMessageDialog(null, jTable); 
    } 
} 


可能的,这是一个更好的长期解决方案,因为它也允许OP为特定列设置单元格渲染器。 - 气垫船全鳝

更妙的是,你不需要知道列索引提前当你覆盖getColumnClass()TableModel

同一渲染器类中的所有列:

class DefaultTableCellRendererBackground extends DefaultTableCellRenderer { 
    private final Color highlightColor; 

    DefaultTableCellRendererBackground(Color highlightColor) { 
     this.highlightColor = highlightColor; 
    } 

    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, 
      int row, int column) { 
     Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, 
       column); 
     rendererComponent.setBackground(highlightColor); 
     return rendererComponent; 
    } 
} 

TableModel返回不同的ColumnClass,每次运行:

final class DefaultTableModelExtension extends DefaultTableModel { 
    private final List<Class<?>> columnClass; 

    DefaultTableModelExtension(int rowCount, int columnCount, List<Class<?>> columnClass) { 
     super(rowCount, columnCount); 
     this.columnClass = columnClass; 
     Collections.shuffle(this.columnClass); 
    } 

    @Override 
    public Class<?> getColumnClass(int col) { 
     return columnClass.get(col); 
    } 

    @Override 
    public Object getValueAt(int arg0, int arg1) { 
     return "value" + new Random().nextInt(4); 
    } 
} 

类型t Ø返回:

interface TagRed {} 

interface TagBlue {} 

interface TagYellow {} 

使用(运行多次...):

public class TableRendererExample { 
    public static void main(String[] args) { 
     JTable jTable = new JTable(); 
     jTable.setDefaultRenderer(TagRed.class, new DefaultTableCellRendererBackground(Color.RED)); 
     jTable.setDefaultRenderer(TagBlue.class, new DefaultTableCellRendererBackground(Color.BLUE)); 
     jTable.setDefaultRenderer(TagYellow.class, new DefaultTableCellRendererBackground(Color.YELLOW)); 

     List<Class<?>> columnClass = Arrays.asList(TagRed.class, String.class, TagBlue.class, TagRed.class, String.class, 
       TagYellow.class, TagBlue.class); 
     jTable.setModel(new DefaultTableModelExtension(10, columnClass.size(), columnClass)); 
     JOptionPane.showMessageDialog(null, jTable); 
    } 
}