I need help on making a button get a string from a text box!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: I need help on making a button get a string from a text box!

  1. #1
    Shawn Guest

    I need help on making a button get a string from a text box!


    I can't seem to get my button to work right. I am just starting Java and I
    am trying to get the button the retrieve the information from the text box
    and put it though the if else statement. I think the problem is that the
    button dosent know how to get the info from the text box. If anyone can help
    if you like you can email me.

    Here is the code:

    // import classes

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;



    public class Bible extends Applet implements ActionListener {

    Button button1;
    TextField input;
    TextField output;
    Label a;
    Label b;
    Font font1;

    public void init() {

    // create button
    button1 = new Button( "Press this suka!" );
    button1.addActionListener( this );
    button1.FocusListener( input );

    // create text fields
    input = new TextField( 2 );
    input.addActionListener( this );
    output = new TextField( 50 );
    output.setEditable( false );

    // create labels
    a = new Label( "Enter a number of 1!" );
    b = new Label( "Here is your verse:" );

    // add GUI components
    add( a );
    add( input );
    add( button1 );
    add( b );
    add( output );

    // set background color and font
    font1 = new Font( "SansSerif", Font.BOLD, 17 );
    setFont( font1 );
    setBackground( Color.blue );

    }

    public void actionPerformed( ActionEvent e )
    {
    String var = e.getActionCommand();


    // set text to nothing on run
    input.setText( "" );
    output.setText( "" );
    showStatus( "Bible applet!" );


    if ( var.charAt( 0 ) == '1' )
    output.setText( "Bible Verse 1" );


    else
    output.setText( "Only input number 1 please!" );


    }

    }



  2. #2
    Paul Clapham Guest

    Re: I need help on making a button get a string from a text box!

    What's in your field "var" at the time of the if-else? Put in
    "System.out.println(var)" to find out. If you want to get info from the
    text box, why not just get it from the text box: "input.getText()".

    Shawn <rusesh@earthlink.net> wrote in message
    news:38ff6000$1@news.devx.com...
    >
    > I can't seem to get my button to work right. I am just starting Java and I
    > am trying to get the button the retrieve the information from the text box
    > and put it though the if else statement. I think the problem is that the
    > button dosent know how to get the info from the text box. If anyone can

    help
    > if you like you can email me.
    >
    > Here is the code:
    >
    > // import classes
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import java.applet.*;
    >
    >
    >
    > public class Bible extends Applet implements ActionListener {
    >
    > Button button1;
    > TextField input;
    > TextField output;
    > Label a;
    > Label b;
    > Font font1;
    >
    > public void init() {
    >
    > // create button
    > button1 = new Button( "Press this suka!" );
    > button1.addActionListener( this );
    > button1.FocusListener( input );
    >
    > // create text fields
    > input = new TextField( 2 );
    > input.addActionListener( this );
    > output = new TextField( 50 );
    > output.setEditable( false );
    >
    > // create labels
    > a = new Label( "Enter a number of 1!" );
    > b = new Label( "Here is your verse:" );
    >
    > // add GUI components
    > add( a );
    > add( input );
    > add( button1 );
    > add( b );
    > add( output );
    >
    > // set background color and font
    > font1 = new Font( "SansSerif", Font.BOLD, 17 );
    > setFont( font1 );
    > setBackground( Color.blue );
    >
    > }
    >
    > public void actionPerformed( ActionEvent e )
    > {
    > String var = e.getActionCommand();
    >
    >
    > // set text to nothing on run
    > input.setText( "" );
    > output.setText( "" );
    > showStatus( "Bible applet!" );
    >
    >
    > if ( var.charAt( 0 ) == '1' )
    > output.setText( "Bible Verse 1" );
    >
    >
    > else
    > output.setText( "Only input number 1 please!" );
    >
    >
    > }
    >
    > }
    >
    >




  3. #3
    Ms. Ami Choksi Guest

    Re: I need help on making a button get a string from a text box!


    "Shawn" <rusesh@earthlink.net> wrote:
    >
    >I can't seem to get my button to work right. I am just starting Java and

    I
    >am trying to get the button the retrieve the information from the text box
    >and put it though the if else statement. I think the problem is that the
    >button dosent know how to get the info from the text box. If anyone can

    help
    >if you like you can email me.
    >
    >Here is the code:
    >
    >// import classes
    >
    >import java.awt.*;
    >import java.awt.event.*;
    >import java.applet.*;
    >
    >
    >
    >public class Bible extends Applet implements ActionListener {
    >
    > Button button1;
    > TextField input;
    > TextField output;
    > Label a;
    > Label b;
    > Font font1;
    >
    > public void init() {
    >
    > // create button
    > button1 = new Button( "Press this suka!" );
    > button1.addActionListener( this );
    > button1.FocusListener( input );
    >
    > // create text fields
    > input = new TextField( 2 );
    > input.addActionListener( this );
    > output = new TextField( 50 );
    > output.setEditable( false );
    >
    > // create labels
    > a = new Label( "Enter a number of 1!" );
    > b = new Label( "Here is your verse:" );
    >
    > // add GUI components
    > add( a );
    > add( input );
    > add( button1 );
    > add( b );
    > add( output );
    >
    > // set background color and font
    > font1 = new Font( "SansSerif", Font.BOLD, 17 );
    > setFont( font1 );
    > setBackground( Color.blue );
    >
    > }
    >
    > public void actionPerformed( ActionEvent e )
    > {
    > String var = e.getActionCommand();
    >
    >
    > // set text to nothing on run
    > input.setText( "" );
    > output.setText( "" );
    > showStatus( "Bible applet!" );
    >
    >
    > if ( var.charAt( 0 ) == '1' )
    > output.setText( "Bible Verse 1" );
    >
    >
    > else
    > output.setText( "Only input number 1 please!" );
    >
    >
    > }
    >
    >}
    >
    >



    Hello Shawn,
    See button.FocusListener is not there. It can be addFocusListener.
    Whatever it may be. See I am giving you the code bye which you will be able
    to do the things you want. O.K. Hope will help you.


    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    /*
    <applet code="tp.class" width=640 height=480>
    </applet>
    */
    public class tp extends Applet implements ActionListener
    {
    Label label1 ;
    Label label2 ;
    TextField textField1 ;
    TextField textField2 ;
    Button button1 ;

    public void init()
    {
    label1 = new Label("Enter a number of 1");
    add(label1);

    textField1 = new TextField(20);
    add(textField1);


    label2 = new Label("Here is your verse");
    add(label2);

    textField2 = new TextField(20);
    textField2.setEditable(false);
    add(textField2);

    button1 = new Button("Press this suka !");
    add(button1);
    button1.setBackground(Color.lightGray);

    button1.addActionListener(this);
    }




    public void actionPerformed(java.awt.event.ActionEvent event)
    {
    Object object = event.getSource();
    if (object == button1)
    button1_ActionPerformed(event);
    }



    void button1_ActionPerformed(java.awt.event.ActionEvent event)
    {
    String a = textField1.getText();
    textField2.setText(a);
    showStatus("Bible Applet !!! ");
    textField1.setText("");
    }
    }





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center