1 year ago
#359162
Artem Barabash
How can I do ListSelectionListener from Object's list?
I have a task to do using ListSelectionListener
. I need to output there, an ArrayList
with class objects.
public class CityBaseLandingSite {
public String city;
public String country;
public boolean possibleLanding;
public CityBaseLandingSite(String city, String country, boolean possibleLanding) {
super(city, country);
this.city = city;
this.country = country;
this.possibleLanding = possibleLanding;
}
}
public class JFrameDemo extends JFrame {
JLabel label = new JLabel();
public JFrameDemo(String title) throws HeadlessException
{
super(title);
setBounds(100, 100, 200, 200);
Container ControlHost = getContentPane();
ControlHost.setLayout(new FlowLayout());
ArrayList<CityBaseLandingSite> citiesArrayList = new ArrayList<CityBaseLandingSite>();
citiesArrayList.add(new CityBaseLandingSite("New York", "USA", true));
citiesArrayList.add(new CityBaseLandingSite("Kyiv", "Ukraine", true));
citiesArrayList.add(new CityBaseLandingSite("Tel-Aviv", "Israel", false));
JList ListCities = new JList(citiesArrayList.toArray());
ListCities.setVisibleRowCount(4);
JScrollPane jcp = new JScrollPane(ListCities);
ControlHost.add(jcp);
ControlHost.add(label);
ListCities.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object SelectedCity = ListCities.getSelectedValue();
label.setText(SelectedCity.toString());
}
});
}
public static void main(String[] args){
JFrameDemo jFrameDemo = new JFrameDemo("City");
jFrameDemo.setSize(500, 500);
//fixed sizes
jFrameDemo.setResizable(false);
jFrameDemo.setVisible(true);
}
}
Field of City need in JList
(only name city), cities data need be in label.
java
swing
jlist
listselectionlistener
0 Answers
Your Answer