我有2 JList
秒。一个列表包含房东对象,第二个Jlist应显示与房东一起列出的属性。
现在我的问题是,当我尝试这样做时,所有的房东和所有属性都会同时显示。
但我想要的是,当我选择一个特定的房东时,那个与房东相关的房产只能展示。
以下是我的代码:
package assignment2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class LeaseDialog extends JDialog implements ActionListener,
ListSelectionListener{
private MySystemGui owner;
private JButton exit;
//DefaultListModel<Landlord> UnleaseProperty;
DefaultListModel<Landlord> UnleasePropertyLandlord;
Iterator<Landlord> getLandlordIterator;
JList<Landlord> landlordList;
JTextArea jta;
Iterator<Property> getPropertyIterator;
DefaultListModel<Property> UnleaseProperty;
JList<Property> propertyList;
public LeaseDialog(MySystemGui owner,String title,boolean moodal) {
super(owner,title,moodal);
this.owner=owner;
this.add(LandlordListPanel());
this.add(buttonsPanel(),"South");
this.setSize(500,500);
this.setLocation(200, 200);
this.setVisible(true);
}
private JPanel LandlordListPanel() {
JPanel llp = new JPanel();
UnleasePropertyLandlord = new DefaultListModel<Landlord>();
UnleaseProperty = new DefaultListModel<Property>();
getLandlordIterator = this.owner.getRealEstate().getLandlords().iterator();
while(this.getLandlordIterator.hasNext()) {
Landlord present = this.getLandlordIterator.next();
System.out.println("The landlord is "+present.toString());
getPropertyIterator = present.getPropertiesIterator();
while(getPropertyIterator.hasNext()){
Property presentProperty= getPropertyIterator.next();
System.out.println("The property is "+presentProperty.toString());
if(presentProperty.isLeased()==false &&
!(UnleasePropertyLandlord.contains(present)) ){
UnleasePropertyLandlord.addElement(present);
//UnleaseProperty.addElement(presentProperty);
}
//else if(presentProperty.isLeased()==false){
//UnleaseProperty.addElement(presentProperty);
//}
}
}
landlordList = new JList<Landlord>(UnleasePropertyLandlord);
landlordList.setVisibleRowCount(4);
landlordList.addListSelectionListener(this);
llp.add(new JScrollPane(landlordList));
propertyList = new JList<Property>(UnleaseProperty);
propertyList.setVisibleRowCount(4);
propertyList.addListSelectionListener(this);
llp.add(new JScrollPane(propertyList));
jta = new JTextArea(10,30);
jta.setEditable(false);
llp.add(new JScrollPane(jta));
return llp;
}
private JPanel buttonsPanel(){
JPanel button = new JPanel();
exit = new JButton("Exit");
exit.addActionListener(this);
button.add(exit);
return button;
}
public void actionPerformed(ActionEvent r){
switch (r.getActionCommand()) {
case "Exit":
this.dispose();
}
}
public void valueChanged(ListSelectionEvent lease){
if (lease.getSource()==UnleasePropertyLandlord){
Landlord l = landlordList.getSelectedValue();
Iterator <Property> pr = l.getPropertiesIterator();
while (pr.hasNext()){
Property presentProperty= pr.next();
System.out.println("The property is "+presentProperty.toString());
if (presentProperty.isLeased()==false){
UnleaseProperty.addElement(presentProperty);
}
}
propertyList.setSelectedValue(UnleaseProperty, true);
}
}
}