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 11 of 11

Thread: Help with event handling - beginner

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with event handling - beginner

    This is my code, i am construction a GUI straight from code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class Menu extends JFrame {
        private ImageIcon image; //variables
        private JLabel label1;
     
     
     
        Menu(){ //constructor
            setLayout(new FlowLayout());
     
            image = new ImageIcon(getClass().getResource("menu.png"));
            label1 = new JLabel(image);
            label1.setLocation(0, 0);
            add(label1);
     
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            label1.setPreferredSize(new Dimension(dim.width-100, dim.height-100));
     
     
            Button button = new Button("submit"); //THIS IS THE BUTTON I WISH TO HAVE AN ACTION PERFORMED EVENT FOR
            add(button);
            button.setSize(20, 50);
            button.setLocation(400,200);
            button.setVisible(true);
     
        }
     
     
     
        public static void main (String args[]){ //main entry point of program
            Menu gui = new Menu();
            gui.pack();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible((true));
            gui.setAlwaysOnTop(true);
            gui.setTitle("TITLE");
            gui.setResizable(false);
     
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            // Determine the new location of the window
            int w = gui.getSize().width;
            int h = gui.getSize().height;
            int x = (dim.width-w)/2;
            int y = (dim.height-h)/2;
     
            // Move the window
            gui.setLocation(x, y);
     
        }
     
    }


    I am having troubles understanding how to implement event handlers for my button "submit" or where to place the code. Can somebody give me some direction so that i might learn how to do this? I am struggling with it at the moment despite attempting multiple online tutorials and documentation. Much help is appreciated, please be gentle to the beginner!

    EDIT: I have commented the specific code that i wish to have an event attached to.
    Last edited by kyasuleyman; May 1st, 2012 at 11:42 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Help with event handling - beginner

    I have read this already but failed to understand it. I figured the best way to learn how to implement handlers in practice is by seeing an example of my code with an event handler.

    For example i do not know where or how to declare an event handler class, and whether or not that means actually creating a new class file to do so and then to reference it in my frame. Im clueless, yet it would help me in understanding this hugely if somebody could be so kind as to show this to me in practice in my own code.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with event handling - beginner

    You can do it a number of ways. Declare it as a separate class, or implement it using the same class, or instantiate an anonymous inner class. Try one, post what you've tried, and we'll go from there. The tutorial pretty much contains all the code you need.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with event handling - beginner

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class Menu extends JFrame {
        private ImageIcon image; //variables
        private JLabel label1;
     
     
     
        Menu(){ //constructor
            setLayout(new FlowLayout());
     
            image = new ImageIcon(getClass().getResource("menu.png"));
            label1 = new JLabel(image);
            label1.setLocation(0, 0);
            add(label1);
     
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            label1.setPreferredSize(new Dimension(dim.width-100, dim.height-100));
     
     
            Button button = new Button("sumbit");
            add(button);
            button.setSize(20, 50);
            button.setLocation(400,200);
            button.setVisible(true);
     
            button.addActionListener(new ActionListener(){ //EVENT HANDLER
     
    public void actionPerformed(ActionEvent ae){
                System.out.println("working?");
            }
    }); 
     
        }
     
     
     
        public static void main (String args[]){ //main entry point of program
            Menu gui = new Menu();
            gui.pack();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible((true));
            gui.setAlwaysOnTop(true);
            gui.setTitle("Star Alliance");
            gui.setResizable(false);
     
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            // Determine the new location of the window
            int w = gui.getSize().width;
            int h = gui.getSize().height;
            int x = (dim.width-w)/2;
            int y = (dim.height-h)/2;
     
            // Move the window
            gui.setLocation(x, y);
     
        }
     
    }

    This is one method i used, it works OK but i do not know if this is the proper way of doing it. And to be honest i copy and pasted that part from here -> java blog for beginners: Java swing event handling example programs for beginners so i dont actually understand how it links my button with that particular event, perhaps you could help me break it down and understand it? Thank you for your patience.

    I suppose the way it understands to link the event wıth that particular button is because the event happens in the addlistener bloc for the button, right? Why does that code end with this though "}); "? as opposed to just "}".
    Last edited by kyasuleyman; May 1st, 2012 at 12:25 PM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with event handling - beginner

    For more info on the underlying workings of Events, read the tutorials: Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And just look at how your parenthesis and brackets match up. Put each one on a separate line if you have to, with a comment explaining what is beginning or ending.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with event handling - beginner

    Believe me, i've read all of that, to no avail!

    Thanks for your help.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with event handling - beginner

    Quote Originally Posted by kyasuleyman View Post
    Believe me, i've read all of that, to no avail!
    Then you need to post exactly what you don't understand.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with event handling - beginner

    Quote Originally Posted by KevinWorkman View Post
    Then you need to post exactly what you don't understand.
    Well as you saw i eventually figured it out. But i need to know how i can use different events:
     button.addActionListener(new ActionListener(){ //EVENT HANDLER
     
    public void actionPerformed(ActionEvent ae){
                System.out.println("working?");
            }
    });
    I mean this is action performed, so basically when the button is clicked. What would i put for a key pressed event for example?

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with event handling - beginner

    Quote Originally Posted by kyasuleyman View Post
    I mean this is action performed, so basically when the button is clicked. What would i put for a key pressed event for example?
    I've asked you several times to read the tutorial. It contains information on many kinds of listeners, including KeyListeners and MouseListeners.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with event handling - beginner

    Also, the first result for googling "java key pressed event" is a part of the tutorial I already linked you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Event Handling - Best Practice?
    By ishtar in forum Java Theory & Questions
    Replies: 6
    Last Post: October 2nd, 2011, 08:52 AM
  2. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 03:29 AM
  3. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  4. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 10:29 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM