Alec
Alec

Reputation: 1

How to pass JButton to the Action Listener class

I'm a beginner programmer and I am trying to learn how to pass certain objects created in the main class to other classes (in this case the action listener class).

My question is - How can I pass the button to the action listener class? Here is my code snippet.

public class MaxMinProgram
{

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Max Min Program");
        GridLayout myLayout = new GridLayout(1,11);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int size = 11;
        JTextField[] fields = new JTextField[size];        
        JPanel panel = new JPanel();
        frame.setPreferredSize(new Dimension(500,110));
        frame.getContentPane().add(panel);
        int k = 0;
        for(k=0;k<fields.length;k++)
        {
            fields[k] = new JTextField("", 3);
            panel.add(fields[k]);
        }
        JButton button = new JButton("Randomize");
        JButton button2 = new JButton("Max Min");
        panel.add(button);
        panel.add(button2);
        frame.pack();
        frame.setVisible(true); 

    }
}

public class myListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent evt)
    {
        ActionListener clickListener = new myListener();
        button.addActionListener(clickListener);  //this is the line
        int [ ] numbers = new int [10];
        JTextField [] textFields;
        Random randomize = new Random();
        int x = randomize.nextInt(100);  
    }
}

Thank you very much for your help!

Upvotes: 0

Views: 1239

Answers (3)

Dev
Dev

Reputation: 3580

You add this line in your code after create the instance of button

button.addActionListener(new myListener());
button1.addActionListener(new myListener());

and remove the myListener class and add this class myListener class

 public class myListener implements ActionListener
  {
    @Override
   public void actionPerformed(ActionEvent evt)
    {
       int [ ] numbers = new int [10];
        JTextField [] textFields;
       Random randomize = new Random();
         int x = randomize.nextInt(100);  
     }
  }

Upvotes: 0

Jixi
Jixi

Reputation: 117

First you should move the whole thing to a constructor

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

public MaxMinProgram(){
    JFrame frame = new JFrame("Max Min Program");
    GridLayout myLayout = new GridLayout(1,11);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int size = 11;
    JTextField[] fields = new JTextField[size];        
    JPanel panel = new JPanel();
    frame.setPreferredSize(new Dimension(500,110));
    frame.getContentPane().add(panel);
    int k = 0;
    for(k=0;k<fields.length;k++)
    {
        fields[k] = new JTextField("", 3);
        panel.add(fields[k]);
    }
    JButton button = new JButton("Randomize");
    JButton button2 = new JButton("Max Min");
    panel.add(button);
    panel.add(button2);
    frame.pack();
    frame.setVisible(true); 
}

and then you can add the listener:

either

button.addActionListener(new myListener());

or

ActionListener listener = new myListener();
    button.addActionListener(listener);

in the constructor.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347184

Move

   ActionListener clickListener = new myListener();
   button.addActionListener(clickListener);  //this is the line

To your main method, for example

public static void main(String[] args)
{
    JFrame frame = new JFrame("Max Min Program");
    GridLayout myLayout = new GridLayout(1,11);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int size = 11;
    JTextField[] fields = new JTextField[size];        
    JPanel panel = new JPanel();
    frame.setPreferredSize(new Dimension(500,110));
    frame.getContentPane().add(panel);
    int k = 0;
    for(k=0;k<fields.length;k++)
    {
         fields[k] = new JTextField("", 3);
         panel.add(fields[k]);
    }
    JButton button = new JButton("Randomize");
    JButton button2 = new JButton("Max Min");

    ActionListener clickListener = new myListener();
    button.addActionListener(clickListener);  //this is the line

    panel.add(button);
    panel.add(button2);
    frame.pack();
    frame.setVisible(true); 

}

Upvotes: 2

Related Questions