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

}

}

[8891 byte] By [max25a] at [2007-10-3 10:18:36]
# 1

>model.addRow(data);

Why are you adding the two-dimensional array as a row in the table? If you add the same array it is natural to see same values since it is the same reference.

Please revise your code regarding data[][] array, you seem to have mixed up the references.

Mike

bellyrippera at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 2

> >model.addRow(data);

>

> Why are you adding the two-dimensional array as a row

> in the table? If you add the same array it is natural

> to see same values since it is the same reference.

>

> Please revise your code regarding data[][] array, you

> seem to have mixed up the references.

>

> Mike

Hi,

Thanks for your reply. I have added that two dimensinal array because each of those elements are containg an array of strings, so theat they can be assiged as items for comboboxes. And another thing is i wanted was one row to be displayed initialy.

Thanks again.

max25a at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 3
Any other advices friends?
max25a at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 4
Have you corrected your code so that you don't add same data array in the actionPerformed method?Kaj
kajbja at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 5
Hi Kaj,Thanks for your reply. But i want to initialize the comboboxes with the same options everytime. So, how do i modify that code?
max25a at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 6
> Hi Kaj,> Thanks for your reply. But i want to> initialize the comboboxes with the same options> everytime. So, how do i modify that code?Create a new array each time.Kaj
kajbja at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...
# 7
Tried the above suggested solution but didn't help, still first combo [first column] in the JTable getting set to the value of earlier row when new row added. Any other suggestions?
max25a at 2007-7-15 5:39:41 > top of Java-index,Java Essentials,New To Java...