0

JTable行自体の値に応じて、のいくつかの行を強調表示する機能が必要です。たとえば、既存の数量<並べ替えレベルの場合、その行はで強調表示されますJTable

行が選択されたときに機能するテーブルメソッドがあることは知っていtblItems.setSelectionBackground(Color.yellow);ますが、選択された行に依存せずに別の色で表示される同様のメソッドはありますか?

public class MyTableCellRenderer implements TableCellRenderer {  
    @Override  
    public Component getTableCellRendererComponent
(JTable table, Object value, 
boolean isSelected, boolean hasFocus, int row, int column) {

        Object ob=table.getValueAt(row, column);
        if(ob.toString().equals("yes")){
            //need to colour the entire row
        }
        return 
    }

}
4

3 に答える 3

2

私の答えを使用して、セルの色を変更できます。同じ手法を使用して、行の各セルに適用できます。

これも例ですprepareRenderer

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class TableWithPrepareRendererExample extends JFrame {
  ColorName colors[] = { new ColorName("Red"), new ColorName("Green"), new ColorName("Blue"),
    new ColorName("Black"), new ColorName("White") };

  public TableWithPrepareRendererExample() {
    super("Table With prepareRenderer Example");
    setSize(500, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new AbstractTableModel() {
      ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0],
        colors[1], colors[2], colors[3], colors[4] };


      public int getColumnCount() {
        return 3;
      }

      public int getRowCount() {
        return 10;
      }

      public Object getValueAt(int r, int c) {
        switch (c) {
          case 0:
            return (r + 1) + ".";
          case 1:
            return "Some pithy quote #" + r;
          case 2:
            return data[r];
        }
        return "Bad Column";
      }

      public Class getColumnClass(int c) {
        if (c == 2)
          return ColorName.class;
        return String.class;
      }

      public boolean isCellEditable(int r, int c) {
        return c == 2;
      }

      public void setValueAt(Object value, int r, int c) {
        data[r] = (ColorName) value;
      }

    }) {
      public Component prepareRenderer(TableCellRenderer renderer,
                                       int rowIndex, int vColIndex) {
        Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
        Object value = getValueAt(rowIndex, vColIndex);
        if (value.toString().equals("Red"))
          c.setBackground(Color.RED);
        else {
          if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
            c.setBackground(Color.YELLOW);
          } else {
            // If not shaded, match the table's background
            c.setBackground(getBackground());
          }
        }
        return c;
      }
    };

    JComboBox combo = new JComboBox(colors);
    table.setDefaultEditor(ColorName.class, new DefaultCellEditor(combo));
    table.setRowHeight(20);
    getContentPane().add(new JScrollPane(table));
  }

  public static void main(String args[]) {
    TableWithPrepareRendererExample ex = new TableWithPrepareRendererExample();
    ex.setVisible(true);
  }

  public class ColorName {
    String cname;

    public ColorName(String name) {
      cname = name;
    }

    public String toString() {
      return cname;
    }
  }

}
于 2012-10-27T10:46:38.150 に答える
1

私が行う方法はJTable、テーブルセルの描画に使用するコードを変更することです...

独自の を実装しますTableCellRenderergetTableCellRendererComponent()メソッドで、必要に応じて qty < reorder level かどうかを確認し、そうである場合は、このメソッドから返す前に をsetBackgroundColor()呼び出します。Componenta を作成する最も簡単な方法TableCellRendererは、既存のTableCellRendererused byのソース コードをJTable確認することです。メソッドは 1 つしかなく、基本的にはそれをコピーしてコードを少し追加するだけで、カラー チェックを実行できます。を使用して設定しTableCellRendererますJTablesetDefaultRenderer()

コメントで @mKorbel が述べたように、JTable prepareRenderer()メソッドを上書きすることで同様のことを達成することもできます。

特に、既存の Java ソース コードを調べてその方法を確認すれば、それほど難しいことではありません。

于 2012-10-27T11:26:28.447 に答える