2014-04-25 126 views
-1

我遇到了将ArrayList添加到JComboBox的问题。有人可以就如何继续提供一些建议吗?我似乎无法让ArrayList进入组合框。使用arrayLists填充JCombobox的不同方法有哪些?将ArrayList添加到JComboBox中

HotelSystem.Java

package hotelManage; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 

import javax.swing.*; 

public class HotelSystem extends JFrame implements ActionListener { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1840835913045151061L; 

    private JFrame mainFrame; 
    private JPanel mainPanel; 
    private JPanel btnPanel; 
    private JButton btnRoom; 
    private JButton btnCustomer; 
    private JButton btnOrder; 
    private JButton btnSearch; 
    private CardLayout cLayout; 
    private JLabel lblUpdate; 
    // 



    // 
    OrderSystem order = new OrderSystem(); 
    JPanel orderPanel = order.getOrderPanel(); 
    // 
    RoomSystem room = new RoomSystem(); 
    JPanel roomPanel = room.getRoomPanel(); 
    ArrayList<String> AvailableRooms = room.getAvailableRooms(); 
    // 
    private JButton btnBackfromRoom = room.getBtnBack(); 
    private JButton btnBackfromOrder = order.getBtnBack(); 


    //other 
    private JButton btnUserInputEdit = room.getBtnEdit(); 


    public HotelSystem(){ 
     showGUI(); 
     registerListeners(); 


    } 

    private void showGUI(){ 
     //create JFrame(mainFrame) 
     mainFrame = new JFrame("Hotel Management System"); 
     mainFrame.setSize(500,300); 
     mainFrame.setLayout(new GridLayout(2,0)); 


     //create buttons 
     btnRoom = new JButton("Room Editor"); 
     btnCustomer = new JButton("Customer Editor"); 
     btnOrder = new JButton("Order"); 
     btnSearch = new JButton("Search"); 

     //create Labels 
     lblUpdate = new JLabel("Instructions/details will go here."); 

     //create main panel 
     mainPanel = new JPanel(); 
     cLayout = new CardLayout(); 
     mainPanel.setLayout(cLayout); 

     //create panel that holds the buttons 
     btnPanel = new JPanel(); 

     //add buttons to panel 
     btnPanel.add(btnRoom); 
     btnPanel.add(btnCustomer); 
     btnPanel.add(btnOrder); 
     btnPanel.add(btnSearch); 
     btnPanel.add(lblUpdate); 


     //Button & Room Panel 
     mainPanel.add(btnPanel, "Buttons"); 
     mainPanel.add(roomPanel, "Rooms"); 
     mainPanel.add(orderPanel, "Orders"); 


     //other stuff 
     mainFrame.add(mainPanel); 
     mainFrame.setVisible(true); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 


    public void registerListeners(){ 
     //register all buttons to self 
     btnRoom.addActionListener(this); 
     btnCustomer.addActionListener(this); 
     btnOrder.addActionListener(this); 
     btnSearch.addActionListener(this); 
     btnBackfromRoom.addActionListener(this); 
     btnBackfromOrder.addActionListener(this); 
     btnUserInputEdit.addActionListener(this); 
    } // end registerListeners 

    public void actionPerformed(ActionEvent e){ 
     System.out.println(e.getActionCommand()); 
     //check all button presses and send 
     //control to appropriate methods 
     if (e.getSource() == btnRoom){ 
      cLayout.show(mainPanel, "Rooms"); 
     } else if (e.getSource() == btnCustomer){ 
     } else if (e.getSource() == btnOrder){ 
      cLayout.show(mainPanel, "Orders"); 
     } else if (e.getSource() == btnSearch){ 
     } else if (e.getSource() == btnBackfromRoom){ 
      cLayout.show(mainPanel, "Buttons"); 
     } else if (e.getSource() == btnBackfromOrder){ 
      cLayout.show(mainPanel, "Buttons"); 
     } else if (e.getSource() == btnUserInputEdit){ 
      //retrieve contents from ComboBox. 
      JComboBox<String> test = room.getRoomType(); 
      String selectedItem = (String) test.getSelectedItem(); 

      //adds user input into array 
      AvailableRooms.add(selectedItem); 



      //testing functionality of.. something. 
      for(String item: AvailableRooms){ 
       System.out.println("retrieved element: " + item); 
       lblUpdate.setText("A new room has been created! It has been stored under the " + item + " category."); 
       } 
      cLayout.show(mainPanel, "Buttons"); 

     } else { 
      //lblOutput.setText("something went wrong"); 
     } // end if 


    } // end actionPerformed 




    public static void main(String[] args) { 
     new HotelSystem(); 

    } 


} 

RoomSystem.Java

package hotelManage; 

import java.awt.*; 
import java.util.ArrayList; 

import javax.swing.*; 

public class RoomSystem { 


    private JButton btnEdit; 
    private JButton btnBack; 
    private JPanel roomPanel; 
    private JComboBox<String> roomType; 

    GridBagLayout gridBag; 

    //RoomPent, RoomLarge, RoomSmall 
    String[] roomArray = { "Penthouse", "Large Room", "Small Room" }; 
    ArrayList<String> AvailableRooms = new ArrayList<String>(); 


    public RoomSystem(){ 
     setBtnEdit(new JButton("Create")); 
     setBtnBack(new JButton("Back to Main Menu")); 

     Label lblRoom= new Label("Room Type: "); 

     setRoomType(new JComboBox<>(roomArray)); 



     roomPanel = new JPanel(); 
     roomPanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gridConst = new GridBagConstraints(); 


     //Panel Dimensions 

     gridConst.gridx = 0; 
     gridConst.gridy = 0; 
     roomPanel.add(lblRoom, gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 0; 
     roomPanel.add(roomType, gridConst); 

     gridConst.gridx = 0; 
     gridConst.gridy = 2; 
     roomPanel.add(getBtnEdit(), gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 2; 
     roomPanel.add(getBtnBack(), gridConst); 


    } 


    public JPanel getRoomPanel() { 
     return roomPanel; 
    } 


    public void setRoomPanel(JPanel roomPanel) { 
     this.roomPanel = roomPanel; 
    } 


    public JButton getBtnBack() { 
     return btnBack; 
    } 


    public void setBtnBack(JButton btnBack) { 
     this.btnBack = btnBack; 
    } 


    public JButton getBtnEdit() { 
     return btnEdit; 
    } 


    public void setBtnEdit(JButton btnEdit) { 
     this.btnEdit = btnEdit; 
    } 


    public ArrayList<String> getAvailableRooms(){ 
     return AvailableRooms; 

    } 

    public void setAvailableRooms(ArrayList<String> AvailableRooms){ 
     this.AvailableRooms = AvailableRooms; 
    } 


    public JComboBox<String> getRoomType() { 
     return roomType; 
    } 


    public void setRoomType(JComboBox<String> roomType) { 
     this.roomType = roomType; 
    } 



} 

OrderSystem.Java

package hotelManage; 

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.util.ArrayList; 

import javax.swing.*; 

public class OrderSystem { 

    private JButton btnOrder; 
    private JButton btnBack; 
    private JPanel orderPanel; 
    private JTextField firstName; 
    private JTextField lastName; 
    //private JComboBox<String> roomList; 
    String[] arraynamehere = { "Temp", "Temp1", "Temp2" }; 
    RoomSystem room = new RoomSystem(); 
    ArrayList<String> aRooms = room.getAvailableRooms(); 
    JComboBox<String> roomList; 
    //DefaultComboBoxModel<String> Modes = new DefaultComboBoxModel<String>(); 

    public OrderSystem(){ 
     btnOrder = new JButton("Process Order"); 
     setBtnBack(new JButton("Back to Main Menu")); 

     JLabel lblfName = new JLabel("First Name: "); 
     JLabel lbllName = new JLabel("Last Name: "); 
     JLabel lblfEmptyRooms = new JLabel("Available Rooms: "); 

     firstName = new JTextField("", 20); 
     lastName = new JTextField("", 20); 


     //ComboBox that should hold the elements of aRooms (Array) 
     roomList = new JComboBox<String>(); 


     orderPanel = new JPanel(); 
     orderPanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gridConst = new GridBagConstraints(); 

     //Panel Dimensions 

     gridConst.gridx = 0; 
     gridConst.gridy = 0; 
     orderPanel.add(lblfName, gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 0; 
     orderPanel.add(firstName, gridConst); 

     gridConst.gridx = 0; 
     gridConst.gridy = 1; 
     orderPanel.add(lbllName, gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 1; 
     orderPanel.add(lastName, gridConst); 

     gridConst.gridx = 0; 
     gridConst.gridy = 2; 
     orderPanel.add(lblfEmptyRooms, gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 2; 
     orderPanel.add(roomList, gridConst); 

     gridConst.gridx = 0; 
     gridConst.gridy = 3; 
     orderPanel.add(btnOrder, gridConst); 
     gridConst.gridx = 1; 
     gridConst.gridy = 3; 
     orderPanel.add(getBtnBack(), gridConst); 


    } 




    public JPanel getOrderPanel() { 
     return orderPanel; 
    } 


    public void setOrderPanel(JPanel orderPanel) { 
     this.orderPanel = orderPanel; 
    } 

    public JButton getBtnBack() { 
     return btnBack; 
    } 


    public void setBtnBack(JButton btnBack) { 
     this.btnBack = btnBack; 
    } 

    public static void main(String[] args) { 


    } 

} 
+0

请将您的问题用你的问题的一个小例子。 – ggovan

+0

你能告诉我如何添加一个ArrayList到JComboBox? – user3552931

+0

是什么让你觉得你没有正确地引用Arraylist?你是否收到某种错误? –

回答

0

您可以使用toArray()方法,并将其传递到JComboBox,如下所示:

JComboBox roomList = new JComboBox(aRooms.toArray()); 
0

A JComboBox应与ComboBoxModel一起使用。

roomList = new JComboBox<>(new DefaultComboBoxModel<String>(aRooms.toArray(new String[aRooms.size()));

此代码将创建并填充组合框模型。

+0

由于某种原因没有解决。它不会在运行或任何事件中发生错误,但组合框仍不会更新。 – user3552931

+0

代码运行时,aRooms的内容是什么?它是否被填充? – ggovan

0

你需要在comobox obect上调用setModel()。

comboBox.setModel(new DefaultComboBoxModel<>(aRooms.toArray(new String[aRooms.size()]))); 

见文件here

+0

我试过了,它似乎没有工作;它仍然不会更新 – user3552931

+0

@ user3552931我相信。它会设置模型,我知道你做错了。在OderSystem calss中,roomList'aRooms'是空的。 –

+0

在RoomSystem类中更新getAvailableRooms,如下所示public ArrayList getAvailableRooms(){ \t AvailableRooms.addAll(Arrays.asList(roomArray)); 返回this.AvailableRooms; }请记住,不要忘记在OrderSystem类中为set roomModel设置roomList组合框 –