Java JTable - Combobox + Checkbox - Cellrenderer + Editor

1.3k views Asked by At

i got a Problem.

I have a JTable, containing descriptors in the first column, and then different types of JComponents in the second column (JTextField as standard, JCheckBox for Boolean and JCombobox for Integers) I created my own Cellrenderer, to do so, but when i try to change the value of (to edit) f.e. the JCheckBox, the CheckBox dissapears, and there is a normal TextField.

So I tried to write my own CellEditor, but it doesnt really work. Might you guys help me please?

Thanks Developer_X

                String[] columnNames =
                {"", ""};

                Object[][] data =
                {
                        { "A", ""},
                        { "B", ""},
                        { "C", new Integer(0)     },
                        { "D", new Boolean(true)  },
                        { "E", "A.B.C" },
                        { "F", "Rhababer"  }
                };

                JTable table = new JTable(data, columnNames)
                {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public boolean isCellEditable(int row, int column)
                    {
                        if(column==0)
                            return false;
                        else
                            return true;
                    }
                };
                table.setRowSelectionAllowed(false);
                table.setColumnSelectionAllowed(false);
                table.setCellSelectionEnabled(false);
                table.setPreferredScrollableViewportSize(table.getPreferredSize());
                table.setDefaultRenderer( Object.class,  new FormalDataCellRenderer(table.getDefaultRenderer(Object.class)));
                table.setDefaultRenderer( Boolean.class, new FormalDataCellRenderer(table.getDefaultRenderer(Boolean.class)));
                table.setDefaultRenderer( Integer.class, new FormalDataCellRenderer(table.getDefaultRenderer(Integer.class)));

                table.setDefaultEditor(Object.class, new FormalDataCellEditor(table.getCellEditor()));

// FormalDataCellRenderer

public class FormalDataCellRenderer implements TableCellRenderer
{
    private TableCellRenderer normal;

    public FormalDataCellRenderer(TableCellRenderer cellRenderer)
    {
        super();
        this.normal = cellRenderer;
    }

    public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
    {
        if(column==1)
        {
            if(value instanceof Boolean)
            {
                return new JCheckBox("",value.equals(new Boolean(true)));
            }
            else if(value instanceof Integer)
            {
                JComboBox<String> typeSelection = new JComboBox<String>();
                typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
                typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));
                typeSelection.setSelectedIndex((Integer)(value));
                return typeSelection;
            }
        }
        return normal.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

// FormalDataCellEditor

public class FormalDataCellEditor extends DefaultCellEditor
{
    public FormalDataCellEditor(TableCellEditor editor)
    {
        super(new JCheckBox()); //? What shall I do?
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
    {
        if(column==1)
        {
            System.out.println(value.toString());

            if(value instanceof Boolean)
            {
                if(value.equals(new Boolean(true)))
                {
                    value = new Boolean(false);
                }
                else
                {
                    value = new Boolean(true);
                }
                return new JCheckBox("",value.equals(new Boolean(true)));
            }
            else if(value instanceof Integer)
            {
                JComboBox<String> typeSelection = new JComboBox<String>();
                typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
                typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));

                int selected = (Integer)(value);
                if(selected<typeSelection.getItemCount()-1)
                    selected++;
                else
                    selected = 0;
                typeSelection.setSelectedIndex(selected);
                value = new Integer(selected);

                return typeSelection;
            }
        }
        return super.getTableCellEditorComponent(table, value, isSelected,row, column);
    }
}
1

There are 1 answers

0
Developer_X On

So this is my complete example.

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class EasyExample
{
//  Attributes

//  Constructor
    public EasyExample()
    {
    }

//  Methods
    public static void main(String[] args)
    {
        Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        JFrame frame = new JFrame();

        frame.setTitle("Table Example");        
        frame.setSize(screen.width/4, screen.height/2);
        frame.setLocationRelativeTo(null);

        frame.setLayout(new BorderLayout());
        JPanel Panel = new JPanel();
        Panel.setLayout(new BoxLayout(Panel,BoxLayout.Y_AXIS));
        frame.add(Panel,"Center");

        JPanel formalData = new JPanel();
        formalData.setBorder(BorderFactory.createTitledBorder("Formal Data"));
        formalData.setLayout(new BorderLayout());
        Panel.add(formalData);

        String[] columnNames = {"", ""};
        Object[][] data =
        {
           { "A", ""},
           { "B", ""},
           { "C", new Integer(0)},
           { "D", new Boolean(true)},
           { "E", "A.B.C" },
           { "F", "Rhababer"}
        };

        JTable table = new JTable(data, columnNames)
        {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column)
            {
                if(column==0)
                    return false;
                else
                    return true;
            }
        };

        table.setRowSelectionAllowed(false);
        table.setColumnSelectionAllowed(false);
        table.setCellSelectionEnabled(false);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setDefaultRenderer( Object.class,  new FormalDataCellRenderer(table.getDefaultRenderer(Object.class)));
        table.setDefaultRenderer( Boolean.class, new FormalDataCellRenderer(table.getDefaultRenderer(Boolean.class)));
        table.setDefaultRenderer( Integer.class, new FormalDataCellRenderer(table.getDefaultRenderer(Integer.class)));

        table.setDefaultEditor(Object.class, new FormalDataCellEditor(table.getCellEditor()));

        formalData.add(new JScrollPane(table));

        frame.setVisible(true);
    }

    static class FormalDataCellRenderer implements TableCellRenderer
    {
        private TableCellRenderer normal;

        public FormalDataCellRenderer(TableCellRenderer cellRenderer)
        {
            super();
            this.normal = cellRenderer;
        }

        public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
        {
            if(column==1)
            {
                if(value instanceof Boolean)
                {
                    return new JCheckBox("",value.equals(new Boolean(true)));
                }
                else if(value instanceof Integer)
                {
                    JComboBox<String> typeSelection = new JComboBox<String>();
                    typeSelection.addItem("male");
                    typeSelection.addItem("female");
                    typeSelection.setSelectedIndex((Integer)(value));
                    return typeSelection;
                }
            }
            return normal.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    }

    static class FormalDataCellEditor extends DefaultCellEditor
    {
        public FormalDataCellEditor(TableCellEditor editor)
        {
            super(new JCheckBox()); //? What shall I do?
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
        {
            if(column==1)
            {
                if(value instanceof Boolean)
                {
                    if(value.equals(new Boolean(true)))
                    {
                        value = new Boolean(false);
                    }
                    else
                    {
                        value = new Boolean(true);
                    }
                    return new JCheckBox("",value.equals(new Boolean(true)));
                }
                else if(value instanceof Integer)
                {
                    JComboBox<String> typeSelection = new JComboBox<String>();
                    typeSelection.addItem("male");
                    typeSelection.addItem("female");

                    int selected = (Integer)(value);
                    if(selected<typeSelection.getItemCount()-1)
                        selected++;
                    else
                        selected = 0;
                    typeSelection.setSelectedIndex(selected);
                    value = new Integer(selected);

                    return typeSelection;
                }
            }
            return super.getTableCellEditorComponent(table, value, isSelected,row, column);
        }
    }
}