hi im implimenting a gui applicaion so i have a list of shapes =>array of objects
and i want to select a shape from the list im stuck in implimenting the listener class
how do i know the selected object that is in my list it wont work with me
this is part of the code....

private JList list;
    private shape []shapesList= {new line(),new rectangle(),new oval()};
    private JLabel l1;
    private JComboBox bx1;
    private String[] clr={"red","yellow","green","blue","black"};
    private int selctIndex1,selectedIndx1;//will contain the index of the index (list,combo box)
    private class listHandler implements ListSelectionListener
           public void valueChanged(ListSelectionEvent ev)
//im stuck here how to do it
5 Years
Discussion Span
Last Post by moonL!ght

your stuck at what exactly?
also, since you're shapes are not primitives, and ev.getSource returns an object, use the equals method to check for equality.


In your valueChanged method ev.getSource() will just be the list box itself, which you probably knew anyway. You can use list.getSelectedValue() to find out which object in the list is currently selected inside your valueChanged method.

ps @stultuske: in this particular case both getSource and getSelectedValue return the actual object in question, so an == test is appropriate.

Votes + Comments
learning every day :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.