Solved

ActionListener & Integer.parseInt Question

Posted on 2001-08-30
12
437 Views
Last Modified: 2008-03-17
Hello experts,

I made a simple applet just to learn but ran into a problem. I commented out the parts that fails the action listener for the textboxes. What I had in mind was for a person to enter in values and then set two variables equal them and then once they click the buton it would repaint the images in a new location. Also the compiler complained about the use of :
x = Integer.parseInt( textField1.getText() );
Why? Thanks for the help


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

public class CarButton extends Applet
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);
     private int count = 0;

     public void init()
     {
          //Create user input boxes
          Label labelOne = new Label( "Enter x value" );
          TextField textField1 = new TextField(2);
          textField1.setText( "" );
          //textField1.addActionListener( new TextHandler1() );

          Label labelTwo = new Label( "Enter Y value" );
          TextField textField2 = new TextField(2);
          textField2.setText( "" );
          //textField2.addActionListener( new TextHandler2() );

          //Create button
          button.addActionListener( new ButtonHandler() );

          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     //public class TextHandler1 implements ActionListener
     //{
          //x = Integer.parseInt( textField1.getText() );
     //}
         
     //public class TextHandler2 implements ActionListener
     //{
          //y = Integer.parseInt( textField2.getText() );    
     //}

     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               repaint();
          }
     }

     public void paint (Graphics g)
     {
          //Begin to draw car image.
          g.setColor(Color.black);
          //Strait verticle lines top down
          g.drawLine(60+x,70+y, 120+x,70+y);
          g.drawLine(65+x,75+y, 120+x,75+y);
          g.drawLine(10+x,100+y, 45+x,100+y);
          g.drawLine(50+x,100+y, 135+x,100+y);
          g.drawLine(5+x,130+y, 20+x,130+y);
          g.drawLine(50+x,130+y, 150+x,130+y);
          g.drawLine(180+x,130+y, 190+x,130+y);

          //Horizontal lines
          g.drawLine(90+x,70+y, 90+x,130+y);
          g.drawLine(140+x,100+y, 140+x,130+y);

          //Slanted lines
          g.drawLine(10+x,100+y, 5+x,130+y);
          g.drawLine(60+x,70+y, 45+x,100+y);
          g.drawLine(65+x,75+y, 50+x,100+y);
          g.drawLine(120+x,75+y, 135+x,100+y);
          g.drawLine(120+x,70+y, 140+x,100+y);
          g.drawLine(140+x,100+y, 180+x,105+y);
          g.drawLine(180+x,105+y, 190+x,130+y);
         
          //Circles for the car wheels
          g.drawOval(20+x,115+y, 30,30);
          g.drawOval(150+x,115+y, 30,30);
     }
}
0
Comment
Question by:MrError
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 1

Expert Comment

by:kanthonym
ID: 6442149
You may want to beging by doing a Trim() in the input from the text box. This will reduce the chance of bad data going to the ParseInt method which will throw and exception.  This is not likley to be the solution to your question but could help avoid another issue.

textField1.getText().trim()

BTW -  Are you including the java.lang.Integer although the parseint is a static method and it should not be required.
0
 
LVL 4

Expert Comment

by:pellep
ID: 6442163
parseInt() throws NumberFormatException. You need to enclose it in a try/catch block.
0
 
LVL 4

Expert Comment

by:pellep
ID: 6442173
parseInt() throws NumberFormatException. You need to enclose it in a try/catch block.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MrError
ID: 6442505
OK pellep & kanthonym

I copied part of the code to show you what I tried...
Here is the errors I get when compiling:

C:\Inetpub\wwwroot\ObjClass\assignment1>javac CarButton.java
CarButton.java:45: illegal start of type
                try
                ^
CarButton.java:52: <identifier> expected
                }
                 ^
CarButton.java:43: CarButton.TextHandler1 should be declared abstract; it does n
ot define actionPerformed(java.awt.event.ActionEvent) in CarButton.TextHandler1
        public class TextHandler1 implements ActionListener
               ^
3 errors

Here is the code that I modified:

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

public class CarButton extends Applet
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);
     private int count = 0;

     public void init()
     {
          //Create user input boxes
          Label labelOne = new Label( "Enter x value" );
          TextField textField1 = new TextField(2);
          textField1.setText( "" );
     textField1.addActionListener( new TextHandler1() );

          Label labelTwo = new Label( "Enter Y value" );
          TextField textField2 = new TextField(2);
          textField2.setText( "" );
          //textField2.addActionListener( new TextHandler2() );

          //Create button
          button.addActionListener( new ButtonHandler() );
          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     public class TextHandler1 implements ActionListener
     {
          try
          {
               x = Integer.parseInt( textField1.getText().trim() );
          }
          catch (NumberFormatException e)
          {
               e.printStackTrace();
          }
     }
0
 

Author Comment

by:MrError
ID: 6442937
Well OK I got this to compile now but it does not seem to move my Car image... Any Ideas?

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

public class CarButton extends Applet implements ActionListener
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     //Create label and text boxes
     TextField textField1 = new TextField(2);
     TextField textField2 = new TextField(2);
     Label labelOne = new Label( "Enter x value" );
     Label labelTwo = new Label( "Enter Y value" );
     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);

     public void init()
     {
          //Create user input boxes
          textField1.setText( "" );
          textField1.addActionListener( this );

          textField2.setText( "" );
          textField2.addActionListener( this );

          //Create button
          button.addActionListener( new ButtonHandler() );

          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     public void actionPerformed (ActionEvent event)
     {
          x = Integer.parseInt( textField1.getText() );
          y = Integer.parseInt( textField2.getText() );
     }

     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               repaint();
          }
     }

     public void paint (Graphics g)
     {
          //Begin to draw car image.
          g.setColor(Color.red);
          //Strait verticle lines top down
          g.drawLine(60+x,70+y, 120+x,70+y);
          g.drawLine(65+x,75+y, 120+x,75+y);
          g.drawLine(10+x,100+y, 45+x,100+y);
          g.drawLine(50+x,100+y, 135+x,100+y);
          g.drawLine(5+x,130+y, 20+x,130+y);
          g.drawLine(50+x,130+y, 150+x,130+y);
          g.drawLine(180+x,130+y, 190+x,130+y);
          //Horizontal lines
          g.drawLine(90+x,70+y, 90+x,130+y);
          g.drawLine(140+x,100+y, 140+x,130+y);
          //Slanted lines
          g.drawLine(10+x,100+y, 5+x,130+y);
          g.drawLine(60+x,70+y, 45+x,100+y);
          g.drawLine(65+x,75+y, 50+x,100+y);
          g.drawLine(120+x,75+y, 135+x,100+y);
          g.drawLine(120+x,70+y, 140+x,100+y);
          g.drawLine(140+x,100+y, 180+x,105+y);
          g.drawLine(180+x,105+y, 190+x,130+y);
         
          //Circles for the car wheels
          g.drawOval(20+x,115+y, 30,30);
          g.drawOval(150+x,115+y, 30,30);
     }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6443008
The action listener on your text field is only invoked when you hit <enter> after entering the value.
If you don't hit enter it is never called.
In addition the way you've currently got it the button is not really required you could just have the label action listeners call repaint:

    public void actionPerformed (ActionEvent event)
    {
         x = Integer.parseInt( textField1.getText() );
         y = Integer.parseInt( textField2.getText() );
         repaint();
    }

This sill requires <enter> to be pressed in your text field.

Alternatively, you could remove the action listeners from your text fields and instead use the above actionPerformed method as your button listener.
This no longer requires <enter> to be pressed in the text field, but instead requires the button to be pressed to move the image.
0
 

Author Comment

by:MrError
ID: 6443074
Objects...

Points for you, Can you help me with one last idea. I want to set the color value using a text box. So I have these elements to deal with:

Label labelThree = new Label( "Enter Color" );
TextField textField3 = new TextField(5);

z = textField3.getString(); <--- how to get the value?
g.setColor(Color.z);  <--- I know that is wrong.


Thanks everyone!!!
0
 
LVL 92

Expert Comment

by:objects
ID: 6443102
The easiest way is to have them enter the colour as a six digit hex value (the same as done in html). Where the value represents rrggbb.

Label labelThree = new Label( "Enter Color" );
TextField textField3 = new TextField(5);

z = textField3.getString(); <--- rrggbb value
int hex = Integer.parseInt(z, 16);
g.setColor(new Color(hex));  <--- I know this works :)

Alternatively if you want to use colour names, then you'll need to define your own lut of colour names, and corresponing colout.


0
 

Author Comment

by:MrError
ID: 6443153
Hex values sound fine to me...

C:\Inetpub\wwwroot\ObjClass\assignment1>javac CarButton.java
CarButton.java:65: cannot resolve symbol
symbol  : method getString  ()
location: class java.awt.TextField
                        z = textField3.getString();
                                      ^
CarButton.java:66: cannot resolve symbol
symbol  : method parseInt  (int,int)
location: class java.lang.Integer
                        hex = Integer.parseInt(z, 16);
                                     ^
2 errors


int z = 0;
int hex = 0;


     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               x = Integer.parseInt( textField1.getText() );
                  y = Integer.parseInt( textField2.getText() );
               z = textField3.getString();
               hex = Integer.parseInt(z, 16);
                  repaint();
          }
     }

     public void paint (Graphics g)
     {
         
          g.setColor(Color.green);

          if ( hex != 0 )
          {
               g.setColor(new Color(hex));  
          }
0
 
LVL 92

Expert Comment

by:objects
ID: 6443162
Sorry, I just cut&pasted your code:

String rrggbb = textField3.getText(); <--- rrggbb value
int hex = Integer.parseInt(rrggbb, 16);


0
 

Author Comment

by:MrError
ID: 6443233
Thanks...

You helped a lot.
0
 
LVL 92

Expert Comment

by:objects
ID: 6443435
no worries.
thanks for the points :)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question