0

Hello I read about Layouts but didn't get which one to use for my application. I want to add image to JPanel and place JLabel on op right corner just below the title bar.

I have written code for that but it is not working. JLabel is displayed at Centre.

My code is

// Below line adds image to Jpanel
 panel = new ImagePanel(backgroundImage);
 // I want to add below label to Jpanel
 JLabel jdesignNo=new JLabel(designNo);
 jdesignNo.setFont((new java.awt.Font("Times New Roman", 1, 30)));
 jdesignNo.setBounds(900, 100, 50, 30);
 jdesignNo.setBackground(Color.GREEN);
 panel.add(jdesignNo);
 frame.getContentPane().add(panel);
 frame.setVisible(true);

In above code i have set required location by setBound but it does not work.

How to solve this problem ?

Thanks !!!

Sachin Doiphode
  • 431
  • 2
  • 10
  • 24

4 Answers4

8

In above code i have set required location by setBound but it does not work.

There is no need to do that. You should not be using setBounds(...). Use layout managers:

JLabel label = new JLabel("Some Text");
label.setHorizontalAlignment(JLabel.RIGHT);
frame.add(label, BorderLayout.NORTH);
Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
4

Since you have a dominating feature in your layout (the image) you probably want to use a BorderLayout as your main layout.

frame.getContentPane().setLayout(new BorderLayout(4, 4));

then add the image to the BorderLayout.CENTER

next the JLabel wants to go in the BorderLayout.NORTH

Now, if you do that, it won't go to the far right, so create a JPanel for the north, add the JLabel to the panel, and place the panel in the north.

Now you need a layout for the north panel. A BoxLayout will work

northPanel.add(Box.createHorizontalGlue());
northPanel.add(label);
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • 1
    The BorderLayout is the default layout for top level containers. – camickr May 01 '11 at 20:59
  • In addition, "`remove` and `setLayout` have been overridden to forward to the `contentPane` as necessary."—[Adding Components to the Content Pane](http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html#contentpane). – trashgod May 01 '11 at 21:58
1

Make sure you have

panel.setLayout(null);

if you wish to position the components yourself.

But identifying and using a layout manager that would fit your needs would make things easier as the complexity of your application grows.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

A possibility with a GridBagLayout:

import java.awt.*;
import javax.swing.*;

public class MyPanel extends JPanel {

    public MyPanel() {
        setLayout(new GridBagLayout());
        add(new JLabel("TOP RIGHT"), new GridBagConstraints(
                0, // gridx
                0, // gridy
                1, // gridwidth
                1, // gridheight
                1, // weightx
                1, // weighty
                GridBagConstraints.NORTHEAST, // anchor <------------
                GridBagConstraints.NONE, // fill
                new Insets(0, // inset top
                0, // inset left
                0, // inset bottom
                0), // inset right
                0, // ipadx
                0)); // ipady
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyPanel());
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Nicolas

Nicolas_75
  • 95
  • 6