0

We want to be able to change the text/image on our JLabel array but we can´t understand why it does not work.

import java.awt.*;

class layoutWindow extends JFrame implements ActionListener {`



    String[] classStrings = { "EN1A", "EN2A", "EN3A", "EN1B", "EN2B", "EN3B",
            "ES1A", "ES2A", "ES3A", "ES1B", "ES2B", "ES3B", "H1", "H2", "H3",
            "N1A", "N1B", "N1C", "N1D", "N1E", "N2A", "N2B", "N2C", "N2D",
            "N2E","N3A","N3B","N3C","N3D","S1A","S1B","S2A","S2B","S3A","S3B" };
    String[] FormationStrings = { "3-3-3", "3-3-2", "2-4-2", "4-4", "2-2-2-2","3-4-3" };
    // Create the combo box, select item at index 4.

     JLabel[] deskLabels = new JLabel[96];



    JComboBox classList = new JComboBox(classStrings);
    JComboBox formationList = new JComboBox(FormationStrings);


    JButton seatbButton = new JButton(" randomize");
    JButton backButton = new JButton("back");
    JButton groupButton = new JButton("Make groups");

    JLabel classLabel = new JLabel("choose class");
    JLabel formationLabel = new JLabel("choose formation");

    JPanel layoutPanel = new JPanel(new GridLayout(8, 12, 1,15));
    JPanel top = new JPanel(new GridLayout(1, 2, 15,1));



    public layoutWindow() {
        super("layout Example");
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);
        Container contentArea = getContentPane();

        top.setVisible(true);
        layoutPanel.setVisible(true);

            int i = 1;
            for (JLabel label : deskLabels) {
                label = new JLabel();
                if ((i % 12) != 100) {
                    label.setText("" + i);
                    layoutPanel.add(label);}
                else {
                    label.setText("  ");
                    layoutPanel.add(label);
                }
                i++;
     }

        top.add(backButton);
        top.add(classLabel);
        top.add(classList);
        top.add(formationLabel);
        top.add(formationList);
        top.add(seatbButton);

        contentArea.add("North", top);
        contentArea.add("Center", layoutPanel);

        setContentPane(contentArea);
        formationList.addActionListener(this);
        classList.addActionListener(this);
        backButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent event) {


        String string = (String) formationList.getSelectedItem();

         if(string == "4-4"){
            deskLabels[5].setText("aasdggnmh");

         }
        }
        }

layouttaskwindow

public class layouttaskwindow {

    public static void main(String[] args) {

        layoutWindow win = new layoutWindow();
    }

}

We don't understand why this does not work. We create all labels from the array using a for loop but when we try to change them it does not work.we have tried to change it in the ActionEvent but we get no response, could someone please explain how this could be solved?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
david
  • 11
  • 3
  • Could you explain what you expect to happen? There are several errors in this code, but it's not clear what problem you are refering to. My first guess is this line: deskLabels[5].setText("aasdggnmh"); I expect this to throw a NPE since you never put anything into deskLabels. – makrom Apr 26 '16 at 13:10
  • well what i want to happen is so my labels within the JLabel array to react to commands, right now it doesn´t at all. – david Apr 26 '16 at 16:53

1 Answers1

1

I also think (like Hackerdarshi said, I did not read the post, but looks good) the problem is caused by the foreach loop. Not sure, but I think Java does not give you the right reference. So to fix it you could try this:

        for (int i = 0; i < deskLabels.length; i++) {
                deskLabels[i] = new JLabel();
                if ((i % 12) != 100) deskLabels[i].setText("" + i);
                else deskLabels[i].setText("  ");
                layoutPanel.add(deskLabels[i]);
     }

And as Berger said another problem is following line: string == "4-4" Without going deep(for more read this): When you use == you would usually compare by value. But Strings are Object so calling == on a object would compare it reference rather than its value. In Java Strings are special. Therefore it depends on how they initialized what == returns. So just use

string.equals("4-4") 

if you want to compare the value of a String