Results 1 to 5 of 5
  1. #1
    michaelxiao is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Exclamation Please help on Button action

    I just wrote this program that want to show names and phone number in a text area by click a button.
    Well, I kind of forgot how to set this action.
    It will be very helpful if someone could help me with it.
    Tell me how to write the only thing left for this program.
    Thank you!

    This is my code:

    import javax.swing.*;
    import java.awt.*;
    import java.text.*;
    import java.awt.event.*;

    public class NameProject extends JFrame
    {
    private JLabel namePromptLabel;
    private JLabel firstNLabel;
    private JTextField firstName;
    private JLabel midNLabel;
    private JTextField midName;
    private JLabel lastNLabel;
    private JTextField lastName;
    private JLabel phoneNLabel;
    private JTextField phoneNumber;
    private JButton displayButton;
    private JTextArea displayArea;

    public NameProject()
    {
    createUserInterface();
    }
    private void createUserInterface()
    {
    Container contentPane = getContentPane();
    contentPane.setLayout( null );

    namePromptLabel = new JLabel();
    namePromptLabel.setBounds( 5, 19, 260, 28 );
    namePromptLabel.setText("Please enter the following information");
    namePromptLabel.setFont(new Font( "Default", Font.PLAIN, 15 ) );
    namePromptLabel.setHorizontalAlignment(JLabel.CENT ER );
    contentPane.add( namePromptLabel );

    firstNLabel = new JLabel();
    firstNLabel.setBounds( 16, 65, 91, 21 );
    firstNLabel.setText( "First name:" );
    contentPane.add(firstNLabel);

    firstName = new JTextField();
    firstName.setBounds( 132, 65, 117, 21 );
    contentPane.add(firstName);

    midNLabel = new JLabel();
    midNLabel.setBounds( 16, 112, 122, 24 );
    midNLabel.setText( "Middle inital:" );
    contentPane.add( midNLabel );

    midName = new JTextField();
    midName.setBounds( 132, 112, 117, 21 );
    contentPane.add(midName);

    lastNLabel = new JLabel();
    lastNLabel.setBounds(16, 159, 122, 24);
    lastNLabel.setText( "Last name:" );
    contentPane.add(lastNLabel);

    lastName = new JTextField();
    lastName.setBounds( 132, 159, 117, 21 );
    contentPane.add(lastName);

    phoneNLabel = new JLabel();
    phoneNLabel.setBounds( 16, 206, 122, 24 );
    phoneNLabel.setText( "Phone number:" );
    contentPane.add(phoneNLabel);

    phoneNumber = new JTextField();
    phoneNumber.setBounds( 132, 206, 117, 21 );
    contentPane.add(phoneNumber);

    JPanel panel = new JPanel();
    JTextArea displayArea = new JTextArea(5,20);
    panel.add(displayArea);
    displayArea.setEditable( false );
    panel.setBounds( 1, 256, 260, 60);
    contentPane.add(panel);

    displayButton = new JButton();
    displayButton.setBounds( 155, 320, 94, 24 );
    displayButton.setText( "Display" );
    contentPane.add( displayButton );
    displayButton.addActionListener(
    new ActionListener()
    {
    public void actionPerformed( ActionEvent event )
    {
    displayButtonActionPerformed( event );
    }
    } );

    setTitle("Name Project");
    setSize( 280, 400 );
    setVisible( true );
    }

    private void displayButtonActionPerformed( ActionEvent event )
    {
    Start from here, I couldn't figure it out!!! Please help!!!
    String name = firstName.getText();
    displayArea.setText( name );
    }

    public static void main( String[] args )
    {
    NameProject application = new NameProject();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You instanciate a local JtextArea in createUserInterface() the instance variable is still null
    Replace
    Java Code:
    JTextArea displayArea = new JTextArea(5, 20);
    with
    Java Code:
    displayArea = new JTextArea(5, 20);

  3. #3
    michaelxiao is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    thank you!!!

  4. #4
    conv_bay is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    ur work alredy finish?

  5. #5
    michaelxiao is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. action class
    By jayagowri in forum Web Frameworks
    Replies: 1
    Last Post: 05-07-2008, 04:23 PM
  2. Run RCP action from outside of the Eclipse RCP
    By leonbandas in forum Eclipse
    Replies: 1
    Last Post: 01-07-2008, 08:15 AM
  3. jsp:forward action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 11:04 AM
  4. jsp:param action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 11:03 AM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 11:19 AM

Posting Permissions

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