Placing a JLabel at a specific x,y coordinate on a JPanel

103,556

Solution 1

You have to set your container's Layout to null:

myPanel.setLayout(null);

However is a good advise also to take a look at the Matisse Layout Manager, I guess it is called GroupLayout now. The main problem with absolute positioning is what happens when the window changes its size.

Solution 2

  1. Set the container's layout manager to null by calling setLayout(null).

  2. Call the Component class's setbounds method for each of the container's children.

  3. Call the Component class's repaint method.

Note:

Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Refer this link: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Solution 3

Layout managers are used to automatically determine the layout of components in a container. If you want to put components at specific coordinate locations, then you should not use a layout manager at all.

myPanel = new JPanel(null);

or

myPanel.setLayout(null);

Solution 4

My advise is to use an IDE like NetBeans with its GUI editor. To inspect the code and because there are many ways:

Setting the layout manager, or for absolute positioning doing a myPanel.setLayout(null), has several influences.

In general, assuming you do your calls in the constructor of a JFrame, you can call pack() to start the layouting.

Then, every layout manager uses its own implementation of add(Component) or add(Component, Constraint). BorderLayout's usage is with add(label, BorderLayout.CENTER) and so on.

Share:
103,556
AndroidDev
Author by

AndroidDev

Updated on June 09, 2020

Comments

  • AndroidDev
    AndroidDev almost 4 years

    I'm trying to place a series of JLabels at specific X and Y coordinates on a JPanel (and set its height and width, too). No matter what I do, each label winds up immediately to the right of the previous label and has the exact same size as all of the others.

    Right now, my Jpanel is in a Grid Layout. I've tried Absolute Layout (illegal argument exception results), Free Design (no labels appear), Flow Layout (everything just gets squeezed to the center), and a few others.

    Not sure what I need to do to make this work. Can anyone help? Thanks!

    JLabel lbl1 = new JLabel("label 1");
    JLabel lbl2 = new JLabel("label 2");
    JLabel lbl3 = new JLabel("label 3");
    JLabel lbl4 = new JLabel("label 4");
    JLabel lbl5 = new JLabel("label 5");
    
    myPanel.add(lbl1);
    myPanel.add(lbl2);
    myPanel.add(lbl3);
    myPanel.add(lbl4);
    myPanel.add(lbl5);
    
    lbl1.setLocation(27, 20);
    lbl2.setLocation(123, 20);
    lbl3.setLocation(273, 20);
    lbl4.setLocation(363, 20);
    lbl5.setLocation(453, 20);
    
    lbl1.setSize(86, 14);
    lbl2.setSize(140, 14);
    lbl3.setSize(80, 14);
    lbl4.setSize(80, 14);
    lbl5.setSize(130, 14);
    
    • gersonZaragocin
      gersonZaragocin over 11 years
      You have to set your container's Layout to null myPanel.setLayout(null);
    • AndroidDev
      AndroidDev over 11 years
      Ack! That was about the only layout that I didn't try. Thanks! If you enter this as an answer, I'll mark it as accepted.
    • Andrew Thompson
      Andrew Thompson over 11 years
      "I'm trying to place a series of JLabels at specific X and Y coordinates on a JPanel" Why?
    • kleopatra
      kleopatra over 11 years
      most probably you do not want to manually size/locate all children of a container - that's the exclusive task of a LayoutManager. So your task it to learn different behaviour of their respective implementations and choose one that fits your requirement
  • Andrew Thompson
    Andrew Thompson over 11 years
    "main problem .. window changes its size." So that must leave the minor problems as: Figuring the logic for the size of a component using different fonts, PLAFs, screen settings.. or accommodating other groups of components. Using a null layout is most often a very bad idea.
  • gersonZaragocin
    gersonZaragocin over 11 years
    @AndrewThompson Yes I agree that using a null layout is a bad idea generally. However consider applications that have a fixed window size or some of them running in embedded devices that runs that single application. In that cases, probably arranging the components with a layout could be just extra work and memory. Regards
  • Andrew Thompson
    Andrew Thompson over 11 years
    "probably arranging the components with a layout could be just extra work" Until the point the client looks at the UI, frowns in concentration, and says, "Move the 'OK' button from below to above the check boxes".
  • gersonZaragocin
    gersonZaragocin over 11 years
    @AndrewThompson, probably that is a different problem or the result of doing poor analysis or design process, But this is going away of the real question: "Placing a JLabel at a specific x,y coordinate on a JPanel". I don't have all the contextual information that questioner has to criticize the way he is doing things or why he is doing that way.
  • Andrew Thompson
    Andrew Thompson over 11 years
    "I don't have all the contextual information" So if someone ask you "How do I shoot myself in the foot?", lacking further contextual information, provide instructions? Sometimes it is better to challenge the need for a question or strategy, than to immediately provide an answer that is almost certain to cause further problems. OTOH, you seem to hold a different view..
  • gersonZaragocin
    gersonZaragocin over 11 years
    @AndrewThompson no, I share your point of view about challenging people. If you read again the answer, I did it after provide it, pointing out the main problem with the strategy provided but knowing that there is a real question that need an answer. But if you think you are right so you are right, I think it is not worth to start a long debate for something as short as this. Regards.
  • kleopatra
    kleopatra almost 8 years
    manual positioning/sizing (== not using a LayoutManager) always was, is, and will be wrong ...