• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable with Drop down issue.

 
kopparapu v kiran kumar
Ranch Hand
Posts: 32
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm facing a issue with Jtable. My Jtable has got a column with the price drop down. That drop down will have some list of prices. Whenever i'm selecting a value from one drop down, automatically the same value gets selected in the all the below rows of the same column.

How to fix this issue?? Please provide a solution for this issue.


Thanks and Regards,
Kiran Kumar K.V
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you got so far and where are you stuck?
 
Rob Camick
Ranch Hand
Posts: 2676
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JTable API and follow the link to the section in the Swing tutorial on "How to Use Tables" for a working examples that use a combo box as an editor.
 
kopparapu v kiran kumar
Ranch Hand
Posts: 32
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my render class

public class MyComboBoxRenderer extends JComboBox implements javax.swing.table.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());
}
//table.setValueAt(value, row, column);
setSelectedItem(value);
return this;
}
}

Editor class:

public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}

and I'm passing the values like this:
String[] values = {"a", "b"};

//...
TableColumn col = jTable1.getColumnModel().getColumn(4);
col.setCellEditor(new com.gmis.analysis.renderers.MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));


This is how my code is...

Please help me to solve this issue. It is very much important for me.
 
Rob Camick
Ranch Hand
Posts: 2676
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me to solve this issue.


I did give you the solution. The tutorial has a working example.

There is no need to create a custom renderer.

Also, use the "Code" tags when posting code.
 
kopparapu v kiran kumar
Ranch Hand
Posts: 32
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rob,


Thank you very much!! This tutorial helped me to solve this issue. Yes you are right for this case we dont need to have a custom renderer.

A big Thanks to you again!!

bye,
kiran
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic