0

I am new to working with JTables and having trouble with getting my custom JTable editor to work properly.

I have a number of custom panels with lists and buttons. To renderder them in a cell I am using a custom PanelCellRenderer that has various constructors for each type of the panel.

To make the buttons clickable I have created this simple PanelCellEditor that extends DefaultCellEditor. To access the data stored within cells at the time of editting I pass the reference to the PanelCellRenderer.

The problem I am having is that when I select the cell (by clicking at it), from displaying the list with the button, the cell selected becomes completely blank. When the cell gets deselected the list with data and the button reappear again. Any advice on this will be helpful. Thanks.

public class PanelCellEditor extends DefaultCellEditor {

    private PanelCellRenderer pcr;
    private Object value;

    public PanelCellEditor(final PanelCellRenderer pcr) {
        super(new JCheckBox());
        this.pcr = pcr;
        this.pcr.setOpaque(true);

        if (pcr.firstPanel != null) {

            pcr.firstPanel.Button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //do something
                    fireEditingStopped();
                }
            });

            pcr.firstPanel.List.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    String value = (String) ((javax.swing.JList) e.getSource()).getSelectedValue();
                    //do something
                    fireEditingStopped();
                }
            });
        }
        else if (pcr.secondPanel != null) {

            pcr.secondPanel.Button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //do something
                    fireEditingStopped();
                }
            });

            pcr.secondPanel.List.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    String value = (String) ((javax.swing.JList) e.getSource()).getSelectedValue();
                    //do something
                    fireEditingStopped();
                }
            });
        }
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {

        //// if I comment this whole bit ////
        if (isSelected) {
            pcr.setForeground(table.getSelectionForeground());
            pcr.setBackground(table.getSelectionBackground());
        } else {
            pcr.setForeground(table.getForeground());
            pcr.setBackground(table.getBackground());
        }

        if (pcr.firstPanel != null)
            pcr.firstPanel.list.setListData((String[])value);
        else if (pcr.secondPanel != null) {
            pcr.secondPanel.list.setListData((String[])value);
        }
        //////// nothing changes /////////

        this.value = value;
        return pcr;
    }

    public Object getCellEditorValue() {
        return value;
    }

    public boolean stopCellEditing() {
        return super.stopCellEditing();
    }

    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}
4

1 回答 1

0

您可以将 JTable.getTableCellEditor 跟踪到您的对象中。您是否真的使用 Jtable 编辑的值注册了您的编辑器?

于 2009-09-01T11:19:09.203 回答