2010-12-23 45 views
0

動機是我想查看錶格單元格中的尾部空格。例如,如果單元格包含「Foo Bar」,我希望在「Bar」之後看到空格字符。有沒有辦法改變文本背景顏色,以便我可以在JTable單元格中輕鬆查看所有字符?我期待爲整個專欄做到這一點。將文本背景顏色添加到JTable列

回答

0

嘗試修改列i中的單元格defaulttablecellrenderer中的組件。 我認爲arg4和arg5是表格的行和列,所以你也可以在這裏控制單元格的渲染器。

JTable table = new JTable(); 
    table.getColumn(i).setCellRenderer(new DefaultTableCellRenderer() { 
     @Override 
     public Component getTableCellRendererComponent(JTable arg0, Object arg1, 
       boolean arg2, boolean arg3, int arg4, int arg5) { 
      Component component = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5); 
      // modify this component here 
      // component.setForeground(Color.black.black); 
      // component.setBackground(Color.black.black); 
      return component; 
     } 
    }); 
0
final JTable table = new JTable(tableModel); 
    table.setPreferredScrollableViewportSize(TABLE_SIZE); 
    table.setFillsViewportHeight(true); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

    table.getTableHeader().addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent mouseEvent) { 
      int selectedHeader = table.convertColumnIndexToModel(table 
        .columnAtPoint(mouseEvent.getPoint())); 

      table.getColumn(table.getColumnName(selectedHeader)) 
        .setCellRenderer(new DefaultTableCellRenderer() { 
         public void setBackground(Color c) { 
          super.setBackground(Color.blue); 
         } 
        }); 
     }; 
    }); 
相關問題