[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

how to access variable


hello there,

how can i get access to a variable anywhere in my class.i have a variable in a switch statement which i want to access.

cheers
zolf
0
zolf
Asked:
zolf
  • 13
  • 9
  • 8
  • +1
3 Solutions
 
CEHJCommented:
Make it an instance variable of the class
0
 
girionisCommented:
Hi zolf

variables in the switch statement block are local to this block, you cannot use them outside the block. I suggest you have an instance variable that you set accordingly in and out fo the switch statement.

Cheers
0
 
zolfAuthor Commented:

you'll mean define that variable outsite the constructor
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
girionisCommented:
Yes
0
 
CEHJCommented:
Yes

public class X {
    int instanceVar;
    public X(){}
}
0
 
zolfAuthor Commented:

i did that but it does not put the value outside it.that switch statement is inside a mouse listener
0
 
CEHJCommented:
You'll find it easier to make the listener non-anonymous

private class MyListener extends MouseAdapter {
...
}
0
 
girionisCommented:
Is your mouselistener class static? If yes you cannot do this, you will need to use the object's reference.
0
 
girionisCommented:
Can you post that bit of code you are trying to access?
0
 
zolfAuthor Commented:
0
 
CEHJCommented:
To use that, do

tree.addMouseListener(new MYListener());

then put the business code in

public void mousePressed(MouseEvent me) {
...
}

of MyListener
0
 
zolfAuthor Commented:

what do you mean
0
 
zolfAuthor Commented:

i dont want to change my code,because that will alter other things
0
 
girionisCommented:
> http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21913364.html

Ok which variable you need to access? Simpyl access it from inse the method mousePressed.
.
0
 
girionisCommented:
> inse

I mean inside
0
 
CEHJCommented:
You can save yourself a lot of hassle there by changing

>>Object uObject = select.getUserObject();

to

BaseRow br = (BaseRow)select.getUserObject();

and then using 'br'. Which variable in the switch do you mean?
0
 
CEHJCommented:
>>i dont want to change my code,because that will alter other things

You'll have to change your code anyway or it won't work
0
 
zolfAuthor Commented:


its this label = getImage(fk); which gets the image for the tree node clicked.but then how do i put that in the panel
suppose i pu the

label = getImage(29); inside the mousepressed,then how do i update my panel which i build below in the constr.i still have to put the label variable inside the

JScrollPane jspMap = new JScrollPane(label,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


please helpppp.

public JLabel getImage(int fkey)
      {
            ImageIcon iIcon = DBImage.dbImage(fkey);
            JLabel label = new JLabel(iIcon);
            return label;
      }
0
 
zolfAuthor Commented:

i am trying to get the fk variable
0
 
girionisCommented:
Put all the variables you need to have access to, *outside* any methods/constructors.
0
 
girionisCommented:
> i am trying to get the fk variable

Where is the "fk" declared?
0
 
girionisCommented:
This

>  fk = ((BaseRow)uObject).getForeignKey();

should be able to get the "fk" if it is declared as instance variable.
0
 
zolfAuthor Commented:

i declared fk outside the constr,right on top of the class,so it acts global.
0
 
zolfAuthor Commented:

yes that is right
0
 
zolfAuthor Commented:

i think the problem is updating the panel which contains the image.i.e everytime the node is clicked the panel should change the image.in my case the jtabbedpane just loads once when the application starts.then when i click on a node to show the image,it does not do anything.

0
 
zolfAuthor Commented:

in the mouse listenser i need to update the JScrollPane jspMap.how do i do that.
0
 
CEHJCommented:
>>i think the problem is updating the panel which contains the image.

You need to be clear what the problem is. The ability to declare variables and access them is a compilation/code issue, not a functionality/execution one
0
 
objectsCommented:
you need to updatre the image displayed by your label using the setIcon() method
0
 
objectsCommented:
so in your listener find the required image, and call setIcon() on the label that is in your tabbed pane
0
 
objectsCommented:
> i declared fk outside the constr,right on top of the class,so it acts global.

thats unnecessary from what you've said, sounds like it can just be a local var in your listener.
Depends on wjhether you need to remember the value *after* the mouse event has been processedd. If its just needed by the mouse listener then no need.
0
 
zolfAuthor Commented:

thankssssssssssss objects.it workedddd.
0
 
CEHJCommented:
>>it workedddd.

That's good, but how is this connected to the question of the title?
0
 
zolfAuthor Commented:

yes i did not know what my problem was,i thought may me it was variable problem.but in my comment i referred to another link which tells in more details my original problem.anyway thanks everybody for your help.
0
 
objectsCommented:
> thankssssssssssss objects.it workedddd.

woo hoo :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 13
  • 9
  • 8
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now