将ArrayList添加到JComboBox [英] Adding a ArrayList into a JComboBox

查看:96
本文介绍了将ArrayList添加到JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将ArrayList添加到JComboBox时遇到问题.有人可以就如何进行提供一些建议吗?我似乎无法让ArrayList进入ComboBox.用arrayLists填充JCombobox的方法有哪些?

I am running into an issue with adding a ArrayList into a JComboBox. Can someone give some advice on how to proceed? I can't seem to get the ArrayList to go into the ComboBox. What are the different ways to work with populating JCombobox's with arrayLists?

HotelSystem.Java

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

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

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) {


    }

}

推荐答案

您需要在comobox对象上调用setModel().

you need to call setModel() on comobox obect.

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

参见doc 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆