jeudi 3 mars 2016

Variable amount of checkboxes

I have an ArrayList of Student objects that have String name and a boolean absent, When I open up the program I want it to use the ArrayList to create a variable amount of checkboxes dependent on how many students there are. I tried a for each loop that would create a new checkbox for every student but that crashed the entire program.

Here is the class with the GUI view

public class SchedulerView extends JFrame implements ActionListener
{
    private String[] shownames = {"show1","show2"};

    private JPanel contentPane;

    private static int show;

    public SchedulerView() 
    {
        setTitle("School of Rock Scheduler");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblSelectShow = new JLabel("Select Show:");
        lblSelectShow.setBounds(77, 11, 107, 14);
        contentPane.add(lblSelectShow);

        JComboBox showlist = new JComboBox(shownames);
        showlist.setSelectedIndex(0);
        showlist.addActionListener(this);
        showlist.setBounds(184, 8, 149, 20);
        contentPane.add(showlist);


        JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
        chckbxNewCheckBox.setBounds(19, 60, 97, 23);
        contentPane.add(chckbxNewCheckBox);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JComboBox cb = (JComboBox)e.getSource();
        int i = cb.getSelectedIndex();
        show = i;
        System.out.println(show);

    }

    public static void main(String[] args) 
    {
        Song song1 = new Song("Holiday", false);
        Song song2 = new Song("Boulevard of Broken Dreams", false);
        Song song3 = new Song("Learn to Fly", false);
        Song song4 = new Song("Come As You Are", false);
        Song song5 = new Song("Everlong", false);

        ArrayList<Song> songlist1 = new ArrayList<Song>();
        songlist1.add(song1);songlist1.add(song2);
        songlist1.add(song3);songlist1.add(song4);
        songlist1.add(song5);

        Student student1 = new Student("Dustin",false,song1);
        Student student2 = new Student("Deloris",false,song2);
        Student student3 = new Student("Gianna",false,song3);
        Student student4 = new Student("Kelly",false,song4);
        Student student5 = new Student("Reggie",false,song5);

        ArrayList<Student> studentlist1 = new ArrayList<Student>();
        studentlist1.add(student1);studentlist1.add(student2);
        studentlist1.add(student3);studentlist1.add(student4);
        studentlist1.add(student5);

        Schedule.createSchedule(studentlist1, songlist1);

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SchedulerView frame = new SchedulerView();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}




Aucun commentaire:

Enregistrer un commentaire