Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I invoke a method to an ActionListener

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I invoke a method to an ActionListener

    Hi, I'm new to Java and I am trying to write the code required for function roll to be called when rollButton is pressed and I have no idea how to do this. So I would appreciate if someone could help me. Thanks

    i
    mport javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;
     
     
    [CODE][CODE][CODE][CODE]public class Dice extends JFrame implements ActionListener {
        private JButton rollButton;
        private JLabel leftDice,rightDice;
        private Random randomGen;
        public Dice()   {
            super("Dice machine");
            rollButton= new JButton("Roll!");
            leftDice=new JLabel("",JLabel.CENTER);
            rightDice=new JLabel("",JLabel.CENTER);
            Container pane=this.getContentPane();
            pane.add(leftDice,BorderLayout.NORTH);
            pane.add(rightDice,BorderLayout.CENTER);
            pane.add(rollButton,BorderLayout.SOUTH);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,100);
            setVisible(true);
            randomGen=new Random(); // init number generator
            roll();
    }
        private void roll() {
            // generate random numbers between 1 and 6
            int left=randomGen.nextInt(5)+1;
            int right=randomGen.nextInt(5)+1;
            leftDice.setText(""+left);
            rightDice.setText(""+right);
    }
     
        public void actionPerformed(ActionEvent e){
         //not sure what to write here exactly
     
     
        }
        public static void main (String[] args) {
            new Dice();
    [/CODE][/CODE][/CODE][/CODE]

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How do I invoke a method to an ActionListener

    You don't invoke an actionListener directly. It is done for you. Here is an example.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ActionListenerDemo implements ActionListener {
       JFrame frame	= new JFrame("ActionLIstener Demo");
       JPanel panel	= new JPanel();
     
       public static void main(String[] args) {
    	  SwingUtilities.invokeLater(() -> new ActionListenerDemo());
       }
     
       public ActionListenerDemo() {
    	  panel.setPreferredSize(new Dimension(400, 200));
    	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  frame.add(panel);
    	  JButton button = new JButton("Button1");
              // listener using method - supported from the beginning.
    	  button.addActionListener(this);
              // listener using lambdas - supported in java 8+
    	  button.addActionListener(
    	        (ae) -> System.out.println("Button pressed - handled from lambda"));
    	  panel.add(button);
     
    	  frame.pack();
    	  frame.setLocationRelativeTo(null); // center on screen
    	  frame.setVisible(true);
       }
     
       public void actionPerformed(ActionEvent ae) {
    	  System.out.println("Button pressed - handled from method");
       }
    }

    The code that goes in the listener is what you want the button to do.

    Regards,
    Jim

Similar Threads

  1. API to Invoke the TERMINAL in UBUNTU
    By amitection in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 7th, 2014, 03:17 PM
  2. Invoke methods
    By wdh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2012, 08:17 AM
  3. method not passing across actionlistener interface
    By flamewolf393 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 16th, 2011, 07:16 AM
  4. Timer expires invoke method
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2011, 05:52 PM
  5. how to invoke a method?????
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 11:48 PM

Tags for this Thread