简体   繁体   中英

Populate JCombobox With Arraylist

I can't seem to get the ArrayList to go into the combobox. 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"

I am implementing 3-tier architecture for my application. What am I doing wrong here?

Entity - 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

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

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. 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.

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. ie comboBox1.addItem(rNumList.get(i).getRoomNo()); instead of 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). 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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