Results 1 to 8 of 8
Thread: Event handling
- 11-24-2010, 06:16 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
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:
is that the right way to go about this? if not, could someone please explain the correct way.Java Code:public void actionPerformed(ActionEvent e) { if (enterButton.[some method](true)) { nameDisplayField = nameEntryField; } }
thanks
ps, i've added an actionListener to enterButton and nameEntryField.
- 11-24-2010, 06:18 PM #2
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)
- 11-24-2010, 06:30 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
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?
- 11-24-2010, 06:36 PM #4
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?
- 11-24-2010, 07:24 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
something like String name = nameEntryField.toString();
then, nameDisplayField.setText(name); ??
- 11-24-2010, 07:38 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
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!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'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); } }
- 11-24-2010, 08:11 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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:
and indeed you can do that in yout button's event listener.Java Code:b.setText(a.getText());
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-24-2010, 08:22 PM #8
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Event Handling
By zzpprk in forum AWT / SwingReplies: 2Last Post: 11-12-2009, 09:30 PM -
Event handling... help?
By kevzspeare in forum New To JavaReplies: 2Last Post: 04-04-2009, 08:46 PM -
rmi and event handling
By darkhorse in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 08:20 AM -
SWT Event Handling
By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM -
Event Handling
By luisarca in forum Sun Java Wireless ToolkitReplies: 5Last Post: 05-07-2007, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks