Problem with JTable - duplicate column data.
Hi friends,
I have three columns in a jtable. On each column i am applying JCombobox renderer. I have a button called ADD. When i click on the add button to add a row, the row is added properly without any data. But strangely the first column is keeping the values choosen in the last row. Any help?
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
publicclass TableDemo3extends JFrameimplements ActionListener{
JPanel panel =null;
JTable table =null;
JButton button =null;
JButton delete =null;
JButton insert =null;
DefaultTableModel model =null;
String values[];
String values1[];
String values2[];
Object data[][];
public TableDemo3(String title){
super(title);
table =new JTable();
model =new DefaultTableModel();
model.addColumn("Column1");
model.addColumn("Column2");
model.addColumn("Column3");
values =new String[]{" ","item1","item2","item3"};
values1 =new String[]{" ","One","Two","Three","Four"};
values2 =new String[]{" ","Java","Linux","Firefox","Swings"};
data =new Object[][]{
{values, values1, values2}
};
button =new JButton("Add");
delete =new JButton("Delete");
insert =new JButton("Insert");
panel =new JPanel();
panel.setLayout(new BorderLayout());
model.addRow(data);
table.setModel(model);
TableColumn column = table.getColumnModel().getColumn(0);
column.setCellEditor(new MyComboBoxEditor(values));
column.setCellRenderer(new MyComboBoxRenderer(values));
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new MyComboBoxEditor(values1));
column1.setCellRenderer(new MyComboBoxRenderer(values1));
TableColumn column2 = table.getColumnModel().getColumn(2);
column2.setCellEditor(new MyComboBoxEditor(values2));
column2.setCellRenderer(new MyComboBoxRenderer(values2));
// Create the table, place it into scroll pane and place
// the pane into this frame.
JScrollPane scroll =new JScrollPane();
// The horizontal scroll bar is never needed.
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.getViewport().add(table);
button.addActionListener(this);
delete.addActionListener(this);
insert.addActionListener(this);
panel.add(delete, BorderLayout.NORTH);
panel.add(insert, BorderLayout.EAST);
panel.add(scroll, BorderLayout.CENTER);
panel.add(button, BorderLayout.SOUTH);
getContentPane().add(panel, BorderLayout.CENTER);
String strPlaf ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try{
//set the look and feel as of windows
UIManager.setLookAndFeel(strPlaf);
//setting new grid border for table header
UIManager.put("TableHeader.cellBorder",BorderFactory.createEmptyBorder(1, 1, 1, 1));
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
e.printStackTrace();
}
}
publicvoid actionPerformed(ActionEvent ae){
if(ae.getSource() == button){
model.addRow(data);
model.fireTableDataChanged();
}
}
class MyComboBoxRendererextends JComboBoximplements TableCellRenderer{
public MyComboBoxRenderer(String[] items){
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,boolean hasFocus,int row,int column){
if (isSelected){
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}else{
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
returnthis;
}
}
publicclass MyComboBoxEditorextends DefaultCellEditor{
public MyComboBoxEditor(String[] items){
super(new JComboBox(items));
}
}
publicstaticvoid main(String[] args){
TableDemo3 frame =new TableDemo3("Table double click on the cell to edit.");
frame.setSize(new Dimension(640, 400));
frame.validate();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

