codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  actionPerformed not activated???  ericwi at 09:25 on Thursday, April 06, 2006
 

Hi all,

I have written a code to allow user to enter a value and calculate the value in another measurement unit.


import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.awt.event.*;

public class MetricConverter extends JFrame implements ActionListener
{
JPanel panel = (JPanel)this.getContentPane();
JPanel panel1 = new JPanel();
JButton ButtonToLeft = new JButton("<");
JButton ButtonToRight = new JButton(">");
GridBagLayout gridlayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JTextArea cmTextBox = new JTextArea();
JTextArea InchesTextBox = new JTextArea();
double CmNumber = 0.0;
double InchesNumber = 0.0;


TextField nameField = new TextField();


public static void main(String[] args)
{
new MetricConverter();
}

public MetricConverter()
{
setTitle(" Metric Converter");
panel.setLayout(null);
setSize(400,180);
setVisible(true);


//CM TextEnter box
JTextArea cmTextBox = new JTextArea();
cmTextBox.setBounds( 20,50,50,20);
panel.add(cmTextBox);

//Box1 attributes
JLabel jlabelBox1 = new JLabel("cm");
jlabelBox1.setBounds(80,50,40,20);
panel.add(jlabelBox1);

//Box2 attributes
JLabel jlabelBox2 = new JLabel("inches");
jlabelBox2.setBounds(300,50,40,20);
panel.add(jlabelBox2);

//Inches TextEnter box
JTextArea InchesTextBox = new JTextArea();
InchesTextBox.setBounds( 240,50,50,20);
panel.add(InchesTextBox);

// Layout for the left and right buttons
panel1.setBounds(150,45,50,30);
panel1.setLayout(gridlayout);
constraints.fill = GridBagConstraints.BOTH;

//leftarrow button
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
panel1.add(new JButton(">"), constraints);
ButtonToLeft.addActionListener(this);

// rightarrow button
constraints.gridx = 0;
constraints.gridy = 1;
panel1.add(new JButton("<"), constraints);
panel.add(panel1);
ButtonToRight.addActionListener(this);


} //MetricConverter statement


public void actionPerformed(ActionEvent evt)
{
if ( evt.getSource() == ButtonToLeft )
{
CmNumber = InchesNumber * 2.54;
nameField.setText("CM =" + CmNumber);
}
else if ( evt.getSource() == ButtonToRight )
{
InchesNumber = CmNumber / 2.54;
nameField.setText("Inches =" + InchesNumber);
}
}

} //Class statement



After I have hit the '>' or '<' button, the function addPerformed does not run. Does anyone know why?

Thanks

  Re: actionPerformed not activated???  crwood at 19:12 on Thursday, April 06, 2006
 

This

panel1.add(new JButton(">"), constraints);
ButtonToLeft.addActionListener(this);

adds a new JButton to the panel. This new JButton has no ActionListener added to it so there is no one to listen for it to send events. You probably meant to add the ButtonToLeft to the panel instead

panel1.add(ButtonToLeft, constraints);
ButtonToLeft.addActionListener(this);

You do have an ActionListener listeneing to this JButton for events.

The call to "setVisible" should be the last statement in your constructor. Adding components to the JFrame after calling "setVisible" doesn't work. The content pane lays out all the added components when you call "pack" or "setVisible"; if you add more after calling "setVisible" it has no idea. So call "setVisible" at the end and the content pane can lay everything out properly.

panel1.add(ButtonToRight,constraints);
panel.add(panel1);
ButtonToRight.addActionListener(this);
setVisible(true);


  Re: actionPerformed not activated???  ericwi at 09:32 on Friday, April 07, 2006
 

Hi crwood ,

Thanks for your help. Appreciated. Now, after I enter the number and click the button , it display another window about 'java.lang.NumberFormatException: empty String'. Is it that I did not initialise the getText?


Thanks again


  Re: actionPerformed not activated???  crwood at 16:31 on Friday, April 07, 2006
 

You enter a number and the parse exception says that the input was an "empty string". There is one thing in the code you posted that may be involved in this. You declared two JTextAreas as instance variables in class scope

public class MetricConverter extends JFrame implements ActionListener
{
...
JTextArea cmTextBox = new JTextArea();
JTextArea InchesTextBox = new JTextArea();

and then declare them again as local variables inside the class constructor.

public MC()
{
...
//CM TextEnter box
JTextArea cmTextBox = new JTextArea();
...
//Inches TextEnter box
JTextArea InchesTextBox = new JTextArea();

These last two are the ones that will appear in the content pane of the JFrame. But as local variables no one else in the MetricConversion class can see them. They can see the two instance variables but these are not the ones that are displayed. The way to do this is to declare the JTextAreas in class scope as instance variables and instantiate them in the constructor. Then they are added to the content pane and everyone in the class can see them

public class MetricConverter extends JFrame implements ActionListener
{
...
JTextArea cmTextBox; // declarations
JTextArea InchesTextBox; // in class scope

public MC()
{
...
//CM TextEnter box
cmTextBox = new JTextArea(); // instantiation
...
//Inches TextEnter box
InchesTextBox = new JTextArea();

This may be causing/contributing to the trouble with the "empty string" parse exception.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  jslider - paint track
•  Re: convert minutes into hours and minutes
•  File handle problem
•  Link Capture, help needed
•  Re: java app auto web update..
•  IE Only comeback
•  ` Error Code 64: Host not available ` - Error when accessing ASP Appln
•  Re: dynamic crystal report generation
•  Launch an app remotely through a website


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007