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

Thread: JButton events

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JButton events

    Hi,
    Im still relatively new to GUI. Ive seen a lot of tutorials out there, but none of them really explain what im doing, and just show examples. Im trying to learn how to make a JButton do something when you click on it.
    Ive seen few examples that say to to make a new function like this:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){

    but i dont really understand how that helps. Could someone tell me how to give a JButton a function, and explain what is happening in the code.
    Thanks
    Pottsie


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton events

    Ive looked at it, but i havent been able to do what theyve done. I try this:

    import javax.swing.*;
    import java.awt.*;
     
    public class LessonOne {
     
        public static void main(String[] args) {
            GridLayout myLayout = new GridLayout(2, 3);
            JFrame frame = new JFrame( "Working with JButtons");
            frame.setSize(100,100);        
            JPanel myPanel = new JPanel();
            myPanel.setLayout(myLayout);
            JButton newButton = new JButton ("Click Me"); 
            newButton.addActionListener(this);
            newButton.setToolTipText("This button does nothing...");
            myPanel.add(newButton);
            frame.getContentPane().add(myPanel);
            frame.setVisible(true);
     
        }
     
         public void actionPerformed(ActionEvent e) {
            Toolkit.getDefaultToolkit().beep();
        }
    }

    and i get the error "cannot find symbol - class ActionEvent"

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JButton events

    You haven't told javac where to look for the class ActionEvent with an import statement. Have a look at the list of imports at the top of the Beeper.java example.

    http://download.oracle.com/javase/tu...nts/intro.html
    http://download.oracle.com/javase/tu...ts/Beeper.java

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton events

    Do i have to write the code the way they did, writing the class as an extension of JPanel? I added in the imports they had, and i get "non-static variable this cannot be referenced from a static context"...

  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: JButton events

    Quote Originally Posted by pottsiex5 View Post
    Do i have to write the code the way they did, writing the class as an extension of JPanel? I added in the imports they had, and i get "non-static variable this cannot be referenced from a static context"...
    You don't need to extend JPanel, but you do need an instance (I'm assuming of ActionListener) to pass into the method. Do that by implementing ActionListener in a class you actually instantiate or by using an anonymous inner class.
    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
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton events

    Hey,
    So i put implement ActionListener into the class line like this:

    public static void main implements ActionListener (String[] args) {

    but i get a red line under "main" and "implements ActionListener (String[] args)". I also tried putting it after (String[] args) but it gave me a red line under implements.

    So what i have all together is:

    import javax.swing.*;
    import java.awt.*;
     
    public class LessonOne {
     
    	public static void main (String[] args) implements ActionListener {
    		GridLayout myLayout = new GridLayout(2, 3);
    		JFrame frame = new JFrame( "Hello World");
    		frame.setSize(100,100);
    		JPanel myPanel = new JPanel();
    		myPanel.setLayout(myLayout);
    		JButton button = new JButton ("Hello");
    		startButton.addActionListener(this);
    		myPanel.add(button);
    		frame.getContentPane().add(myPanel);
    		frame.setVisible(true);
     
    	}
     
    	public void actionPerformed(ActionEvent a) {
    			System.out.println("hello");
    	}
     
    }

  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: JButton events

    Well, that's not valid syntax, and that's not what I suggested. Recommended reading: Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Edit- I see Sean4u already gave you this advice.
    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
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton events

    I cant find any syntax other than
    "public class ClassName extends JPanel
    implements ActionListener {"
    which doesnt help me...

  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: JButton events

    You could use an anonymous inner class, as stated above. Did you do any googling of that? Something like:

    ActionListener al = new ActionListener(){
       public void actionPerformed(ActionEvent e){
          System.out.println("clicked");
       }
    }
     
    button.addActionListener(al);
    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. Events on JOptionPane dialogs
    By beer-in-box in forum AWT / Java Swing
    Replies: 6
    Last Post: March 23rd, 2012, 01:33 AM
  2. [SOLVED] events log
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 17th, 2010, 05:21 AM
  3. Handling two button events?
    By BelowTheHeavens in forum AWT / Java Swing
    Replies: 2
    Last Post: May 15th, 2010, 01:35 AM
  4. JComboBoxes & Events
    By KevinGreen in forum AWT / Java Swing
    Replies: 3
    Last Post: April 21st, 2010, 05:11 AM
  5. Connecting events
    By Olufemi in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2010, 04:58 AM