Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to access variable

Posted on 2006-07-10
34
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
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 200 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 200 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

636 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