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?