简体   繁体   English

用Arraylist填充JCombobox

[英]Populate JCombobox With Arraylist

I can't seem to get the ArrayList to go into the combobox. 我似乎无法让ArrayList进入组合框。 The combobox is supposed to display the available room numbers. 该组合框应该显示可用的房间号。

Currently, when it compiles the combobox is showing what I think to be some kind of exception. 目前,在编译时,组合框显示了我认为是某种异常的东西。 The combobox displays rows with "HotelReservation.entity.Rooms@24084dad, HotelReservation.entity.Rooms@716ab511..etc" 组合框显示带有“ HotelReservation.entity.Rooms @ 24084dad,HotelReservation.entity.Rooms @ 716ab511..etc”的行

I am implementing 3-tier architecture for my application. 我正在为我的应用程序实现三层体系结构。 What am I doing wrong here? 我在这里做错了什么?

Entity - Rooms.java 实体-Rooms.java

public ArrayList<Rooms> getRoomNums() {
    // TODO Auto-generated method stub
    ArrayList<Rooms> rNumList = new ArrayList<Rooms>();
    DBController db = new DBController();
    String dbQuery="SELECT DISTINCT roomNo FROM Rooms "
            + "WHERE hotelNo='" + CheckAvailability.hotNo + "' AND type='" + AgentDetails.typeDin + "' AND roomNo NOT IN (SELECT roomNo FROM booking WHERE departureDate >= '" 
            + CheckAvailability.depDate + "' AND arrivalDate <= '" + CheckAvailability.arrDate + "');";
    try{
        db.getConnection();
        ResultSet rs = db.readRequest(dbQuery);
        while(rs.next())
        {
            Rooms r = new Rooms(rs.getString("roomNo"));
            rNumList.add(r);
        }
     }

    catch(Exception e){
        e.printStackTrace();
    }
    db.terminate();
    return rNumList;
}

Controller class - AdmininistrateController.java 控制器类-AdmininistrateController.java

public ArrayList processGetRoomNum() {
    // TODO Auto-generated method stub
    Rooms s = new Rooms();
    ArrayList<Rooms> rNumList = new ArrayList<Rooms>();
    rNumList = s.getRoomNums();
    return rNumList;

}

UI - GuestDetails.java 用户界面-GuestDetails.java

comboBox1 = new JComboBox();
comboBox1.setFont(new Font("Tahoma", Font.PLAIN, 15));
comboBox1.setBounds(61, 9, 411, 25);
ucc = new AdministrateController();
ArrayList<Rooms>rNumList = new ArrayList<Rooms>();
rNumList = ucc.processGetRoomNum();
displayRoomNo(rNumList);


private void displayRoomNo(ArrayList<Rooms> rNumList) {
    // TODO Auto-generated method stub

    //I have tried two different ways but still didn't work
    //#1 For loop
    for (int i = 0; i < rNumList.size(); i++){

            comboBox1.addItem(rNumList.get(i));
     }

    //#2Convert to array & set model
    DefaultComboBoxModel model = new DefaultComboBoxModel(rNumList.toArray(new 
    Rooms[rNumList.size()]));
    comboBox1.setModel(model);

HotelReservation.entity.Rooms@24084dad

What you're seeing is the memory location of the Rooms object. 您所看到的是Rooms对象的内存位置。 You can simply override the toString() of the Rooms class and return how you want it to be represented (ie just the room number) as a String. 您可以简单地重写Rooms类的toString()并返回您希望它如何以String表示(即仅房间号)。

Another thing you may want to consider is that if you just want a list of the roomNo , can you just use the room number for the combobox. 您可能要考虑的另一件事是,如果您只想要roomNo的列表,您可以只使用组合框的房间号吗? ie comboBox1.addItem(rNumList.get(i).getRoomNo()); comboBox1.addItem(rNumList.get(i).getRoomNo()); instead of comboBox1.addItem(rNumList.get(i)); 而不是comboBox1.addItem(rNumList.get(i)); This way you don't have to override the toString() of the Rooms class, in case you want the String representation to be more than just the room number (maybe for something else). 这样,您就不必重写Rooms类的toString()了,以防您希望String表示的内容不仅仅是房间号(也许还有其他内容)。 The problem with this though is that when you select the room number, you will not be selecting the Rooms object, but instead just the room number. 但是,这样做的问题是,当您选择房间号时,您将不会选择“ Rooms对象,而只会选择房间号。 You would have to do some extra query (with the room number) to use that room db domain object. 您必须做一些额外的查询(使用房间号)才能使用该房间数据库域对象。 So it really depends on the full requirement for which way you should go. 因此,这实际上取决于您应该选择哪种方式的完整要求。

The combobox displays rows with... 组合框显示带有...的行

Your query only returns a list of room numbers, not entire rows. 您的查询仅返回房间号列表,而不返回整个行。 I also notice that you are not using ORM entity, that you are just creating a default Rooms for each room number obtained from the qeusry, so the rooms object don't have an specific db data for each rooms. 我还注意到您没有使用ORM实体,只是为从qeusry获得的每个房间号创建一个默认Rooms ,因此Rooms对象没有每个房间的特定db数据。 So you may just want to use the second method. 因此,您可能只想使用第二种方法。 But then again, if you do it this way, I see no point in creating a list of Rooms. 但是话又说回来,如果您这样做,创建房间列表将毫无意义。 Instead just create a list or room numbers from the result set. 而是只从结果集中创建一个列表或房间号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM