-1

I am trying to add a button over the image. But neither see the button nor the image. Why is that ? I am calling this method from the constructor just after the initComponents method is called by the IDE.

public void initD() {
    try {
        BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\meaning.JPG"));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
        JButton b = new JButton("Press me");
        jPanel1.add(picLabel);
        jPanel1.add(b);
        System.out.println("last statement");
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

I only see the frame as an output.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
saplingPro
  • 20,769
  • 53
  • 137
  • 195
  • What layout manager are you using? Did you know the `JButton` has icon support? – MadProgrammer Sep 04 '13 at 07:33
  • 4
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 04 '13 at 07:34
  • @MadProgrammer The default used by IDE. I have just added this method. I have to add many buttons over the image. – saplingPro Sep 04 '13 at 07:36
  • If the default by IDE is `GroupLayout` then adding to it dynamically is tricky. Use a helper panel with a different layout manager (somewhat similar situation [here](http://stackoverflow.com/questions/18258482/create-and-show-a-label-after-running-java-application/18258621#18258621)). – kiheru Sep 04 '13 at 07:46
  • @kiheru I didn't get that. Can you explain as what do I need to do through an answer – saplingPro Sep 04 '13 at 08:17
  • 1
    `GroupLayout` requires adding the components to the *layout*, and in a rather complicated way. Change the layout manager of `jPanel1` to something more appropriate (or use a helper panel if `jPanel1` has some other contents that have been placed using the IDE). – kiheru Sep 04 '13 at 08:23
  • @saplingPro : Please have a look at this answer for [how to load an Image to the Project](http://stackoverflow.com/a/9866659/1057230), hope it helps :-) – nIcE cOw Sep 04 '13 at 09:25

1 Answers1

2

I dont know which layout you are using, however you should implement button.setIcon(); like this;

public void initD() {
    JButton button = new JButton("Press me");
    try {
        BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\meaning.JPG"));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));

        button.setIcon(new ImageIcon(myPicture));

        System.out.println("last statement");
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

In addition you may need to consider resource of your image maybe this implemantation can be helpfull ;

ImageIO.read(getClass().getResource("resources/meaning.JPG")));
goGud
  • 4,163
  • 11
  • 39
  • 63
  • I want buttons over image..not image over a button – saplingPro Sep 04 '13 at 13:30
  • @saplingPro : Please have a look at this answer, regarding [adding background image](http://stackoverflow.com/a/11428289/1057230), hope it might be able to help you. Though the last link in the comments to your question, is a better approach, to draw the image on the `JPanel` and add components to it, instead of putting components on the `JLabel`. – nIcE cOw Sep 04 '13 at 15:35