Results 1 to 6 of 6
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    127
    Rep Power
    0

    Default Can anyone give me a little bit of help?

    so i created this little GUI that lets you fill in some information, and does something when you hit enter.
    i am still learning java so i followed this guys's steps: thenewboston - YouTube
    the problem for me is: i created these text fields with "first name" in it etc. etc.
    when you click on one of those fields to fill in information you have to delete "first name" first.
    i want to make it so that when you click on the field, the text "first name" automatically disappears and you can fill in information.
    this is the code:

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    
    
    public class firstGUI extends JFrame{
    	
    	private JTextField HF1;
    	private JTextField HF2;
    	private JTextField HF3;
    	private JPasswordField passwordField;
    	
    	public firstGUI(){
    		super("User details");
    		setLayout(new FlowLayout());
    		HF1 = new JTextField("First name: ", 10);
    		add (HF1);
    		HF2 = new JTextField("Last name: ", 10);
    		add(HF2);
    		HF3 = new JTextField("Your age: ", 10);
    		HF3.setEditable(true);
    		add(HF3);
    		
    		passwordField = new JPasswordField("", 10);
    		add(passwordField);
    		
    		thehandler handler = new thehandler();
    		HF1.addActionListener(handler);
    		HF2.addActionListener(handler);
    		HF3.addActionListener(handler);
    		passwordField.addActionListener(handler);
    		
    		
    		
    	}
    	
    	
    	private class thehandler implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    			String string = "";
    			
    			if(event.getSource()==HF1)
    				string=String.format("Your first name is: %s", event.getActionCommand());
    			else if(event.getSource()==HF2)
    				string=String.format("Your last name is: %s", event.getActionCommand());
    			else if(event.getSource()==HF3)
    			string=String.format("Your age is:  %s", event.getActionCommand());
    			else if (event.getSource()==passwordField)
    				string=String.format("Your password is: %s", event.getActionCommand());
    			
    			JOptionPane.showMessageDialog(null, string);
    			
    		}
    	}
    	
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Can anyone give me a little bit of help?

    Why not just add a JLabel next to the field?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    127
    Rep Power
    0

    Default Re: Can anyone give me a little bit of help?

    haven't learned that yet, but ill make sure to check out as soon as i get back home.
    thanks for the idea :)

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Can anyone give me a little bit of help?

    There are several ways to implent this, but one thing you can do is add a FocusListener:
    Java Code:
            final JTextField f = new JTextField("First name");
            f.addFocusListener( new FocusListener() {
                public void focusGained(FocusEvent e) {
                    if ( f.getText().equals("First name")) {
                       f.setText("");
                    }
                }
    
                public void focusLost(FocusEvent e) {
                    if (f.getText().isEmpty()) {
                        f.setText("First name");
                    }
                }
            });
    The if-checks are in there to prevent erasing the user's input. Sort of like this, I guess. I haven't tested it :)
    Another improvement would be to make the text "First name" a variable so it's declared in a single place, not as three spearate Strings.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Can anyone give me a little bit of help?

    You've not met my friend, Mr F Name, have you?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Can anyone give me a little bit of help?

    Quote Originally Posted by Tolls View Post
    You've not met my friend, Mr F Name, have you?
    He lives next to Mr. L Name. :)

Similar Threads

  1. give me a code
    By florida in forum New To Java
    Replies: 12
    Last Post: 03-06-2013, 07:57 AM
  2. Can anyone give a suggestion for the following :
    By Manojx in forum New To Java
    Replies: 0
    Last Post: 03-16-2011, 06:41 PM
  3. how to give delay?
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-17-2008, 06:57 PM
  4. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •