Solved

how to access variable

Posted on 2006-07-10
34
247 Views
Last Modified: 2011-09-20

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
Comment
Question by:zolf
  • 13
  • 9
  • 8
  • +1
34 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071259
Make it an instance variable of the class
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071264
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
 

Author Comment

by:zolf
ID: 17071272

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!

 
LVL 35

Expert Comment

by:girionis
ID: 17071275
Yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071276
Yes

public class X {
    int instanceVar;
    public X(){}
}
0
 

Author Comment

by:zolf
ID: 17071280

i did that but it does not put the value outside it.that switch statement is inside a mouse listener
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071287
You'll find it easier to make the listener non-anonymous

private class MyListener extends MouseAdapter {
...
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071292
Is your mouselistener class static? If yes you cannot do this, you will need to use the object's reference.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071295
Can you post that bit of code you are trying to access?
0
 

Author Comment

by:zolf
ID: 17071302
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071310
To use that, do

tree.addMouseListener(new MYListener());

then put the business code in

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

of MyListener
0
 

Author Comment

by:zolf
ID: 17071314

what do you mean
0
 

Author Comment

by:zolf
ID: 17071322

i dont want to change my code,because that will alter other things
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 50 total points
ID: 17071329
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 17071333
> inse

I mean inside
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 17071350
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071352
>>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
 

Author Comment

by:zolf
ID: 17071364


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
 

Author Comment

by:zolf
ID: 17071375

i am trying to get the fk variable
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071383
Put all the variables you need to have access to, *outside* any methods/constructors.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071386
> i am trying to get the fk variable

Where is the "fk" declared?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071389
This

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

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

Author Comment

by:zolf
ID: 17071395

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

Author Comment

by:zolf
ID: 17071398

yes that is right
0
 

Author Comment

by:zolf
ID: 17071418

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
 

Author Comment

by:zolf
ID: 17071427

in the mouse listenser i need to update the JScrollPane jspMap.how do i do that.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071441
>>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
 
LVL 92

Expert Comment

by:objects
ID: 17071443
you need to updatre the image displayed by your label using the setIcon() method
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17071448
so in your listener find the required image, and call setIcon() on the label that is in your tabbed pane
0
 
LVL 92

Expert Comment

by:objects
ID: 17071457
> 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
 

Author Comment

by:zolf
ID: 17071468

thankssssssssssss objects.it workedddd.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17071475
>>it workedddd.

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

Author Comment

by:zolf
ID: 17071483

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
 
LVL 92

Expert Comment

by:objects
ID: 17071519
> thankssssssssssss objects.it workedddd.

woo hoo :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
CSV file parsing thru Java 13 39
Why is enum singleton a better approach than static factory 3 51
Netbeans and org.apache.commons.lang3 issue 3 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question