3

I have these line of code and I want to disable the button after a passenger has been added. I want to disable the button. seats[i].setEnabled(false) won't work since it's inside an anonymous inner class.

JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
    seats[i] = new JButton();//creating the buttons
    seats[i].setPreferredSize(new Dimension(50,25));//button width
    panel4seating.add(seats[i]);//adding the buttons to the panels
    final int seatingID = i;  // Create a local final variable so it can be passed to the anonymous innerClass...

    seats[i].addActionListener(new ActionListener()
    {  //anonymous inner class
        public void actionPerformed(ActionEvent evt)
        {  
            String firstName = showInputDialog();
            String lastName = showInputDialog();

            sw101.AddPassenger(firstName, lastName, seatingID);//adding a pasenger

            //I want to add a line here that disables the button.
        }
    });
}
HoLyVieR
  • 10,985
  • 5
  • 42
  • 67
dave
  • 419
  • 1
  • 5
  • 10

4 Answers4

3

Because when you do:

setEnabled(false);

inside the anonymous inner class, you're calling that method on the ActionListener instance. not the JButton.

Try this:

JButton [] seats = new JButton[40];

for (int i = 0; i < 40; i++)
{
    final JButton seat = new JButton();
    final int seatingID = i;

    seats[i] = seat;
    seat.setPreferredSize(new Dimension(50,25));
    panel4seating.add(seat);

    seat.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt)
        {  
            String firstName = showInputDialog();
            String lastName = showInputDialog();

            sw101.AddPassenger(firstName, lastName, seatingID);

            seat.setEnabled(false);
        }
    });
}
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
2

One way is:

((JButton)ae.getSource()).setEnabled(false);:

E.G.

Here is an SSCCE based on your earlier code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GuiCreator extends JFrame
{
    public GuiCreator()
    {
        super("Seats");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.add(new SeatingPanel());

        pack();

        setVisible(true);
    }

    public static void main(String[] args) {
        new GuiCreator();
    }
}

class SeatListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent ae) {
        //String firstName = showInputDialog();
        //String lastName = showInputDialog();

        //sw101.AddPassenger(firstName, lastName, seatingID);//adding a passenger
        ((JButton)ae.getSource()).setEnabled(false);
    }

    public String showInputDialog() {
        return JOptionPane.showInputDialog(null, "Enter Data");
    }
}

class SeatingPanel extends JPanel
{
    public SeatingPanel()
    {
        super(new BorderLayout());

        JPanel panel4seating = new JPanel();//creating a grid panel
        panel4seating.setLayout(new GridLayout(4, 10));//setting the layout of the grid panel

        JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
        ActionListener listener = new SeatListener();
        for (int i = 0; i < 40; i++)
        {
            seats[i] = new JButton();//creating the buttons
            //better to set the preferred size of the button
            seats[i].setPreferredSize(new Dimension(50,25));
            panel4seating.add(seats[i]);
            seats[i].addActionListener(listener);
        }

        add(panel4seating, BorderLayout.CENTER);
    }
}

Screenshot

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • thank you very much it works. the stackoverflow community is so helpful. again thakn you all for your help. I should start contributing in my area of expertise, answering simple questions :) – dave Jun 12 '11 at 10:32
1

Try to export it as a class field, it should work that way:

public class Whatever {

private JButton [] seats;
private function whastsUpDude() {
seats = new JButton [40]; //creating a pointer to the buttonsArray
        for (int i = 0; i < 40; i++)
        {
            seats[i] = new JButton();//creating the buttons
            seats[i].setPreferredSize(new Dimension(50,25));//button width
            panel4seating.add(seats[i]);//adding the buttons to the panels
            final int seatingID = i;  // Create a local final variable so it can be passed to the anonymous innerClass...

            seats[i].addActionListener(new ActionListener()
             {  //anonymous inner class
                public void actionPerformed(ActionEvent evt)
                {  
                    String firstName = showInputDialog();
                    String lastName = showInputDialog();

                    sw101.AddPassenger(firstName, lastName, seatingID);//adding a passenger

                    //I want to add a line here that disables the button.
                }
             });
}
Protostome
  • 5,569
  • 5
  • 30
  • 45
0

Try seats[i].setEnabled(false);

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • It wont work because its inside an anonymous inner class it wont accept any variables that are not final – dave Jun 12 '11 at 09:58
  • I don't feel this answer deserved a down vote. Could you add the 'comment' you made to dave on the answer of Protostome, as an edit to your answer? AFAIK it is a 'correct' answer, but just making some different (and probably better) design assumptions. – Andrew Thompson Jun 12 '11 at 11:48