tags:

views:

455

answers:

2

I want to have an ImageIcon in a JLabel that i will be able to update from another method. But I can't figure out a way to be able to create a static JLabel(so as to be able to acccess it from another method) and also instaiate it as a Jlabel that contains an imageIcon - is there another method other than JLabel label = new JLabel(imgIcon) to create a label with an imageIcon? tried to use the setIcon method without the label being instatiated the way it is above but it gave a null pointer exception. Thanks in adavance for any help.

A: 

Don't make the JLabel static - instead define it outside of other methods but still in your class.

public class Test {

    private JLabel label = new JLabel(new ImageIcon(/*your icon*/));
}

If you need to access it from another class, create an accessor method:

public JLabel getLabel() {
    return label;
}
AlbertoPL
If the label is created private can I access it form other methods in the same class if it was created outside of any method? Because I am getting an error that tells me that a non-static variable cannot be referenced froma static context? Does this mean tnhat even for within the same class I have to use the getlabel(0 method? If so how do I use the getlabel() method? do i need to create a label to call the getLabel method from ie label2 = test.getLabel()? or how do I get access to it to change the Icon in the label? thanks
pie154
Don't make your methods static if you want access from other methods in the same class.
AlbertoPL
but what do I do if I want access from both the same class and another class?
pie154
That's what the getLabel() method is for. You'd have to create an instance of Test. I recommend www.java-made-easy.com, it has the perfect tutorial for showing how to properly create a class and instances of the class for use outside of the class.
AlbertoPL
A: 

I havent been able to get AlbertoPL way to work so I have make a short example program that brings up the same null pointer error when run, if you just change the images to any jpg of ur own choice u will b able to see the problem and hopefully will be able to help me out. AlbertoPL what you are telling me probably works just I am not understanding it vey well. UI have also included the methods used to resize the bufferedImage and load the image, incase the error is oming from there, but the nullpointer says it is coming from the line

picPanel.add(labelPicPanel);

Thanks very much in advance for any help.

the example code is,

public class test {

static JPanel picPanel;
static JLabel labelPicPanel = new JLabel(new ImageIcon("C:/Documents and Settings/Admin/My Documents/My Pictures/pi.jpg"));

public static void test() {
    String ref = "C:/temp/new00000001.jpg";
    BufferedImage loadImg = loadImage(ref);
    ImageIcon imgIcon = new ImageIcon(loadImg);
    labelPicPanel.setIcon(imgIcon);
    picPanel.add(labelPicPanel);
    picPanel.setPreferredSize(new Dimension(1120, 620));
    JFrame frame = new JFrame("Frame");
    frame.add(picPanel);
    frame.pack();
    frame.setVisible(true);
}

public static BufferedImage loadImage(String ref) {  
        BufferedImage bimg = null;  
        try {  

          bimg = javax.imageio.ImageIO.read(new File(ref));  
     } catch (Exception e) {  
         e.printStackTrace();  
     }  
     BufferedImage bimg2 = resize(bimg,1120,620);
     return bimg2;  
 }  


 public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());  
    Graphics2D g = dimg.createGraphics();  
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
    g.dispose();  
    return dimg;  
}

public static void main(String args[]){
    test();
}

}

pie154
Just realised I maybe shouldn't have posted this as an answer but didnt want to create a new question. Sorry but what is the proper protocol for this(for future reference?)
pie154
Th ereason you have to use static everywhere is because you're calling the test method statically from your main method. If you instead do this: test myTest = new test(); and make a proper constructor, then you won't have to have everything be static.
AlbertoPL
ok, that makes sense. What do u mean by a proper constructor?
pie154
also, do u think that will solve the problem of getting the nullpointer? R u able to place a BUfferedImage in to create a new ImageIcon, because it inherits that ability from image? thanks.
pie154
First of all, the name of your class should start with an uppercase letter. Besides that, the constructor for your class should be: public test() { /*your code*/ }, notice how there is no return type. And yes, you should be able to place a BufferedImage in. I don't exactly know where you are getting a null pointer.
AlbertoPL