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

Thread: Java Learning Help: JOptionPane / Listener

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java Learning Help: JOptionPane / Listener

    Hey,

    I'm trying to learn Java (The GUI part right now), but my code won't do as intetden.
    What it should do:
    When I press enter in a field, then show the dialog (See fat text in code (Can't use FAT in code, so look at the [/B])

    My Problem
    It will only show the message in the password-part (Which I think is stupid)


    Code:

    public class EHTwo extends JFrame{
     
    	private JTextField itemOne;
    	private JTextField itemTwo;
    	private JTextField itemThree;
    	private JPasswordField passwordField;
     
    	public EHTwo(){
     
    		super("The Title");
    		setLayout(new FlowLayout());
     
    		//10 is the max length.
    		itemOne = new JTextField(10);
     
    		//This is like a PROMT
    		itemTwo = new JTextField("TRY ENTER!");
     
    		itemThree = new JTextField("Uneditebale", 20);
    		//This makes sure that the user can't edit it.
    		itemThree.setEditable(false);
     
    		//mypass is the default password
    		passwordField = new JPasswordField("mypass");
     
    		add(itemOne);
    		add(itemTwo);
    		add(itemThree);
    		add(passwordField);
     
     
    		TheHandler handler = new TheHandler();
     
     
    		itemOne.addActionListener(handler);
    		itemTwo.addActionListener(handler);
    		itemThree.addActionListener(handler);
    		passwordField.addActionListener(handler);
     
    	}
     
     
     
    	//When a class is inside another class, then it will INHERITANCE all the stuff form the first.
    	private class TheHandler implements ActionListener{
     
     
    		//This has to be name actionPerformed. (ActionEvent) is the type. event is the name.
    		public void actionPerformed(ActionEvent event){
     
    			//This is the final string we output.
    			String string = "";
     
     
     
    			[B]//This is to check which box is clicked.[/B]
    			if(event.getSource()==itemOne){
    				//This is going to set the string.
    				string=String.format("Field One %s", event.getActionCommand());		//event.getActionCommand() means that it is gonna get the information form the "box" /point /object it's at.
    			}
    			else if(event.getSource()==itemTwo){
    				string=String.format("Field Two %s", event.getActionCommand());
    			}
    			else if(event.getSource()==itemThree){
    				string=String.format("Field Three %s", event.getActionCommand());
    			}
    			else if(event.getSource()==passwordField){
    				string=String.format("Password Field is: %s", event.getActionCommand());
     
    			JOptionPane.showMessageDialog(null, string);
     
    			}
     
    		}
     
    	}

    Thanks, I hope that you can read the code, if not then message me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Java Learning Help: JOptionPane / Listener

    It will only show the message in the password-part
    The order that the code is executed is controlled by the {}s following the if statements.
    Move the code outside of the enclosing {}s to get it to execute after the last if. As it is now, it only executes when the last if is true.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Nicken99 (March 16th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: Java Learning Help: JOptionPane / Listener

    I recommend that you have a much better understanding of the basics before beginning your GUI study.

Similar Threads

  1. learning Java
    By stanley in forum Member Introductions
    Replies: 1
    Last Post: December 8th, 2013, 04:08 PM
  2. learning java
    By javaman93 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 13th, 2013, 11:52 AM
  3. I'm just learning Java
    By michael.duffy31 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 5th, 2013, 10:27 AM
  4. Need some help, learning java!
    By thisguyrighthere in forum Object Oriented Programming
    Replies: 5
    Last Post: June 10th, 2012, 09:41 PM
  5. Learning Java
    By jgc1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 6th, 2011, 06:17 PM

Tags for this Thread