Strange top-margin in JPanel with added Image in JLabel - java

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.

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.

Related

Putting a Panel on top of a JLabel backGround (Java)

I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.

Java - Change IconImage in JLabel created with JScrollPane

Probably a stupid question. My apologies if so. I couldn't find what I needed with google searches, but probably because I'm not really sure how to word what I need.
I have this line of code:
JScrollPane scrlPane = new JScrollPane(new JLabel(imgIcon));
This does exactly what I need it to do. However, I'm not sure how to change the image in the imgIcon after it's created with this method.
Again, I apologize if this is a stupid question. I've tried to create a separate JLabel outside of this method and add it to the JScrollPane, but for some reason, it draws a gray box over the image. I know it's drawing the image, because I can see 1 pixel of the image loaded around the edge of the gray box.
Thanks for your time!
The easiest thing to do would be to keep a reference to your JLabel object, and to to use its setIcon(Icon icon) method to change it to your new ImageIcon object.
Or you can do somehting like this.
JViewport viewport = scrollPane.getViewport();
JLabel label = (JLabel)viewport.getView();
if (label != null) {
label.setIcon(newImgIcon);
}

Why will image not add when JFrame has .setSize

I'm a somewhat novice programmer and I'm have some trouble adding an image to my frame. While I know how to add images generally, this specific case it does not work.
public class Tutorial extends JFrame{
Tutorial(){
JFrame frame = new JFrame("ImageTutorial");
frame.setVisible(true);
frame.setSize(750,850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
ImageIcon image = new ImageIcon(getClass().getResource("Green Block.png"));
JLabel imagelabel = new JLabel(image);
imagelabel.setBounds(10, 10, 75, 75);
imagelabel.setOpaque(true);
frame.add(imagelabel);
Now, I've located the problem but I don't understand 'why' its a problem. When I remove
frame.setSize(750,850);
the image shows, but when its there it doesn't. How can the frame's size impact the image showing and how can I get around it?
Just curious, logically, what makes you think a frame should be visible before you add any components? Logically speaking, wouldn't it seem right to add your components first, then make the frame visible. It's like displaying a painting in an art gallery even before the painter has painted anything on it. It just makes no sense. I highly doubt setting the size has anything to do with it. IF you don't set the size of the frame, then the frame appears as small as possible. When you resize the frame, it causes a repaint, then showing the label you add. But generally, you want to always set frame visible after all you components are added, to avoid this problem.
Side note: You should stay away from null layouts. You need to learn to use Layout Managers and let them do the dynamic sizing and locating for you.

Image positioning in java swings

I have this code to add an image
JFrame note=new new JFrame();
JLabel label5=new JLabel();
label5.setIcon(new ImageIcon(searchresult.class.getResource("/images/expired.png")));
label5.setBounds(200,500,450,100);
note.add(label5);
The result I get is this
I tried changing the bounds to other values but there is no change in the image position.The image remains at that same position.
what am I doing wrong?
You haven't set a layout to frame.
note.setLayout(new FlowLayout());
This will help you to drag your image (or whatever the component you want to add) in center.
If you want to position the component via setBounds(), you need to set the layout of the container to null. See http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
But I would recommend to use a proper layout manager with constraints.

JLabel on top of another JLabel

Is it possible to add a JLabel on top of another JLabel? Thanks.
The short answer is yes, as a JLabel is a Container, so it can accept a Component (a JLabel is a subclass of Component) to add into the JLabel by using the add method:
JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);
In the above code, the insideLabel is added to the outsideLabel.
However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.
So, the question comes down what one really wants to accomplish by adding a label on top of another label.
Edit:
From the comments:
well, what i wanted to do was first,
read a certain fraction from a file,
then display that fraction in a
jlabel. what i thought of was to
divide the fraction into 3 parts, then
use a label for each of the three.
then second, i want to be able to drag
the fraction, so i thought i could use
another jlabel, and place the 3'mini
jlabels' over the big jlabel. i don't
know if this will work though..:|
It sounds like one should look into how to use layout managers in Java.
A good place to start would be Using Layout Managers and A Visual Guide to Layout Managers, both from The Java Tutorials.
It sounds like a GridLayout could be one option to accomplish the task.
JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
In the above example, the JPanel is made to use a GridLayout as the layout manager, and is told to make a row of JLabels.
The answer to your original question is yes for the reasons given that any Component can be added to a Container.
The reason you don't see the second label is because by default a JLabel uses a null layout manager and the size of the second label is (0, 0) so there is nothing to paint. So all you need to do is set the bounds of the second label and away you go.
You can't use a layout manager if you want to drag components around because as soon as you resize the frame etc, the layout manager will be invoked and the components will be repositioned based on the layout manager of the component.
it's a matter of layout.
you can do that using null layout (with hard coded locations) or with a custom layout.
you can use a JLayeredPane and set it's border to No Border.
you can add put them above each others by using the horizontal or vertical gap (hgap,vgap) the attributes of the layout
JPanel p = new JPanel(new GridLayout(2, 1,-40,0));
//the 40 is the hgap , make it the same with the label height .

Categories

Resources