|
-
Apr 4th, 2003, 05:03 PM
#1
Thread Starter
Addicted Member
readfrom TextField writeto TextArea *resolved*
What i wish to do is have a TextField that user can enter name and on ActionPerformed(buttonclick) display that name in a TextArea.
Do I use keyboard class to read the TextField or is there a simpler way?
Any advice or sample code appreciated
Last edited by mbonfyre; Apr 7th, 2003 at 10:59 AM.
-
Apr 6th, 2003, 03:37 PM
#2
Lively Member
Add an actionlistener to a button, and in the actionPerformed() get the value of the textfield (textfield.getText()), and put it in the Textarea.
Or did I misunderstand the problem?
-
Apr 7th, 2003, 01:03 AM
#3
Thread Starter
Addicted Member
ok, so I think that I have done as you suggested:
Code:
public void actionPerformed(ActionEvent e){
String vfName=JTFfName.getText();
TADisplay.setText(vfName);
}
which to ME says: put the text from TextField(JTFfName) into a variable called vfName. Set the text of TextArea(TADisplay) to that of vfName.
When I typed in my name (sarah) into TextField (JTFfName) and clicked JButSubmit, nothing appeared in the TextArea(TADisplay), but the DOS window running beehind the application displayed the following errors:
>
at javax.swin.plaf.BasicButtonListener.MouseReleased<Basic Button listener.java.216>
at java.awt.Component.processMouseEvent<Component.java.3715>
at java.awt.Component.processEvent<Component.java.3544>
at java.awt.Container.processEvent<Container.java.1164>
at java.awt.Component.dispatchEventImpl<Component.java.2593>
at java.awt.Container.dispatchEventImpl<Container.java.1213>
at java.awt.Component.dispatchEvent<Component.java..2497>
at java.awt.LightweightDispatcher.retargetMouseEvent<Container.java.2451>
at java.awt.LightweightDispatcher.processMousEvent<Container.java.2216>
at java.awt.LightweightDispatcher.dispatchEvent<Container.java.2125>
at java.awt.Container.dispatchEventImpl<Container.java.1200>
at java.awtWindow.dispatchEvent<Window.java.926>
at java.awt.Component.dispathEvent<Component.java.2497>
at java.awt.EventQueue.dispatchEvent<EventQueue.java.339>
at java.awt.EventDispatchThread.pumpOneEventForHierarchy<EventDispatchThread<EventDispatchThread.java.1 31>
at java.awt.EventDispatchThread.pumpOneEventForHierarchy<EventDispatchThread<EventDispatchThread.java.9 8>
at java.awt.EventDispatchThread.pumpEventsForHierarchy<EventDispatchThread<EventDispatchThread.java.93>
at java.awt.EventDispatchThread.run<EventDispatchThread<EventDispatchThread.java.85>
Have you any idea where I've gone wrong?
Here's my full code - if it helps(lines that errors are showing on marked by forward slashes):
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.border.*;
import javax.swing.JOptionPane; //Not all implemented yet!
public class RegistrationForm extends JFrame implements ActionListener, ItemListener {
boolean inAnApplet = true;
JComboBox cboMonth;
JComboBox cboDate;
JComboBox cboYear;
JTextField JTFfName;
JButton JButSubmit = new JButton("Submit");
JButton JButClear = new JButton("Clear");
JTextArea TADisplay = new JTextArea(10,15);
String regDate ="";
CheckboxGroup oneCBG;
Checkbox cbMale, cbFemale;
JCheckBox checkAer = new JCheckBox("Aerobics");
JCheckBox checkFB = new JCheckBox("Football");
JLabel fName;
public RegistrationForm() {
Container contentPane = getContentPane();
oneCBG = new CheckboxGroup(); //Create checkbox group for radio buttons
cbMale = new Checkbox("Male", false,oneCBG); //populate buttons
cbMale.addItemListener(this); //add itemListener for user interaction
cbFemale = new Checkbox("Female",false,oneCBG);
cbFemale.addItemListener(this);
JButSubmit.addActionListener(this);
contentPane.setLayout(new GridLayout(18,4)); //Define Layout for contentpane
JComboBox cboMonth =new JComboBox(); //Create combo box
cboMonth.addItem("January"); //populate combo box
cboMonth.addItem("February");
cboMonth.addItem("March");
cboMonth.addItem("April");
cboMonth.addItem("May");
cboMonth.addItem("June");
cboMonth.addItem("July");
cboMonth.addItem("August");
cboMonth.addItem("September");
cboMonth.addItem("October");
cboMonth.addItem("November");
cboMonth.addItem("December");
JComboBox cboDate =new JComboBox();
cboDate.addItem("1st");
cboDate.addItem("2nd");
cboDate.addItem("3rd");
cboDate.addItem("4th");
cboDate.addItem("5th");
cboDate.addItem("6th");
cboDate.addItem("7th");
cboDate.addItem("8th");
cboDate.addItem("9th");
cboDate.addItem("10th");
cboDate.addItem("11th");
cboDate.addItem("12th");
cboDate.addItem("13th");
cboDate.addItem("14th");
cboDate.addItem("15th");
cboDate.addItem("16th");
cboDate.addItem("17th");
cboDate.addItem("18th");
cboDate.addItem("19th");
cboDate.addItem("20th");
cboDate.addItem("21st");
cboDate.addItem("22nd"); //////////////////////////////////////////////////////////////
cboDate.addItem("23rd");
cboDate.addItem("24th");
cboDate.addItem("25th");
cboDate.addItem("26th");
cboDate.addItem("27th");
cboDate.addItem("28th");
cboDate.addItem("29th");
cboDate.addItem("30th"); ///////////////////////////////////////////////////
cboDate.addItem("31st");
JComboBox cboYear =new JComboBox();
cboYear.addItem("2003");
cboYear.addItem("2002"); /////////////////////////////////////////////////////////
cboYear.addItem("2001");
contentPane.add(new JLabel()); //space - probably not good programming practise, but it helps me for now!
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
JLabel fName = new JLabel("First Name");
contentPane.add(fName);
JTextField JTFfName = new JTextField();
contentPane.add(JTFfName);
contentPane.add(new JLabel("")); //space
contentPane.add(new JLabel("Registration Date:"));
contentPane.add(new JLabel("Surname"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(cboMonth);
contentPane.add(new JLabel("Street"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(cboDate);
contentPane.add(new JLabel("City"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(cboYear); //space
contentPane.add(new JLabel("County"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space //////////////////////////////////////////////////////////////////////////////
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel("Post Code"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel("Gender: ")); //space
contentPane.add(new JLabel("Telephone"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(cbMale);
contentPane.add(new JLabel("URL"));
contentPane.add(new JTextField());
contentPane.add(new JLabel()); //space
contentPane.add(cbFemale);
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel("Choose Sports: "));//space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel());//space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel());//space
contentPane.add(checkAer);
checkAer.addItemListener(this);
contentPane.add(new JLabel());//space
contentPane.add(checkFB);
checkFB.addItemListener(this);
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel());//space
contentPane.add(new JLabel());//space
contentPane.add(new JLabel());//space
contentPane.add(new JLabel());//space
contentPane.add(JButClear);
contentPane.add(JButSubmit);
JButSubmit.addActionListener(this);
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space
contentPane.add(new JLabel()); //space(
contentPane.add(new JLabel());//space
contentPane.add(new JLabel()); //space
contentPane.add(TADisplay); //space
}
public void actionPerformed(ActionEvent e){
//regDate += cboMonth.getSelectedItem()+ " ";
//TADisplay.setText(regDate);
//}
//{
String vfName=JTFfName.getText();
TADisplay.setText(vfName);
}
public void itemStateChanged(ItemEvent e) { //Responds to click event from Radio buttons and checkboxes
if (e.getItemSelectable() == cbMale) { // Radio Buttons for gender working - but are overwritten by choosing
TADisplay.setText("Male" + "\n"); //a checkbox further on in the form
}
if (e.getItemSelectable() == cbFemale) {
TADisplay.setText("Female");
}
if (e.getItemSelectable() ==checkAer) { //Check boxes working - but only one at a time (2ns choice wipes out first
TADisplay.setText("Aerobics");
}
////////////////////////////////////////////////////////////////////
if (e.getItemSelectable() ==checkFB) {
TADisplay.setText("Football");
}
}
public static void main(String args[]) { //
RegistrationForm window = new RegistrationForm();
window.inAnApplet = false;
window.setTitle("Registration"); //adds title to application bar
window.pack();
window.setVisible(true);
}
}
-
Apr 7th, 2003, 09:30 AM
#4
Lively Member
I ran your code, and exactly as you say, there's an error... NullPointerException at line 192, and that's where you use any method in the JTextField class for the first time.
If fixed this, with replacing line 107
JTextField JTFfName = new JTextField();
with
JTFfName = new JTextField();
You shouldn't write the classname before the variable again, since you've already done that one time (line 16). Don't really know the words for it, but I hope you know what I mean 
Try to remove all places where this is done, since this obviously causes problems. I saw it in a few places.
-
Apr 7th, 2003, 10:58 AM
#5
Thread Starter
Addicted Member
thank you soooooo much!
I will take your advice and check my code for repeating object names. Java is so sensitive, I'm sure I'll get the hang of it if I keep on trying ( and using this great forum when I get stuck)
thanks again, Sarah
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|