Solved

how to access variable

Posted on 2006-07-10
34
217 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
Comment Utility
Make it an instance variable of the class
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility

you'll mean define that variable outsite the constructor
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Yes
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes

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

Author Comment

by:zolf
Comment Utility

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
Comment Utility
You'll find it easier to make the listener non-anonymous

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

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
Can you post that bit of code you are trying to access?
0
 

Author Comment

by:zolf
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility

what do you mean
0
 

Author Comment

by:zolf
Comment Utility

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
Comment Utility
> 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
Comment Utility
> inse

I mean inside
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
Comment Utility
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
Comment Utility
>>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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:zolf
Comment Utility


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
Comment Utility

i am trying to get the fk variable
0
 
LVL 35

Expert Comment

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

Expert Comment

by:girionis
Comment Utility
> i am trying to get the fk variable

Where is the "fk" declared?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
This

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

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

Author Comment

by:zolf
Comment Utility

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

Author Comment

by:zolf
Comment Utility

yes that is right
0
 

Author Comment

by:zolf
Comment Utility

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
Comment Utility

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

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility

thankssssssssssss objects.it workedddd.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>it workedddd.

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

Author Comment

by:zolf
Comment Utility

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
Comment Utility
> thankssssssssssss objects.it workedddd.

woo hoo :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now