【解決方法】JTable のセルにカスタム コンポーネントを挿入しますか?


JComboBox と JCheckBox を挿入する例をいくつか見てきましたが、独自のコンポーネントを挿入すると問題が発生します。コンポーネントのチェックボックスとボタンをクリックすることはできますが、コンポーネントのすべてのセルに同じハッシュ コードが含まれています。テーブル。 この場合、すべてのリンクが同じオブジェクト (同じコンポーネント) を指しているため、すべての JCheckBox (選択されたプロパティ) の状態は保存されません。 この問題を解決するにはどうすればよいでしょうか?

レンダラーとエディターを追加する方法は次のとおりです。

ミドル
table.getColumn(0).setCellRenderer(new MyCellComponentRenderer());

table.getColumn(0).setCellEditor(new MyCellComponentEditor(new JCheckBox()));

私のレンダラークラスは次のとおりです。

C#
public class MyCellComponentRenderer extends MyComponent implements TableCellRenderer {

      public MyCellComponentRenderer() {
       // setOpaque(true);
      }

      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
          this.setForeground(table.getSelectionForeground());
          setBackground(table.getSelectionBackground());
        } else{
          setForeground(table.getForeground());
          setBackground(Color.white);
        }


        return this;
      }
    }

編集者:

MSIL
public class MyCellComponentEditor extends DefaultCellEditor {

    private MyComponent myComponent;

    private boolean isChecked;
    private boolean isPushed;

    public MyCellComponentEditor(JCheckBox checkBox) {
        super(checkBox);

        myComponent = new MyComponent();
        myComponent.getCheckBox().setOpaque(true);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column)
    {
        if (isSelected) {
            myComponent.setForeground(table.getSelectionForeground());
            myComponent.setBackground(table.getSelectionBackground());
        } else {
            myComponent.setForeground(table.getForeground());
            myComponent.setBackground(Color.orange);
        }

        return myComponent;
    }

    @Override
    public Object getCellEditorValue() {
        if (isPushed) {
        }
        isPushed = false;
        return new String("");
    }
    @Override
    public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
    }
    @Override
    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}

成分:

MSIL
public class MyComponent extends JPanel
{
    private JButton button;
    private JCheckBox checkBox;
    private JLabel label1;
    private JLabel label2;

    private boolean state;

    public MyComponent() {

        button = new JButton("A");
        checkBox = new JCheckBox("B");
        label1 = new JLabel("1");
        label2 = new JLabel("2");

        this.add(button);
        this.add(checkBox);
        this.add(label1);
        this.add(label2);

        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(button,button.hashCode());
            }
        });
    }

    public JCheckBox getCheckBox()
    {
        return checkBox;
    }

    public void setCheckBox(JCheckBox checkBox)
    {
        this.checkBox = checkBox;
    }

    public void setState(boolean state)
    {
        this.state = state;
        checkBox.setSelected(state);
    }

}

解決策 1

カスタムの JCheckBox を使用する必要があります。 その JCheckBox には ID を付けることができ、それによって識別できます。

public class CustomCheckBox extends JCheckBox {
    private final String strID;

    public CustomCheckBox(final String strID){
        super();
        this.strID = strID;
    }

    public String getID(){
        return strID;
    }
}

ID は任意のものにすることができます。行または行の内容を定義するものなどです。
また、カスタム スタイル、名前付け (国際化!) などを使用してカスタム コンポーネントを拡張することもできます。 したがって、コンポーネントは、基本的な実装に付属しているものよりも、必要なものそのものです。

TableCellEditor に次のように挿入します。

C#
public Component getTableCellEditorComponent(JTable table, Object value,
              boolean isSelected, int row, int column) {
        switch (column) {
            case 0:
                return new CustomCheckBox("Row1");

            //....
            default: return null;
        }
    }

よろしく
トルステン

解決策 2

問題は解決されました。 エディター クラスの public boolean stopCellEditing() メソッドに、データベースに新しい値 (私の場合はブール値) を設定するコードを追加するだけです。

解決策 3

Java NetBeansコードのチェックボックスでブール値がtrueであることを取得する方法

コメント

タイトルとURLをコピーしました