Enum problem ,Can't able to access in another classs of differrent package
Can anyone tell me how to access enum constants in another package...
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Declare them public, import the package, refer to them by fully-qualified name.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
well ,
I declared below in coursebookie package ..
public class GlobalVariables {
public static enum BetfairStatus{NOT_CONNECTED, CONNECTED};
}
and i want to access in
import coursebookie.presentation.BookiesFrame;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import coursebookie.GlobalVariables.BetfairStatus;
import coursebookie.GlobalVariables;
import coursebookie.*;
/**
*
* @author Dale
*/
public class BetfairLogoutAction extends AbstractAction{
public BetfairLogoutAction(String text, String desc) {
super(text);
putValue(SHORT_DESCRIPTION, desc);
}
public BetfairLogoutAction(String text, String desc, Integer mnemonic) {
super(text);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e)
{
if (GlobalVariables.BetfairStatus = CONNECTED)
{
}
if (GlobalVariables.BetfairStatus = NOT_CONNECTED) {
JOptionPane.showMessageDialog(null,"logged out of betfair" );
}
}
}
but it says error at
GlobalVariables.BetfairStatus that BetfairStatus is not delcared
please help me out..
i think i declared public and all...
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
You are trying to use the enum as if it is a variable. Your code should look more like this...
BetfairStatus status = ...
if (status == BetfairStatus.CONNECTED) ...
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
BetfairStatus status = ...
if (status == BetfairStatus.CONNECTED) ...
should i make object of BetfairStatus enum ...but it cannot be instatiated...error comes what to use after
BetfairStatus status = what i can write here
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
I really don't approve of global variables. IMHO you should always make variables private and use get/set methods. But if that's the way you want to do it...
In your globals you need the enum to define the possible values, plus another global variable that holds the current value:
public static enum BetfairStatus{NOT_CONNECTED, CONNECTED};
public static BetfairStatus status;
then, in the other modules:
GlobalVariables.status = BetfairStatus.CONNECTED;
or
if (GlobalVariables.status == BetfairStatus.CONNECTED) ...
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Thanks it solved my problem..
James ..you rock!!!!!!
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Please mark thread as solved, so everyone else knows not to try to asnswer it! J
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
//logoutActions
logout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (GlobalVariables.status == BetfairStatus.CONNECTED)
{
//try{
// GlobalAPI.logout(null);}
//catch(Exception k)
//{}
GlobalVariables.status = BetfairStatus.NOT_CONNECTED;
}
else if(GlobalVariables.status == BetfairStatus.NOT_CONNECTED)
{
JOptionPane.showMessageDialog(null, "You are not logged in to betfair");
}
}
});
I am applying this to logout Jmenuitem ..nothing Action is happening ..
why..
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Add some print statements so you can see what is (or is not) happening exactly.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
it is not going in any if statement...I am not getting any error in any of if statements ..but when i print outside if statement it prints...
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Try printning GlobalVariables.status, maybe you haven't initialsed it?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
public void actionPerformed(ActionEvent e)
{
System.out.println(GlobalVariables.BetfairStatus.CONNECTED);
if(GlobalVariables.status == BetfairStatus.NOT_CONNECTED)
{
System.out.println("hi to all");
JOptionPane.showMessageDialog(null, "You are not logged in to betfair");
}
}
i did above and it is printing connected..
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
The above code not printing "hi to all" in print statement
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
You didn't read my post properly. GlobalVariables.BetfairStatus.CONNECTED is NOT waht I said to print.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
did i need to intialise..
public static enum BetfairStatus{NOT_CONNECTED, CONNECTED};
this ..
amit.hak50
Junior Poster in Training
65 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
One last time: it's the variable GlobalVariables.status that you need to look at. Your enum is OK.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073