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

Thread: Swing EventHandler Problem...

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Swing EventHandler Problem...

    I just recently got into coding and running applications using Java's Swing. While trying to get Event Handling and ActionListeners and whatnot to work, I ran into a problem and I cannot for the life of me figure it out. I was following TheNewBoston's video tutorial, and I went back and checked my code multiple times, and the process that I follow is exactly the same as his, but his ran and worked while mine ran but did not function properly. Was wondering if anybody could look over it and tell me what's wrong with it. Thanks!

    EventHandlerMain Source Code:

    import javax.swing.*;
    import java.awt.*;
     
     
    class EventHandlerMain {
        public static void main(String[] Args) {
    	EventHandlerSecondary gui = new EventHandlerSecondary();
     
    	gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	gui.setSize(350,150);
    	gui.setVisible(true);
        }
    }

    EventHandlerSecondary Source Code:

    import javax.swing.*;
    import java.awt.*;
     
    public class EventHandlerSecondary extends JFrame {
        private JTextField tf1;
        private JTextField tf2;
        private JTextField tf3;
        private JPasswordField pwf;
     
        public EventHandlerSecondary() {
    	super("Event Handling Test");
    	setLayout(new FlowLayout());
     
    	tf1 = new JTextField(10);
    	add(tf1);
     
    	tf2 = new JTextField("Enter Text Here...");
    	add(tf2);
     
    	tf3 = new JTextField("* Not Available *", 20);
    	add(tf3);
    	tf3.setEditable(false);
     
    	pwf = new JPasswordField("password");
    	add(pwf);
     
    	Handler eventHandler = new Handler();
    	tf1.addActionListener(eventHandler);
    	tf2.addActionListener(eventHandler);
    	tf3.addActionListener(eventHandler);
    	pwf.addActionListener(eventHandler);
        }
     
        private class Handler implements ActionListener {
    	public void actionPerformed(ActionEvent event) {
    	    String string = "";
     
    	    if(event.getSource() == tf1)
    		string = String.format("Field 1: %s", event.getActionCommand));
     
    	    else if(event.getSource() == tf2)
    		string = String.format("Field 2: %s", event.getActionCommand));
     
    	    else if(event.getSource() == tf3)
    		string = String.format("Field 3: %s", event.getActionCommand));
     
    	    else if(event.getSource() == pwf)
    		string = String.format("Password field is: %s", event.getActionCommand));
     
    	    JOptionPane.showMessageDialog(null, string);
     
    	}
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Swing EventHandler Problem...

    tell me what's wrong with it
    Can you explain what happens and why you think there is something wrong with the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing EventHandler Problem...

    Oh sorry, I thought that I mentioned it. It wasn't recognizing the event (when I hit enter in a text field, it wouldn't display the message) however, I found out what was wrong. I had to import java.awt.event.*, which confuses me. Does anybody know why the java.awt.* didn't automatically import the event files? I had though that java.awt.* would import all awt files.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Swing EventHandler Problem...

    java.awt.* would import all awt files.
    It would look in the java.awt folder to resolve class definitions. Not in folders in the awt folder.

    How are you compiling the code? Does it have compiler errors?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Swing EventHandler Problem...

    No, no. I just didn't understand how the importing works. The program works perfectly now, though. Thanks!

Similar Threads

  1. [SOLVED] Problem with image in swing gui
    By Pusillus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 14th, 2012, 11:13 AM
  2. IO-swing problem
    By haresh2906 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 07:00 AM
  3. [SOLVED] Problem with swing-buttons
    By Minken in forum AWT / Java Swing
    Replies: 8
    Last Post: October 15th, 2010, 09:20 PM
  4. [SOLVED] Java Swing problem?
    By nasser in forum AWT / Java Swing
    Replies: 2
    Last Post: July 3rd, 2010, 12:34 PM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM