2017-06-13 87 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がリクエストされたことを確認しましたが、あなたがここにいないのと同じように、1つを提供しました。あなたの質問にまともな答えが必要な場合は、これらの要求を無視しないでください。私たちはあなたのコード、あなたの問題、あなたの質問を完全に理解できるように、この情報を理由で要求しています。 –

答えて

3

残念ですが、NPE catch(NullPointerException n){}をキャッチしようとすると非常に間違っています。これを行うことはありませんが、代わりにNPEの原因となるバグを修正してください。

あなたの問題については、実際のメソッドがオーバーロードされていません。あなたのメソッドシグネチャ:あなたは3番目のパラメータ、列int型のパラメータが欠落している、JTable APIあたりとして

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

public Component prepareRenderer(TableCellRenderer renderer, int rowIndex) 

は、実際のメソッドのシグネチャと一致していません。

常には、@Override注釈でオーバーライドされたメソッドを追加する必要があります。

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

この方法のforループはかなり疑わしいです。レンダラーは行と列のインデックスで指定されたセルだけをレンダリングする必要があるので、私はそれを取り除きます。

例えば、

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

あなたはJTableが、DefaultTableCellRendererを拡張して、表のデフォルトのレンダラとしてその設定しないでください。

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; 
    } 
} 

異なるColumnClassesを返すTableModelは、それぞれ実行されます。

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 O返される:

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); 
    } 
} 
関連する問題