problem with comboBox editor

Hi experts,

i am keeping a combobox renderer and editor in my JTable for 2 columns say 5th column and 7th column.

In renderer no Problem.

But while i am editing ,whenever i am choosing a perticular item from the drop down in the first row of table,it is affecting the remainingset of rows.,not all the rows also.

for example,next 5 rows are getting the same selected value.

next some 2 rows are getting affected.

the below 2 class are my renderer and editor.publicclass ComboBoxRendererextends JComboBoximplements TableCellRenderer{

public ComboBoxRenderer(String[] items){

super(items);

}

public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int column){

if (isSelected){

// Select the current value

setSelectedItem(value);

}

returnthis;

}

}

My ComboBox editor ispublicclass ComboBoxEditorextends DefaultCellEditor{

public ComboBoxEditor(String[] items){

super(new JComboBox(items));

}

}

so i am using this render and editor in my swing screen by the following codeString strSP[] ={"P","O","H"," "};

column5.setCellEditor(new ComboBoxEditor(strSP));

column5.setCellRenderer(new ComboBoxRenderer(strSP));

I am sure like the problem with my renderer and editoronly,but i am helpless.

Please if someone help me to come out of this problem,it will be much helpful for me .

thanks

[2773 byte] By [sheelata] at [2007-11-26 16:13:26]
# 1
Your problem sounds very similar to this one: http://forum.java.sun.com/thread.jspa?threadID=5128282
Rodney_McKaya at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

Did you test your code without the custom renderer and editor? Did it work?

Did you try the code with only the renderer and editor? Did it work?

Did you try the code with both the renderer and editor? Did it work?

In other words did you try to isolate the problem?

This is how I normaly create an editor:

String[] items = { "one", "two", "three", "four" };

JComboBox editor = new JComboBox( items );

DefaultCellEditor dce = new DefaultCellEditor( editor );

table.getColumnModel().getColumn(0).setCellEditor(dce);

Simple working example can be found here:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=617938

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi,

Thanks for u'r reply

I tried in so many ways.

I tried with u'r editor,and sun forum examples also.

I brief what i want.

I am having table with 3 column.

The zeroth col, and the 2 col is a combo box.so i am rendering.

when i choose a one from zeroth col combo Box ,the 2nd col ComboBox value has to change into YES

when i choose a apart from one from zeroth col combo Box ,the 2nd col combo Box value has to change into NO.

Here my Problem is

When ever i am choosing the particular value from the 1st combo box,it is affecting the all the combobox values below(set of values,not all)..To solve that i kept the commented line in the renderer but it is creating the new problem.

I am very seriously working to solve this problem,if any one suggest to solve this,it will be very much helpful to me.

I am posting my code belowimport java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.TableModelEvent;

import javax.swing.event.TableModelListener;

import javax.swing.table.*;

public class TableComboBox extends JFrame implements TableModelListener

{

public JTable table;

DefaultTableModel model = null;

JComboBox editor2 = null;

public TableComboBox()

{

Object[][] data = { {" ", "A" , " "}, {" ", "B", " "}, {" ", "C", " "}, {" ", "D", " "}, {" ", "E", " "}, {" ", "F", " "}, {" ", "G", " "}, {" ", "H", " "} };

String[] columnNames = {"FIRST","SECOND", "THIRD" };

String[] column0Items = { "one", "two", "three", "four" };

String column2Items[] = {"YES","NO"};

model = new DefaultTableModel(data, columnNames);

model.addTableModelListener(this);

table = new JTable(model);

// Set the Zeroth column to use a combobox as its editor,renderer

table.getColumnModel().getColumn(0).setCellRenderer(new ComboBoxRenderer(column0Items));

table.getColumnModel().getColumn(0).setCellEditor(new ComboBoxEditor(column0Items));

// Set the second column to use a combobox as its editor,renderer

table.getColumnModel().getColumn(2).setCellRenderer(new ComboBoxRenderer(column2Items));

table.getColumnModel().getColumn(2).setCellEditor(new ComboBoxEditor(column2Items));

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

}

public void tableChanged(TableModelEvent e) {

int row = e.getFirstRow();

int column = e.getColumn();

if(e.getType() == TableModelEvent.INSERT) {

//

}

if (e.getType() == TableModelEvent.UPDATE) {

if(table.getEditingRow() != table.getSelectedRow())

((DefaultCellEditor)table.getCellEditor()).cancelCellEditing();

if ( column == 0 ) {

if(model.getValueAt(row, 0).equals("one")) {

model.setValueAt("YES", row, 2);

} else if(model.getValueAt(row, 0).equals("two")){

model.setValueAt("NO", row, 2);

} else if(model.getValueAt(row, 0) == "three"){

model.setValueAt("NO", row, 2);

} else if(model.getValueAt(row, 0) == "four"){

model.setValueAt("NO", row, 2);

}

}else{

//do nothing

}

}

}

public static void main(String[] args)

{

final TableComboBox frame = new TableComboBox();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

class ComboBoxRenderer extends JComboBox implements TableCellRenderer {

String items[];

public ComboBoxRenderer(String[] items) {

super(items);

this.items = items;

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

if(isSelected){

setSelectedItem(value.toString());

return this;

}/*else{

setSelectedItem(items[0]));

}*/

return this;

}

}

class ComboBoxEditor extends DefaultCellEditor {

public ComboBoxEditor(String[] items) {

super(new JComboBox(items));

}

}

sheelata at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...
# 4

class ComboBoxRenderer extends JComboBox implements TableCellRenderer {

String items[];

public ComboBoxRenderer(String[] items) {

super(items);

this.items = items;

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

if (value.equals(" "))

setSelectedIndex(0);

else

setSelectedItem(value.toString());

return this;

}

}

Rodney_McKaya at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thanks a lot,the miracle linespublic Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

if (value.equals(" ")){

setSelectedIndex(0);

}

else{

setSelectedItem(value.toString());

return this;

}

return this;

}

done really miracle in my application.

even i kept one empty string (" "),but it is not happened.

But i not kept any empty String,then i tried ,i was happened.

Once again thanks for u'r very good logic.

sheelata at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks a lot,the miracle linespublic Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

if (value.equals(" ")){

setSelectedIndex(0);

}

else{

setSelectedItem(value.toString());

return this;

}

return this;

}

done really miracle in my application.

even i kept one empty string (" "),but it is not happened.

But i not kept any empty String,then i tried ,i was happened.

Once again thanks for u'r very good logic.

sheelata at 2007-7-8 22:36:10 > top of Java-index,Desktop,Core GUI APIs...