Can anyone tell me how to access enum constants in another package...

Recommended Answers

All 16 Replies

Declare them public, import the package, refer to them by fully-qualified name.

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...

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) ...

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

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) ...

Thanks it solved my problem..

James ..you rock!!!!!!

Please mark thread as solved, so everyone else knows not to try to asnswer it! J

//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..

Add some print statements so you can see what is (or is not) happening exactly.

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...

Try printning GlobalVariables.status, maybe you haven't initialsed it?

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..

The above code not printing "hi to all" in print statement

You didn't read my post properly. GlobalVariables.BetfairStatus.CONNECTED is NOT waht I said to print.

did i need to intialise..

public static enum BetfairStatus{NOT_CONNECTED, CONNECTED};
this ..

One last time: it's the variable GlobalVariables.status that you need to look at. Your enum is OK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.