0

I have a JComboBox with 2 columns inside. I am getting the data from a database in an ArrayList. And with loop in the JComboBox:

for(int x = 0; x < result.size(); x++)
    combo.addItem(new String[]{
            (String) result.get(x),""+(String) result2.get(x)});

This is my Custom Renderer:

class MyRenderer extends JPanel implements ListCellRenderer<Object> {  

    private static final long serialVersionUID = 236185631594254585L;

    JLabel[] lbl = new JLabel[2];  

    public MyRenderer () {  

        setLayout(new GridLayout(0,2));  
        for(int x = 0; x < lbl.length; x++){  
            lbl[x] = new JLabel();  
            lbl[x].setOpaque(true);  
            add(lbl[x]);  
        }  
    }  

    public Component getListCellRendererComponent (JList<?> list,Object value,  
                  int index,boolean isSelected,boolean cellHasFocus) {  

        for(int x = 0; x < lbl.length; x++)
            lbl[x].setText((String)((String[])value)[x]);  
        return this;  
    }  
}  

runable code:

public class MultipleComboTest extends JFrame {

JComboBox<String[]> combo = new JComboBox<String[]>();  

ArrayList result;
ArrayList result2;

public TestingMultipleCombo(){ 

setLocation(400,300);  
setDefaultCloseOperation(EXIT_ON_CLOSE);  
combo.setRenderer(new MyRenderer());  
getContentPane().add(combo);  

 result = new ArrayList();  
 result2 = new ArrayList();

    result.add("Test1");
    result.add("Test2");
    result2.add("Value1");
    result2.add("Value2");


for(int x = 0; x < result.size();x++) combo.addItem(new String [] {(String)   result.get(x),""+(String) result2.get(x)});  
pack(); 

combo.setEditable(true);

}  
public static void main(String[] args){new TestingMultipleCombo().setVisible(true);}  
}  

class MyRenderer extends JPanel implements ListCellRenderer<Object> {  

private static final long serialVersionUID = 236185631594254585L;


JLabel[] lbl = new JLabel[2];  

  public MyRenderer(){  

    setLayout(new GridLayout(0,2));  
    for(int x = 0; x < lbl.length; x++){  
      lbl[x] = new JLabel();  
      lbl[x].setOpaque(true);  
      add(lbl[x]);  
    }  
  }  

public Component getListCellRendererComponent(JList<?> list,Object value,  
                  int index,boolean isSelected,boolean cellHasFocus){  

for(int x = 0; x < lbl.length; x++){  
  lbl[x].setText((String)((String[])value)[x]);  
}  
return this;  
}  
}  

Everything is working well until I set the JComboBox editable. It displays:

[Ljava.lang.String;@c7dc2d".

I want to display only 1 value of the 2 columns. Does anyone know how I can do this?

coo12
  • 117
  • 1
  • 11
  • 1
    Post a runnable example for better help. The text you're seeing is the `Object.toString()` of a String array. At first glance, I thought it was because of you're renderer implementation, but I see you are getting single values an putting it into labels (why, I have no idea). Maybe suggests, you haven't set the renderer. Please give us an runnable example to test out, that demonstrates the problem. – Paul Samsotha Sep 09 '14 at 08:20
  • don't put JComponent inside XxxRenderer, use JTable as Renderer – mKorbel Sep 09 '14 at 08:24
  • 2
    Also maybe wrap everything in a pojo and override the `toString` to display it how you want to. Just add instances of the pojo to the list – Paul Samsotha Sep 09 '14 at 08:25
  • 1
    Thanks for the update: But can you take out all the DB access code, and just fill the list with hard coded values. We can't run this code, as we are not using your db – Paul Samsotha Sep 09 '14 at 08:27
  • is this better explanation for, better described nad clear - [I want to display only 1 value of the 2 columns. Does anyone know how I can do this?](http://stackoverflow.com/questions/25738472/get-id-and-data-from-database-and-add-to-jcombobox) – mKorbel Sep 09 '14 at 08:31
  • know you can use the code – coo12 Sep 09 '14 at 08:33
  • Let me just get this straight. You want the combobox to be editable, and you want to edit the values in the array? – Paul Samsotha Sep 09 '14 at 08:45
  • 2
    Guess the problem is default Editor. It tries to show toString() of value (the array). You can implement your own TableCellEditor – StanislavL Sep 09 '14 at 08:47
  • @ presskillet: I want to implant my Autocomplete in the combobox so I need to make it editable. So The user is only able to choise values from the arrayList. And he should be able to typ in the Combobox. The ComboBox Popup should display 2 columns – coo12 Sep 09 '14 at 08:52

0 Answers0