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

Thread: adding text to JLabel during actionPerformed

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default adding text to JLabel during actionPerformed

    Ok so I want the user to enter their name and age and when they press a JButton the program sends a welcome message to the user "Welcome first name last name"

    here is my code:
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class message extends JFrame implements ActionListener
    {
    	JLabel Lname, Fname, Mname, Welcome;
    	JTextField Field1, Field2, Field3;
    	JButton click;
     
    	public message()
    	{
    		setLayout(new FlowLayout(FlowLayout.LEFT));
     
    		Lname = new JLabel("Last Name");
    		Field1 = new JTextField(15);
    		Fname = new JLabel("First Name");
    		Field2 = new JTextField(10);
    		Mname = new JLabel("Age");
    		Field3 = new JTextField(3);
    		click = new JButton("Click When Done");
    		Welcome = new JLabel();
     
    		click.addActionListener(this);
     
    		this.add(Lname);
    		this.add(Field1);
    		this.add(Fname);
    		this.add(Field2);
    		this.add(Mname);
    		this.add(Field3);
    		this.add(click);
    		this.add(Welcome);
    	}
     
    	public static void main(String[] args) 
    	{
    		message frame = new message();
    		frame.setSize(300, 300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		Welcome.setText("Welcome " + Field2 + " " + Field1);
    	}
     
    }

    The actionPerformed method isnt working properly, it sends a message like "Welcome javax.swing......" instead of "Welcome John Smith"


  2. #2
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: adding text to JLabel during actionPerformed

    ok got it...
    instead of
    Welcome.setText("Welcome " + Field2 + " " + Field1);

    I should have had:
    Welcome.setText("Welcome " + Field2.getText() + " " + Field1.getText());

Similar Threads

  1. [SOLVED] Adding an object to Arraylist from text files
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: January 18th, 2012, 09:27 AM
  2. Adding data to a text box on another class
    By Qualitystreet in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2011, 02:38 AM
  3. Adding entered text from one GUI to another
    By fride360 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 24th, 2011, 01:08 PM
  4. [SOLVED] Storing info into a text file & adding to it
    By BlackFlame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 09:06 PM
  5. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM