0

I add some images to a JPanel. Therefore, I add a single image to a JLabel as an ImageIcon and add this to the main JPanel. Although I set the bounds (setBounds) to the image-size, there is a margin of a few pixel on top of the image shown below.

image http://w752749.open.ge.tt/1/files/64dsvTG/0/blob/x675

I also tried to add the images as DisplayJai(), without success (with DisplayJai, the images have also been croped in a strange way).

The important part of the code is

    JPanel srcJPanel = new JPanel();
    srcJPanel.setBounds(posW, posH, width, height);
    srcJPanel.setBorder(new LineBorder(Color.GREEN.darker(), 2));

    Image image = newImage.getScaledImg().getAsBufferedImage();
    JLabel l = new JLabel(new ImageIcon(image));
    l.setBorder(new LineBorder(Color.RED.darker(), 2));
    srcJPanel.add(l, BorderLayout.CENTER);

    MainPanel.add(srcJPanel);
    this.validate();

Can anyone help me with this margin?

Thanks a lot.

1
  • 2
    1) setBounds Don't do that, that is probably the core of the problem. Instead use layouts. 2) It might be more interesting to see the images used. Can you upload one (small in bytes) image that displays the problem? 3) For better help sooner, post an SSCCE. 4) Interesting screenshot, is that image in the BG done as a time-lapse? Apr 18, 2012 at 5:46

1 Answer 1

0

setBounds method should be used only if you have "null" layout on your MainPanel, otherwise with each its validation your bounds will be reset to default layout bounds. To set "null" layout - just pass null into MainPanel's setLayout method.

Also using "null" layout is unnecessary in most cases. You can simply use existing layouts or write your own to avoid problems you might have using "null" layout.

Anyway the code you have provided is not enough to see the actual problem - better post an SSCCE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.