• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

combobox rendered in a Jtable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,


I have a JTable for which i have rendered one column as a JCombobox.
The Combobox appears to be fine.
The problem is when i select a value from the combobox, all the other values of the combobox also get selected the same value .
This happens for the first time only.
For eg i have 1,2,3,4 as combobox elements .
If i select value 1 on the first row all the other row values are also set to one.

Below is some part of the code for clarity.

Please help me.
Thanks in advance.

//this is how i add the jcombobox to the jtable cell.
jtable2.getColumn("VALIDITY").setCellRenderer(new MyComboRenderer(new JComboBox(new String[]{"1","2","3"})));


jtable2.getColumn("VALIDITY").setCellEditor(new MyComboEditor(new JComboBox(new String[]{"1","2","3"})));

Below is the MyComboRenderer and My ComboEditor class

//COMBOBOX RENDERER
class MyComboRenderer extends CustomTableCellRenderer{


public MyComboRenderer(JComboBox aRenderingComponent) {
super(aRenderingComponent);
}

public void setValue(Object aValue) {
((JComboBox)this.getRenderingComponent()).setSelectedItem(aValue);
}
public void setValue(Object aValue, int row) {
//System.out.println(" -----------cell REndere--------------");
((JComboBox)this.getRenderingComponent()).setSelectedItem(aValue);
}
}

////COMBOBOX EDITOR
class MyComboEditor extends CustomTableCellEditor implements ActionListener {


public MyComboEditor(JComboBox aRenderingComponent){
super(aRenderingComponent);
aRenderingComponent.addActionListener(this);
}

/* (non-Javadoc)
* @see
tp.table.CustomTableCellRenderer#setValue(java.lang.Object)
*/
public void setValue(Object aValue) {
// System.out.println(" -----------cell EDITOR-- avalue--------");
((JComboBox)this.getEditorComponent()).setSelectedItem(aValue);
}

public void setValue(Object aValue , int row) {
System.out.println(" -----------cell EDITOR----avalue, , row---------");
((JComboBox)this.getEditorComponent()).setSelectedItem(aValue);
}


public Object getCellEditorValue() {
//System.out.println(" cell editor value");

System.out.println(" Any combo value it prints------- > " + ((JComboBox)this.getEditorComponent()).getSelectedItem());
return ((JComboBox)this.getEditorComponent()).getSelectedItem();

}

/* (non-Javadoc)
* @see
java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// System.out.println(" Action performed ---------------Source is "+e.getSource());
fireEditingStopped();
}
}

















 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karishma

Welcome to Javaranch! We don't have too many rules around here but we do have a Naming Policy. Please read and adjust your display name accordingly.

Thanks.
 
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic