Calling a class from a function?

Hi have a function with the following code:

public void itemStateChanged(ItemEvent evt)

{

if ( evt.getItemSelectable() == m_oval )

{

JOptionPane.showMessageDialog(this, "U selected the oval radio button.");

m_bSelectOval = true

}

class StringCatcher extends KeyAdapter

{

public void keyPressed(KeyEvent evt)

{

if(m_bSelectOval || m_bSelectSquare || m_bSelectTriangle)

{

if(( temp >= KeyEvent . VK_0 ) && ( temp <= KeyEvent . VK_9 ) && (temp == KeyEvent . VK_ENTER) )

{

JOptionPane.showMessageDialog(mainWindow, "U selected just hit enter.");

}

}

}

}

}

I have a list of radio buttons, one of them is called m_oval . Once that is selected a message appears wich works fine and it is then set to true. Now once it is set to true i want control to go to my string catcher class and check if one of the radios is true go into my iner if, this will check if a user enters a value between 1-9 and hits enter, a message should appear. However, this not appearing. I beilve it could be because i am not calling the string catcher from my itemstate changed method. So my question is how to i call(move control) exactly to the string catcher class to do its magic.Thanks guys

Message was edited by:

nvidia1

[1377 byte] By [nvidia1a] at [2007-11-26 18:37:06]
# 1
Use a reference for the StringCatcher class inside the class implementing the listener. Thats all you need to do.
qUesT_foR_knOwLeDgea at 2007-7-9 6:11:11 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi have a function with the following code:

>

> public void itemStateChanged(ItemEvent evt)

> {

>

> if ( evt.getItemSelectable() == m_oval )

> {

> OptionPane.showMessageDialog(this, "U selected the

> oval radio button.");

> m_bSelectOval = true

> }

>

> class StringCatcher extends KeyAdapter

> {

> public void keyPressed(KeyEvent evt)

> {

> f(m_bSelectOval || m_bSelectSquare ||

> m_bSelectTriangle)

> {

> if(( temp >= KeyEvent . VK_0 ) && ( temp <= KeyEvent

> . VK_9 ) && (temp == KeyEvent . VK_ENTER) )

>

> JOptionPane.showMessageDialog(mainWindow, "U

> selected just hit enter.");

>

> }

>

> }

>

>

> }

>

>

> I have a list of radio buttons, one of them is called

> m_oval . Once that is selected a message appears wich

> works fine and it is then set to true. Now once it is

> set to true i want control to go to my string catcher

> class and check if one of the radios is true go into

> my iner if, this will check if a user enters a value

> between 1-9 and hits enter, a message should appear.

> However, this not appearing. I beilve it could be

> because i am not calling the string catcher from my

> itemstate changed method. So my question is how to i

> call(move control) exactly to the string catcher

> class to do its magic.Thanks guys

>

> Message was edited by:

> nvidia1

Take a look at this first of all.

import javax.swing.*;

public class DoubleTrouble

{

public static void main(String[] args)

{

JRadioButton button = new JRadioButton();

if (button.isSelected())

{

System.out.println("I dont need an extra boolean switch because there is an isSelected method");

}

}

}

also can I ask why you are using two classes to process the input?

kikemellya at 2007-7-9 6:11:11 > top of Java-index,Java Essentials,Java Programming...
# 3

Regarding the first post: Can you explain to me what you mean about the referencing, if that is u don't mind ofcourse as i would be much appreciated.

Regarding the second post:

Yes of course, basically i am using a two inputs, one is for radio selection i.e oval

the second one is for a user to then select how many ovals they wish to use, after i expect them to hit enter to start my game and do other stuff. Hope that answers u'r question:)

nvidia1a at 2007-7-9 6:11:11 > top of Java-index,Java Essentials,Java Programming...
# 4

> Regarding the first post: Can you explain to me what

> you mean about the referencing, if that is u don't

> mind ofcourse as i would be much appreciated.

>

> Regarding the second post:

> Yes of course, basically i am using a two inputs,

> one is for radio selection i.e oval

> he second one is for a user to then select how many

> ovals they wish to use, after i expect them to hit

> enter to start my game and do other stuff. Hope that

> answers u'r question:)

Hi,

Why I asked is I can not see a need for two sepearte class files when it would work fine in one and solve your problem of calling other classes.

You asked about calling the other class.

you would need to create an instance of the class to be called in the calling class like so

ClassIWantToCall someClass = new ClassIWantToCall();

someClass.SomeMethodInSomeClass(someValue);

use the above to talk to other classes. You create a copy (reference/instance) of that class then use the instance variable name and then its method that you want to call, and pass in values if needed like below.

someClass.SomeMethodInSomeClass();

or if they you wanted to pass in some data when calling the method, maybe a String value of some text then use

someClass.SomeMethodInSomeClass("A String to process based on the event type");

and in your other class:

public ClassIWantToCall

{

SomeMethodInSomeClass(String s)

{

//do stuff with the string passed in

}

}

Message was edited by:

kikemelly

kikemellya at 2007-7-9 6:11:11 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi i am using a string catcher object

public StringCatcher s = new StringCatcher();

and i have modified my itemstate change method

public void itemStateChanged(ItemEvent evt)

{

if ( evt.getItemSelectable() == m_oval )

{

JOptionPane.showMessageDialog(this, "U selected the oval radio button.");

m_bSelectOval = true;

s.keyPressed(m_oval);

}

class StringCatcher extends KeyAdapter

{

public void keyPressed(KeyEvent evt) {

if(m_bSelectOval || m_bSelectSquare || m_bSelectTriangle)

{

if(( temp >= KeyEvent . VK_0 ) && ( temp <= KeyEvent . VK_9 ) && (temp == KeyEvent . VK_ENTER) )

{

JOptionPane.showMessageDialog(mainWindow, "U selected just hit enter.");

}

}

}

}

}

When i compile the code i get the following error message:

cannot find symbol method KeyPressed(java.awt.Checkbox).

As you can see i am trying to send the value of the oval to my string catcher. So that when the oval radio button is selected, the user then types in a value in my text field to say how many they want to use. After the user hits enter and it then goes into my first + second if statment.

Again, thx for posting up the object reference post, much apprecaited.

nvidia1a at 2007-7-9 6:11:11 > top of Java-index,Java Essentials,Java Programming...