• Post Reply Bookmark Topic Watch Topic
  • New Topic

A urgent help with jtextfield--correction updated

 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i have a simple application
a text box(JTextField) and a submitbutton(JButton), what i m trying to do is a user enters his name in the textfield, then onclicking the button the userentered value(content entered by user) should be stored or printed ..i dont know where im going wron so im attaching my code for ur review pls tell me wat to do , thanks in advance for ur support
public class Anmeldung extends JFrame implements ActionListener{
JTextField usertext;
JButton submitbutton;
public Exhibit(){
setTitle("XLedig" );
setSize(360,280);
JPanel content = new JPanel();
JLabel userlabel = new JLabel("UserName");
content.add(userlabel);
JTextField usertext = new JTextField(10);
content.add(usertext);
usertext.addActionListener(this);
....then some code
now my method ..
public void actionPerformed(java.awt.event.ActionEvent ae)
{
Object str = ae.getSource();
if(str.equals("submit"))
{
try{

System.out.println(usertext.getText());
}
catch(Exception e)
{
}
}
}
[ September 06, 2002: Message edited by: vivek sivakumar ]
[ September 06, 2002: Message edited by: vivek sivakumar ]
[ September 06, 2002: Message edited by: vivek sivakumar ]
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is kind of hard to see everything that is wrong when you put this together by cutting and pasting from parts of your code.
But it looks like you have defined your textfield as both an instance variable and local variable. You are setting the listener on the textfield and not the button but appear to be checking if the button was clicked.
You might want to post your whole code and use the code tags the little code button at the bottom to format it.
 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thankx a lot for ur response here is my full code , as i said before when i press the submit button all the values entered on the textfields should be printed out how can i do this???
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make your textfields instance variables. You don't need/want to set the listener on them only the button. Then just put
System.out.println(yourTextfield.getText());
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!