JTable and combobox cell editor problem

Discussion in 'Java' started by kim.berrisford@gmail.com, Sep 27, 2006.

  1. Guest

    I have a JTable which uses a custom cell editor for comboboxes. The
    table starts out with one row and the user can add more rows by
    clicking a button. What is wrong is when the user selects a combobox
    item in the first row, and then adds a second row, going to the
    combobox column in the second row is bringing up what they selected in
    the first row. I want the combobox in the new row to initialize with
    the first item in the combobox (in this case it's an empty string).
    Here is my code, it should be able to be compiled and tested with the
    second column in the table:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.table.*;
    import java.util.*;

    public class testTable extends JPanel implements ActionListener {
    String tableName;
    String prettyName;
    String buttonName;
    int maxRows;
    JTable table;
    Vector rows, columns;
    DefaultTableModel tableModel;
    JScrollPane scrollPane;
    JButton buttonAdd, buttonDelete;
    JPanel buttonPanel;

    public static void main(String[] args) {
    JFrame jf = new JFrame("Testframe");
    jf.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    });

    testTable jsF = new testTable();
    jf.getContentPane().add(jsF, BorderLayout.CENTER);
    jf.pack();
    jf.setVisible(true);
    }

    public testTable() {

    rows = new Vector();
    columns = new Vector();

    setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
    setFocusCycleRoot(true);

    maxRows = 10;

    String[] c = {"Col1", "Col2", "Col3"};
    addColumns(c);
    tableModel = new DefaultTableModel();
    tableModel.setDataVector(rows, columns);
    table = new JTable(tableModel) {
    public void changeSelection(int row, int column, boolean
    toggle, boolean extend){
    super.changeSelection(row, column, toggle, extend);
    if(editCellAt(row, column))
    getEditorComponent().requestFocusInWindow();
    }
    };
    table.setSurrendersFocusOnKeystroke(true);
    scrollPane = new JScrollPane(table);
    table.setRowSelectionAllowed(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    setCellEditors();

    buttonPanel = new JPanel();
    buttonAdd = new JButton("Add New Row");
    buttonDelete = new JButton("Delete Row");

    buttonPanel.add(buttonAdd);
    buttonPanel.add(buttonDelete);
    buttonAdd.addActionListener(this);
    buttonDelete.addActionListener(this);

    setLayout(new BorderLayout());
    add("Center", scrollPane);
    add("South", buttonPanel);
    addRow();
    }

    public void addColumns(String[] c) {
    for(int i = 0; i < c.length; i++) {
    columns.addElement(c);
    }
    }

    public void setCellEditors() {
    TableColumn col = table.getColumnModel().getColumn(1);
    Object[] vals = new Object[] {"", "Test1", "Test2", "Test3"};
    col.setCellEditor(new AutoCompletionComboBoxEditor(vals));
    }

    public static class AutoCompletionComboBoxEditor extends
    AbstractCellEditor implements TableCellEditor {
    JComboBox cbx;

    public AutoCompletionComboBoxEditor(Object[] items){
    cbx = new JComboBox(items);
    }

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

    public Object getCellEditorValue() {
    return cbx.getSelectedItem();
    }
    }

    public void addRow() {
    if(table.getRowCount() < maxRows){
    Vector r = createBlankElement();
    rows.addElement(r);
    table.addNotify();
    }
    }

    public Vector createBlankElement() {
    Vector t = new Vector();
    t.addElement((String) "");
    t.addElement((String) "");
    t.addElement((String) "");
    return t;
    }

    public void deleteRow(int index) {
    if(index > -1){
    TableCellEditor cellEditor = table.getCellEditor();
    if(cellEditor!=null)
    cellEditor.stopCellEditing();
    ((DefaultTableModel)table.getModel()).removeRow(index);
    table.addNotify();
    }
    }

    public void actionPerformed(ActionEvent source) {
    if(source.getSource() == (JButton) buttonAdd){
    addRow();
    } else if(source.getSource() == (JButton) buttonDelete) {
    deleteRow(table.getSelectedRow());
    }
    }

    }
    , Sep 27, 2006
    #1
    1. Advertising

  2. wrote:
    >
    > public Component getTableCellEditorComponent(JTable table,
    > Object value, boolean isSelected, int row, int column) {


    cbx.setSelectedItem(value);

    > return cbx;
    > }



    Hope this helps,

    Richard
    Richard Wheeldon, Oct 1, 2006
    #2
    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. Tivo Escobar
    Replies:
    1
    Views:
    7,274
    manusa
    Apr 12, 2007
  2. news.rcn.com
    Replies:
    1
    Views:
    9,634
    Chris Smith
    Jul 12, 2004
  3. Chris
    Replies:
    0
    Views:
    8,231
    Chris
    Jun 14, 2005
  4. Replies:
    2
    Views:
    4,122
  5. RC
    Replies:
    1
    Views:
    520
    Daniel Pitts
    Nov 8, 2007
Loading...

Share This Page