mercredi 13 juillet 2016

How can I access ArrayList from another class?

I have created an arraylist in one class and after clicking a JButton and switching to another panel and another class, I want to be able to access it and generate a random number. How can I do that?

 ArrayList<Integer> hiraganaArray = new ArrayList<Integer>();

 OCheckBox.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED) {
                hiraganaArray.add(1);
                hiraganaArray.add(2);
                hiraganaArray.add(3);
                hiraganaArray.add(4);
                hiraganaArray.add(5);
            }
            else {
                hiraganaArray.removeAll(Arrays.asList(1));
                hiraganaArray.removeAll(Arrays.asList(2));
                hiraganaArray.removeAll(Arrays.asList(3));
                hiraganaArray.removeAll(Arrays.asList(4));
                hiraganaArray.removeAll(Arrays.asList(5));
            }
        }

 startButton = new JButton("Start");
    startButton.setPreferredSize(new Dimension(100, 40));
    gbc.gridx = 3;
    gbc.gridy = 13;
    add(startButton, gbc);

    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layout.show(panelCont, "6");
        }
    });

It's working very well when I add

  System.out.println(hiraganaArray.get(r.nextInt(hiraganaArray.size())));

to the JButton's Action Listener, but the same code doesn't work when used in another class.




Aucun commentaire:

Enregistrer un commentaire