0

I have been creating a system by which I am listing various seats on a train within a GUI. I had decided to use a simple grid layout with numbered JButtons representing the seats and the seatNo and empty JLabels to represent empty space (I realise this may be trivial but it was simple). I have used a Gridlayout and I have created the Buttons and Labels as shown below.

I have an List (compArray2) in a separate class that consists of 0s and 1s and a direction that represent where there should be a seat and where the shouldn't be a seat. the .getNum gets the value either 0 for empty space or 1 for a seat and the .getDir gets the direction the seat will be facing.

public JPanel rowPanelCreation(int type, int rowNo, int width){

    theNoOfSeats=0;
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0,width));

    List<seatLayout> layout = new ArrayList<>();
    layout = ModelConstants.compArray2;


    for (seatLayout isit : x2){

        buttonEna = new JButton();
            buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
            buttonEna.setBorderPainted(false);
        buttonDis = new JLabel();

        if (isit.getNum()==0){
            if (isit.getDir()=='x'){
            panel.add(buttonDis);
            }
        }

        else if (isit.getNum()==1){

            theNoOfSeats++;

            if (isit.getDir()=='N'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultN.png"); 
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='E'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultE.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                buttonEna.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){JOptionPane.showMessageDialog(null,"You have chosen : ");}});
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='S'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultS.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='W'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultW.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
        }
    }
    return panel;
}

the main problem I am having is that when I press one of the seat buttons I want a message to appear telling me the seatNo of the seat I have selected is. However when I do this it simply displays the very last seatNo. Is there a better way I could do this ? I'm assuming that as all the buttons are fundamentally the same, there is no way to differentiate between them ?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Tomousee
  • 23
  • 1
  • 5
  • All this code does is creates the buttons. If you're having trouble with some action, wouldn't it be relevant to show your `actionPerformed`? Or is this where you think the error is occurring? – Paul Samsotha Jan 05 '14 at 02:34

1 Answers1

3

"I want a message to appear telling me the seatNo of the seat I have selected is. However when I do this it simply displays the very last seatNo."

Here's your problem. It's a reference problem. In the end, all the buttons will have the same JButton and JLabel reference. So all the buttons and labels will get the last button and label reference created, hence "displays the very last seatNo."

buttonEna = new JButton();
buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
buttonEna.setBorderPainted(false);
buttonDis = new JLabel();

You need to create a new JButton and new JLabel reference each iteration

JBUtton buttonEna = new JButton();
buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
buttonEna.setBorderPainted(false);
JLabel buttonDis = new JLabel();

Another approach would be to have an array of JButton, loop to set the buttons, and add them to the Panel. Say you have a GridLayout(2, 2). You then would need a array of 4 JButtons

JButton[] buttons = new JButton[9];
...
for (int i = 0; i < 4; i++){
    buttons[i] = new JButton();
    buttons[i].setFont(new Font("Sans Serif", Font.BOLD , 14));
    buttonEna.setBorderPainted(false);
    // set other properties for button
    panel.add(buttons[i]);
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720