在JTable的单元格中插入自定义组件? [英] Inserting a custom component in cells of JTable ?

查看:153
本文介绍了在JTable的单元格中插入自定义组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些插入JComboBox和JCheckBox的示例,但是当我插入自己的组件时,出现了一个问题:我可以单击组件上的复选框和按钮,但是该组件具有相同的哈希码表格中的所有单元格.在这种情况下,由于所有链接都指向相同的对象(相同的组件),因此不会保存每个JCheckBox(选定的属性)的状态.如何解决这个问题?

在这里,我如何添加渲染器和编辑器:

I''ve already seen some examples with inserting of JComboBox and JCheckBox, but when I insert my own component there is a problem: I can click on the checkbox and button on my component but the component has the same hash-code for all cells of the table. In this case the state of every JCheckBox (selected property) is not saved because all links point to the same object (the same component). How to solve this problem ?

Here how I add a renderer and editor:

table.getColumn(0).setCellRenderer(new MyCellComponentRenderer());

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



这是我的渲染器类:



Here is my renderer class:

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







Editor:

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



组件:



Component:

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

}

推荐答案

您应该使用自定义的JCheckBox.该JCheckBox可以具有一个ID,可以在其上进行标识:

You should use a custom JCheckBox. That JCheckBox can have an ID, on which you can identify it:

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中看到的那样进行插入:



The ID can be whatever you want it - perhaps something defining the row or the content of the row...
Also can you extend the custom component with custom styles, naming (internationalization!) and much more. So the Component is more what you need it to be than what comes with the basic implementation.

insert as seen here in your TableCellEditor:

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

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




问候
Torsten




regards
Torsten


问题已解决.我只需要在方法中放入代码,即可在数据库中设置一个新值(在我的情况下为布尔值):编辑器类中的public boolean stopCellEditing()
The problem was resolved. I just need to put a code which sets a new value into the database (in my case it''s a boolean value) in the method: public boolean stopCellEditing() in the editor class


这篇关于在JTable的单元格中插入自定义组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆