1

UPDATED

In this code after mouse click table cell is still in edit mode and JRadioButton is in bad state. I have to push ESC key to escape edit mode and let renderer render correct state of the JRadioButton in cell. Does someone know what I need to implement in code to let cell editor release edit mode?

public class Test extends JFrame {

    JTable myTable = new JTable();

    public Test() {
        this.setSize(150, 150);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
                System.exit(0);
            }
        });
        setVisible(true);
        myTable.setModel(new MyModel());
        myTable.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer());
        myTable.getColumnModel().getColumn(0).setCellEditor(new MyEditor());
        myTable.setTableHeader(null);
        this.add(myTable);
    }

    public static void main(String[] args) {
        Test test = new Test();
    }

    class MyModel extends AbstractTableModel {

        Object myModelData[];

        public MyModel() {
            List list = new ArrayList();
            list.add(new MyModelData("item 1", new Boolean(true)));
            list.add(new MyModelData("item 2", new Boolean(false)));
            list.add(new MyModelData("item 3", new Boolean(true)));
            myModelData = list.toArray();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int column) {
            return "";
        }

        @Override
        public int getRowCount() {
            return myModelData.length;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return myModelData[row];
        }

        @Override
        public Class getColumnClass(int column) {
            return JRadioButton.class;
        }

        @Override
        public void setValueAt(Object value, int row, int column) {
            myModelData[row] = value;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }
    }

    class MyModelData {

        private String text;

        private Boolean status;

        public MyModelData(String text, Boolean status) {
            this.text = text;
            this.status = status;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Boolean getStatus() {
            return status;
        }

        public void setStatus(Boolean status) {
            this.status = status;
        }
    }

    class MyRenderer extends JRadioButton implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable t, Object v, boolean s, boolean f, int r, int c) {
            this.setSelected(((MyModelData) v).getStatus());
            this.setText(((MyModelData) v).getText() + " rendered");
            return this;
        }
    }

    public class MyEditor extends JRadioButton implements TableCellEditor {

        @Override
        public Component getTableCellEditorComponent(JTable t, Object v, boolean s, int r, int c) {
            ((MyModelData) v).setStatus(!((MyModelData) v).getStatus());
            this.setText(((MyModelData) v).getText() + " in edit mode");
            return this;
        }

        @Override
        public Object getCellEditorValue() {
            return null;
        }

        @Override
        public boolean isCellEditable(EventObject event) {
            return true;
        }

        @Override
        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        }

        @Override
        public boolean stopCellEditing() {
            return true;
        }

        @Override
        public void cancelCellEditing() {
        }

        @Override
        public void addCellEditorListener(CellEditorListener l) {
        }

        @Override
        public void removeCellEditorListener(CellEditorListener l) {
        }
    }
}
1ac0
  • 2,875
  • 3
  • 33
  • 47
  • 1
    What do you mean fire `ESC`, do you want to cancel editing? – alex2410 Oct 24 '14 at 10:47
  • Post [MCVE](http://stackoverflow.com/help/mcve). Also you can try to call `stopCellEditing();` – alex2410 Oct 24 '14 at 11:03
  • override override all states for editor, one of them is cancelled – mKorbel Oct 24 '14 at 12:03
  • `in fact, i dont't know. i have jtable with jradiobuttons in cells. **works fine** (radio button is switching) except the fact that actual state of the jradiobutton is rendered just after i press esc key on keyboard or click on another component.` == it doesn't works fine – mKorbel Oct 24 '14 at 12:04
  • 1
    1. [e.g. usage of `cancelCellEditing`/`fireEditingCanceled` by @aterai](http://stackoverflow.com/a/18165504/714968), 2. search for `cancelCellEditing`/`fireEditingCanceled`, 3. there isn't real answer to your question without an `SSCCE`/`MCVE` short, runnable, compilable, with hardcoded value for JTable/XxxTableModel in local variable, 4. (I'm strongly suggest to) use `JComboBox` as `DefaultCellEditor` and `JRadioButton` as `XxxTableCellRenderer` – mKorbel Oct 24 '14 at 12:19
  • question in this form should be closed as too_broad, off-topics because - too lazy to search :-) – mKorbel Oct 24 '14 at 12:23
  • 1
    search for [Java + Swing + JTable + JRadioButton](http://stackoverflow.com/search?q=Java+%2B+Swing+%2B+JTable+%2B+JRadioButton) here, maybe I'm post something about here, including highlighter on rollover – mKorbel Oct 24 '14 at 12:45
  • again without an SSCCE/MCVE short, runnable, compilable, with hardcoded value for JTable/XxxTableModel in local variable isn't answerable here, again to use JComboBox as DefaultCellEditor and JRadioButton as XxxTableCellRenderer in the case that you want to simulating JRadioButton in ButtonGroup (only one JRadioButton can be selected) – mKorbel Oct 24 '14 at 12:47
  • Maybe someone will have mercy:-), here are a few users with this nature that ignoring all good rules here – mKorbel Oct 24 '14 at 12:49
  • please to add to your question about fact that ESC key `is`/`can be`/`will be pressed` (== Key Bindings) or only about how to do that programatically without any KeyEvents – mKorbel Oct 24 '14 at 12:51
  • i have column with jradiobuttons and just one should be selected == JComboBox as TableCellEditor(will be updated with lint to SSCCE/MCVE) , (to avoid) but is possible to create JRadioButtons in ButtonModel but use only code where is stored boolean value in XxxTableModel for JTables view – mKorbel Oct 24 '14 at 12:54
  • [JRadioButton or JComboBox as TableCellEditor - // comment out the two preceding lines](http://stackoverflow.com/a/10616768/714968) – mKorbel Oct 24 '14 at 12:57

0 Answers0