Results 1 to 8 of 8

Thread: Event handling

  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default Event handling

    i have a JTextField - nameEntryField
    another JTextField - nameDisplayField
    and a JButton - enterButton.

    when a user enters their name in the nameEntryField and clicks enter, the name should then be displayed in the nameDisplayField. in my actionPerformed method i can't get this to happen. is their a particular method like clickEvent(), or something similar to do this?

    maybe something like:

    Java Code:
    public void actionPerformed(ActionEvent e) 
    	{
    		if (enterButton.[some method](true))
    		{
    			nameDisplayField = nameEntryField;
    		}
    	}
    is that the right way to go about this? if not, could someone please explain the correct way.

    thanks

    ps, i've added an actionListener to enterButton and nameEntryField.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Quote Originally Posted by smallmos1 View Post
    is that the right way to go about this? if not, could someone please explain the correct way.
    No. That probably won't change anything.

    I think you're looking for something more like the setText() method. Check out the API for JTextField: JTextField (Java Platform SE 6)

  3. #3
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    i've tried the setText method in this way:

    nameDisplayField.setText(nameEntryField);

    but i'm being prompted to change to setCaret() or setToolTipText();

    do i have to change the data stored in name entry field to a String then pass it through the setText() method?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    The setText() method takes a String parameter. You're giving it a JTextField. You want to give it that JTextField's text. Hint: do you see a way to get the text of a JTextField?

  5. #5
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    something like String name = nameEntryField.toString();
    then, nameDisplayField.setText(name); ??

  6. #6
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    ok that seems to be kind of working, but whats being displayed in the absolute garbage....

    javax.swing.JTextField[,57,26,169x20,layout=javax.swing.plaf.basic.BasicT extUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0, border=javax.swing.plaf.BorderUIResource$CompoundB orderUIResource@2f93c0cf,flags=296,maximumSize=,mi nimumSize=,preferredSize=,caretColor=sun.swing.Pri ntColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=15,columnWidth=11,command=,horizontalAlig nment=LEADING]
    i have no idea where this is coming from!

    i'll post my code there could be an error that i havn't noticed

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class MyTextField extends JFrame implements ActionListener
    {
    	private JLabel enterNameLabel;
    	private JButton enterButton;
    	private JTextField nameEntryField, nameDisplayField;
    	private Container  contentPane;
    	
    	public MyTextField()
    	{
    		contentPane = getContentPane();
    		
    		setTitle("Name Entry");
    		setSize(250,175);
    		setResizable(true);
    		setLocation(150, 250);
    		
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		setLayout(new FlowLayout());
    		contentPane.setBackground(Color.lightGray);
    		
    		enterNameLabel = new JLabel("Please enter your name");
    		enterNameLabel.setSize(100, 30);
    		add(enterNameLabel);
    		
    		this.nameEntryField = new JTextField(15);
    		add(nameEntryField);
    		
    		this.nameDisplayField = new JTextField(15);
    		add(nameDisplayField);
    		
    		this.enterButton = new JButton("Enter");
    		enterButton.setSize(80, 30);
    		add(enterButton);
    		
    		enterButton.addActionListener(this);
    		nameEntryField.addActionListener(this);
    		nameDisplayField.addActionListener(this);
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		String name = nameEntryField.toString();
    		
    			nameDisplayField.setText(name);
    	}
    	
    	public static void main(String[] args)
    	{
    		MyTextField firstTextField = new MyTextField();
    		firstTextField.setVisible(true);
    	}
    
    }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by smallmos1 View Post
    ok that seems to be kind of working, but whats being displayed in the absolute garbage.... i have no idea where this is coming from!
    I do; that's no garbage, that's how a JTextField displays itself in text form: it shows you everything that can be important to you except the text it contains. When you want to set the text of a JTextfield a to a JTextField b you have to get the text from a and set it to be the text contained by b, so:

    Java Code:
    b.setText(a.getText());
    and indeed you can do that in yout button's event listener.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Event Handling
    By zzpprk in forum AWT / Swing
    Replies: 2
    Last Post: 11-12-2009, 09:30 PM
  2. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:46 PM
  3. rmi and event handling
    By darkhorse in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 08:20 AM
  4. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:21 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 06:05 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
  •