-1

I want to take from a list the user's choice and convert it to a string with Jbox. How can I convert the contents to string so I can use it?

public class Graph extends JFrame
{
    private String temp;

    public Graph()
    { }
    public void CreateBox(String[] a)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jButton1 = new JButton("ok");
        final JList jList1 = new JList(a);
        jButton1.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                Object contents = jList1.getSelectedValue();
                //System.out.println(contents);
                setChoise((String)contents);//how can i convert it to string ?
            }
        });
        JButton jButton2 = new JButton("close");
        jButton2.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });    
        frame.add(jList1, "Center");
        frame.add(jButton1,"South");
        frame.add(jButton2,"North");
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
    public void setChoise(String temp)
    {
        this.temp=temp;
    }

    public String getChoise()
    {
        return this.temp;
    }
}
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58

3 Answers3

5

I am going to be different and explain a solution but not the one proposed of converting the Object to a String. Rather just declare your JList for its proper use (which it seems is to hold Strings). Just declare the list as

final JList<String> jList1 = new JList<String>(a);

and then the getSelectedValue() method will return a String not a raw Object allowing you to use it in methods that take in a String.

However as people have already noted, this will not work if you need to manipulate the list as Objects, so only do this if you are only using the list for Strings

chancea
  • 5,858
  • 3
  • 29
  • 39
0

Easiest way would be to implement the toString method of that object.

@Override public String toString() {

    return field1 + field2; //Example
  }

Then you can simply do

setChoise((ClassType)contents);

EDIT:

Another option will be :

Typecast the Object into its class. (In your case you are not doing it )

Say the Object is of type City you can simply put City myCity = (City)contents (Again you have to manage multiple selection)

Then call the getters of the fields you want to display. say :

setChoise(myCity.getName);
Aditya Peshave
  • 667
  • 9
  • 26
0

Make a method called:

public String toString(){

    String result = "All the values inside the class u want to display";
    return result;

}

Now When you use System.out.println(Graph);

It should get the result form the toString() method

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • It should still display it with out @Override – bhjsgh7363is Dec 05 '14 at 19:26
  • Yes and using raw types and cryptic variable names also works. But that doesn't mean you should do that. – Tom Dec 05 '14 at 19:27
  • Every one has his own way of doing something. I just never bother overriding toString methods, since they always returned same result – bhjsgh7363is Dec 05 '14 at 19:30
  • 1
    I'm not sure this is right, because it's not clear to me that the questioner wants to convert a `Graph` to a `String`. I don't see anywhere where he's trying to use `println` on a `Graph`. – ajb Dec 05 '14 at 19:40