JTable and combobox cell editor problem

K

kim.berrisford

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());
}
}

}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top