将选中的复选框添加到arraylist

时间:2015-04-25 17:51:22

标签: java

我在Java上很新/不好,而且是GUI的新手。 如何将选定的复选框添加到arraylist?我试过环顾四周但却找不到任何有用的东西。 任何帮助都会被贬低。请记住,我是java新手,我的代码可能会非常糟糕。

http://tinypic.com/r/11b6pno/8 不确定是否有这个帮助,但可能会提出一个想法,因为我可能已经措辞错了,当我点击我想要的程序时,它将该程序输出到屏幕上,我想将它们放入arraylist

public class testProcedures extends JPanel
{
    private static JLabel procedureLabel;
    private static JButton orderButton, resetButton, quitButton;
    private static JTextArea orderDetails;
    private static JCheckBox[] procedure;
    private static int NUM_PROCEDURES= 5;
    static JTextField patientIDField;


    public testProcedures()
    {
        super();
        patientIDField = new JTextField("Enter Patient ID Number", 25);
        add(patientIDField);

        setPreferredSize(new Dimension(350,300));
        setLayout(new GridLayout(1,1));
        add(pane, new Dimension(1,1));
        setVisible(true);

        JFrame frame = new JFrame("Procedures");
        frame.setContentPane(split);
        frame.pack();
        frame.setVisible(true);*/
    }

    public void selectProcedures()
    {
        procedureLabel = new JLabel("Select Procedures");
        procedure= new JCheckBox[NUM_PROCEDURES];
        procedure[0] = new JCheckBox("Extraction",false);
        procedure[1] = new JCheckBox("Filling",false);
        procedure[2] = new JCheckBox("Cleaning",false);
        procedure[3] = new JCheckBox("Crown",false);
        procedure[4] = new JCheckBox("X-Ray",false);


        orderButton = new JButton("Set Procedures");
        orderButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event){



                        processOrder();
                    }
                });

        resetButton = new JButton("Reset Patient Procedures Form");
        resetButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event){
                        resetForm();
                    } });

        quitButton = new JButton("Quit Program");
        quitButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event){

                        System.exit(0);
                    } });

        orderDetails = new JTextArea("Awaiting Procedures....",5,40);
        add(procedureLabel);
        for (int index=0; index < NUM_PROCEDURES;++index)
            add(procedure[index]);
        add(orderButton);
        add(resetButton);
        add(quitButton);
        add(orderDetails);
    }

    static void processOrder()
    {
        String order = "Procedures Selected:";
        boolean proceduresSelected= false;

        String patientID = patientIDField.getText();
        int patientNumb = Integer.parseInt(patientID);
        ArrayList<Patient> PatientList = DentistTabbed.getPatientList();
        ArrayList <Procedure> procList = new ArrayList<Procedure>();
        for (int index=0; index < NUM_PROCEDURES;++index)
            if (procedure[index].isSelected())
            {
                //here
                for(Patient pat: PatientList)
                {
                    if(pat.getPatientNumber()==patientNumb)
                    {

                        //pat.addProcedure(procedure);
                        //procList.add(procedure);

                    }
                }
                //here
                proceduresSelected = true;
                order += "\n"+ procedure[index].getLabel();
            }
        if (!proceduresSelected)
            order += "No procedure Selected.";
        order += ".";
        orderDetails.setText(order);
    }
}

3 个答案:

答案 0 :(得分:0)

一般解决方案:

checkBox.setSelected(true);
list.add(checkBox);

答案 1 :(得分:0)

你可以使用循环,它将通过过程数组查找选中的CheckBoxes:

        ArrayList selected = new ArrayList();
        for(JCheckBox oneProcedure: procedure){
            if(singleProcedure.isSelected()){
                selected.add(singleProcedure);
            }
        }

但是我想知道,为什么你需要JCheckBox ArrayList?也许是一个选择的程序&#39;用String形式就足够了吗?

        ArrayList<String> selected = new ArrayList<String>();
        for(JCheckBox oneProcedure: procedure){
            if(singleProcedure.isSelected()){
                selected.add(singleProcedure.getText());
            }
        }

答案 2 :(得分:0)

在不知道ProcedurePatient类的情况下,我假设您需要复选框的实际文本,而不是在给定Patient中设置为PatientList s的复选框}:

//here
for (Patient pat: PatientList) {
    if (pat.getPatientNumber() == patientNumb) {
        Procedure proc = new Procedure(procedure[index].getText());
        pat.addProcedure(proc);

        // note that if you add the procedure to 
        // procList while looping through PatientList,
        // you will get duplicate treatments in the list if 
        // there are more than one patients
        procList.add(proc);
    }
}
//here