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.
Related
I want to add a background image to a JFrame which doesn't have any panels. It is a project I'm working on and I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code to do that and also I'm using netbeans. Is there any solution for this?
I want to add a background image to a JFrame which doesn't have any panels.
The content pane of the frame is a JPanel, so yes it does have panels.
I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code
If you want a background image then you will need to change your code to make sure the content pane can display the image. So yes you will need to change your code whether you use a JLabel of a JPanel that paints an image.
Check out Background Panel for code that will allow you to use either approach.
The key is that you need to set the content pane of your frame BEFORE you start adding components to the frame. So the code might look something like:
BackgroundPanel panel = new BackgroundPanel( yourImage );
frame.setContentPane( panel );
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
I don't know what the Netbeans generated code looks like so I'll leave it up to you to figure out where to put the code.
I have a JFrame that has a JPanel inside. I call "setPreferredSize(new Dimension(500, 600));" but I want the JPanel and its contents to resize when someone resizes the JFrame.
BorderLayout is the way to go. Components start at their preferred size, but are expanded as needed to fill the region they are in.
Set your layout on your frame with BorderLayout
Add your JPanel by
frame.add(yourPanel, BorderLayout.CENTER);
This will allow it to stretch vertically & horizontally
As for the Contents inside a JPanel, give it a layout that will accommodate stretching as well.
Use a layout manager instead of setting the bounds for each component.
It is going to vary from program to program how you want your components to move.
Take a look at this and try to see which layout will work best for you.
I have problem using Grid Layout in Java Swing. I create Panel and add GridLayout with 4 columns and 2 rows.
I try to add JButton inside it, but the JButton stretch the width.
Look this image :
I want create JButton position like this, because I want to make image gallery using Java Swing.
Look this image :
Any idea? Thanks before :)
Use GridBagLayout and specify GridBagConstraints. It will help you to render components as you want
Kindly refer GridBagLayout
You can try the layout http://java-sl.com/tip_columns_flow_layout.html
It's kind of Win Explorer layout when components flow to fill columns to available width.
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.
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 .