2009-04-02

Java : JTable の特定の行の文字色を変えたい - 1


JTable の特定の行の文字色を変えたい

詳細
  • 各行の 6 カラム目(項目 F の列)の値が“red”の場合、その行の文字色を赤色にしたい



import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Sample extends JFrame {

  private String[] columnNames = { "A", "B", "C", "D", "E", "F" };

  private String[][] sampleData = {{ "a", "1", "い", "", "1234567890", "red"}
                                      , { "b", "2", "ろ", "", "4444444444", "0"}
                                      , { "c", "3", "は", "", "wwwwwwwwww", "0"}
                                      , { "d", "4", "に", "", "IIIIIIIIII", "red"}
                                  };

  public Sample() {
      JTable table = new JTable(sampleData, columnNames) {
          @Override
          public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
              Component c = super.prepareRenderer(tcr, row, column);
              if ("red".equals(getValueAt(row, 5))) {
                  c.setForeground(Color.RED);
              } else {
                  c.setForeground(getForeground());
              }
              return c;
          }
    
      };
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
      JScrollPane sp = new JScrollPane(table);
      sp.setPreferredSize(new Dimension(500, 200));
      JPanel panel = new JPanel();
      panel.add(sp);
      getContentPane().add(panel);
  }

  public static void main(String[] args) {
      Sample frame = new Sample();
      frame.setTitle("sample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setBounds(60, 60, 600, 600);
      frame.setVisible(true);
  }
}

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class Sample extends JFrame {

  private String[] columnNames = { "A", "B", "C", "D", "E", "F" };

  private String[][] sampleData = {{ "a", "1", "い", "", "1234567890", "red"}
                                      , { "b", "2", "ろ", "", "4444444444", "0"}
                                      , { "c", "3", "は", "", "wwwwwwwwww", "0"}
                                      , { "d", "4", "に", "", "IIIIIIIIII", "red"}
                                  };

  public Sample() {
      JTable table = new JTable(sampleData, columnNames);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
      table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
          @Override
          public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
              if ("red".equals(table.getValueAt(row, 5))) {
                  setForeground(Color.RED);
              } else {
                  setForeground(table.getForeground());
              }
              return this;
          }
      });
      JScrollPane sp = new JScrollPane(table);
      sp.setPreferredSize(new Dimension(500, 200));
      JPanel panel = new JPanel();
      panel.add(sp);
      getContentPane().add(panel);
  }

  public static void main(String[] args) {
      Sample frame = new Sample();
      frame.setTitle("sample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setBounds(60, 60, 600, 600);
      frame.setVisible(true);
  }
}



微妙だ。
無名クラスでやるより、javax.swing.JTable クラスか javax.swing.table.DefaultTableCellRenderer クラスの拡張クラスを用意した方がよいのかもしれない。(多分、前者)。



参考

0 件のコメント:

コメントを投稿