Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

actionPerfomed processing not executing.... 1

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello everyone,

I have a peculiar problem in (as I described in the subject line)that actionPerformed method is not excuting .

I have no errors during compilation and when a click on a button, nothing happens.

Here's my version of actionPerfomed....

public class ItsMe extends JFrame
implements ActionListener
{
......

/* ---- declare JButtons ------- */

private JButton myButton1;
private JButton myButton2;
private JButton myButton3;
private JButton myButton4;

.......



/* ---- My Constructor ------- */

public ItsMe()
{
..........
JButton myButton1 = new JButton("Button 1");
myButton1.addActionListener(this);
...........

JButton myButton2 = new JButton("Button 2");
myButton2.addActionListener(this);
...........

JButton myButton3 = new JButton("Button 3");
myButton4.addActionListener(this);
...........

JButton myButton4 = new JButton("Button 4");
myButton4.addActionListener(this);
...........

} // end of my constructor




public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();

//If button 1 pressed . . .

if (source == myButton1)
{
recordCounter ++;
System.out.println("You have clicked on Button 1. Counter hit is: " + recordcounter );
......
}


//If button 2 pressed . . .

if (source == myButton2)
{
recordCounter ++;
System.out.println("You have clicked on Button 2. Counter hit is: " + recordcounter );
......
}


//If button 3 pressed . . .

if (source == myButton3)
{
recordCounter ++;
System.out.println("You have clicked on Button 3. Counter hit is: " + recordcounter );
......
}


//If button 4 pressed . . .

if (source == myButton4)
{
recordCounter ++;
System.out.println("You have clicked on Button 4. Counter hit is: " + recordcounter );
......
}

} // end of actionPerformed method


.....


} // end of my ItsMe class


Any ideas?

Thank you,

Christopher

P.S. I understand there are alternate ways of doing this but this makes it clearer for me to read. :)


 
Hi asiavoices,

You have accidently created a new JButton reference in the constructor.

See for example:
private JButton myButton1;
and
JButton myButton1 = new JButton("Button 1");

When you do this, the scope of the myButton1 object that you initialized in the constructor is only the constructor area. The myButton1 object that you have defined as a before has a different reference, and that object is null.

So this is fixed like this:

private JButton myButton1;

public ItsMe()
{
myButton1 = new JButton("Button 1");
}

...and it should work fine :)

-Vepo
 
Wow! thanks Vepo...

I appreciate your help. forgot about the scope part. Now that I know, I hope to remember it ;-)

Oh yeah, I almost forgot... how do you check for a minumum no. of characters in one text field (i.e. Name) and a minumum of 9 digits for a numeric field (ie. cost)?

I have another question but will post it separately from this thread.

Its regarding...

How can I display my recordset, created from an ArrayList into a JPanel I've created? I'd like to have the people be ale to scroll through the list if possible.

For eample, I'm thinking of putting the recordset into the JScrollPane and then putting it into the Panel.

=========================
= My list =
=========================
= =
= item1 cost taxID =
= item2 cost taxID =
= ............ =
= =
= =
=========================

The list is inside a JScrollPane.

How do I go about doing that? I know you can put a Vector into a JScrollPane but "not " an ArrayList.

Any ideas?

Thanks,



 
Hi,

You can get the textField's length like this:
sometextfield.getText().length();

-Vepo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top