Problem/Question Using JComboBox as a tablecelleditor

Discussion in 'Java' started by dieselmachine@gmail.com, Jan 28, 2007.

  1. Guest

    I have the following code, which uses two different methods of editing
    a cell with a jcombobox. With the first column, i can click once, and
    have the menu pop up, but when i select a value, the popup disappears,
    but the arrow indicating a dropdown remains, and the color of the cell
    remains the color of the dropdown.

    In the second column, this problem is not present. I am wondering what
    changes I would need to make to the first column's editor code in
    order to have it behave the same way as the second column's editor. Or
    more in general, what is the method that causes the editor to vanish
    and yield to the cellrenderer?

    Here is the code in its entirety:

    import java.awt.BorderLayout;
    import javax.swing.*;
    import javax.swing.table.*;

    class comboTest extends JFrame
    { private JPanel panel;
    private JTable table;
    private JScrollPane scrollpane;

    public comboTest()
    {
    setSize( 300, 200 );

    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    getContentPane().add(panel);

    String columns[] = {"Type 1","Type 2"};
    String combochoices[] = {"value1","value2","value3"};
    String data[][] =
    {
    {"value1","value2"},
    {"value2","value3"}
    };
    table = new JTable( data, columns );

    TableColumn col0 = table.getColumnModel().getColumn(0);
    col0.setCellEditor(new comboBoxEditor(combochoices));
    TableColumn col1 = table.getColumnModel().getColumn(1);
    col1.setCellEditor(new comboBoxEditor2(combochoices));

    scrollpane = new JScrollPane( table );
    panel.add( scrollpane, BorderLayout.CENTER );
    }

    public static void main( String args[] )
    { comboTest frame = new comboTest();
    frame.setVisible(true);
    }
    }

    class comboBoxEditor extends AbstractCellEditor implements
    TableCellEditor
    { JComponent component;

    public comboBoxEditor(String[] choices)
    { component = new JComboBox(choices);
    }
    public Object getCellEditorValue()
    { return ((JComboBox)component).getSelectedItem();
    }
    public JComponent getTableCellEditorComponent
    (JTable table, Object value, boolean isSelected, int rowIndex, int
    vColIndex)
    { ((JComboBox)component).setSelectedItem(value);
    return component;
    }
    }
    class comboBoxEditor2 extends DefaultCellEditor
    { public comboBoxEditor2(String[] choices)
    { super(new JComboBox(choices));
    }
    public Object getCellEditorValue()
    { return ((JComboBox)getComponent()).getSelectedItem();
    }
    }
     
    , Jan 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacques Chaurette

    using editable jcombobox

    Jacques Chaurette, Oct 2, 2005, in forum: Java
    Replies:
    7
    Views:
    5,896
    Ravindra Khyalia
    Sep 6, 2007
  2. Ben
    Replies:
    5
    Views:
    1,186
    Roedy Green
    Feb 13, 2006
  3. Replies:
    2
    Views:
    2,292
  4. softwarepearls_com

    Using JTree to render JComboBox popup

    softwarepearls_com, Nov 18, 2008, in forum: Java
    Replies:
    2
    Views:
    1,377
    softwarepearls_com
    Nov 19, 2008
  5. mttc
    Replies:
    6
    Views:
    1,130
Loading...

Share This Page