Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error When trying to display

 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to write a program that displays the selected item from a JList but what I get is the following LJava.lang.Object:@7ca8\

The following is my code(WHERE DID I GO WRONG???)

JFrame f = new JFrame("Order Please");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
String[] items = { "Ketchup", "Mustard", "Pickle", "Onion", "Tomato",
"Lettuce", "Mayonise"};
final JList list = new JList(items);
String [] items2 = {"Beef", "Chicken", "Beef", "Veggie"};
JComboBox maincourse = new JComboBox(items2);
JButton mybutton = new JButton("Print");
mybutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Object[] selection = list.getSelectedValues();
String[] selection2 = (String)selection;
System.out.print(selection);
}
});
Container content = new JPanel();
content.add(maincourse);
content.add(list);
content.add(mybutton);
f.setContentPane(content);
f.pack();
f.setLocation(300, 300);
f.setVisible(true);
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing the entire array to println():


This will call toString() on your Object array, which will produce the output you're getting.

Try printlng out the contents of the array instead, e.g.:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic