将ArrayList添加到JComboBox中

时间:2014-04-25 17:02:08

标签: java user-interface arraylist jcombobox

我遇到了将ArrayList添加到JComboBox的问题。有人可以就如何进行提供一些建议吗?我似乎无法让ArrayList进入ComboBox。使用arrayLists填充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) {


    }

}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

JComboBox应与ComboBoxModel一起使用。

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

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

答案 2 :(得分:0)

你需要在组合框对象上调用set Model()。

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

请参阅文档here