0

I want to select a value in a default cell editor combobox initially. When i set that inside renderer or editor the combo always shows the same value even if the user changes it, since i am setting the value in renderer. How to set the combobox value in renderer and allow the user to make changes to the combo? below is my code:

public TableCellRenderer getCellRenderer(int row, final int column) {
if (column == 1) {    
    TableCellRenderer renderer = new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable arg0, Object arg1,boolean arg2, boolean arg3, int row, int col) {             
            String text="";
            Component  comp;


            if(lovArray[row]!=null && lovArray[row].split("\\|").length>1)
            {
                JComboBox combo = new JComboBox(lovArray[row].split("\\|"));
                comp =combo;
                //combo.setSelectedItem(values[row]);
            }
            else
            {
                comp = CustomTable.super.getCellRenderer(row, col).getTableCellRendererComponent(arg0, arg1, arg2, arg3, row, col);
            }                               

            return comp;
        }
    };      

    return renderer;
}

return super.getCellRenderer(row, column);

}

in the above code i am going to display a combo only if the values for that particular row has multiple values delimited by "|". otherwise am going to return the default renderer.

I also want to set the combo value to a particular value from an array. but since am setting inside renderer its always displayng the same value even if the user changes the combo value. how to fix this?

Mohamed Iqzas
  • 976
  • 1
  • 14
  • 19
  • a combo as renderer doesn't make much sense: it's usage essence is to choose a value from its dropdown, but the dropdown will never be visible in a rendering component. Also: don't re-create a new renderer/rendering component in every call to getCellRenderer, instead re-use the same instance – kleopatra Sep 25 '13 at 11:04
  • @kleopatra i didnt get what you mean by "but the dropdown will never be visible in a rendering component". I am able to get combobox dropdown in all the cells wherever necessary according to my code. The thing is i need to change the preselected value from combo when its getting displayed. I am able to get the dropdown and make a selection. but after the selection the combobox just displays one fixed value that i set inside the renderer. I want the cells to look like a drop down box bcoz the user should know that they can select a value from it. any suggestions? Thanks – Mohamed Iqzas Sep 25 '13 at 11:12

1 Answers1

0

sorry. It's my mistake that i made in another part of the code. In my custom table i have overriden the following:

public Object getValueAt(int row, int col)
{
if(col==0)
{
    return variables[row];
}
else if(col==1)
{
    return values[row];
}

return null;
}

this made the comboBox at column=1 to always display one specific item. Now I have removed this. and the combo box value in the cell editor is changing according to user selection.

I have removed the renderer and decided to display it as a default cell itself. when the user clicks on it I have made a combo box in a DefaultCellEditor allowing the user to select a value from the drop down.

Cheers.

Mohamed Iqzas
  • 976
  • 1
  • 14
  • 19