Search code examples
javaswingjlabellayout-managernull-layout-manager

JLabel not positioning correctly


I am just throwing together a quick and dirty GUI to display some data when I ran into an odd issue. The last label I add to the JFrame doesn't want to be positioned or display the border I put on it, so it looks like this:

Here is my code:

public DisplayData (Connection tConn)
{
    ID = tID;
    conn = tConn;

    setupObjects();
    setupFrame();
}

private void setupObjects()
{
    JLabel caseLabel = new JLabel ("Case #:");
    JLabel dateLabel = new JLabel ("Date:");
    JLabel reportLabel = new JLabel ("Report:");
    JLabel offenceLabel = new JLabel ("Offence:");
    JLabel descriptionLabel = new JLabel ("Description:");

    this.add(caseLabel);
    this.add(dateLabel);
    this.add(reportLabel);
    this.add(offenceLabel);
    this.add(descriptionLabel);

    caseLabel.setBounds(50, 50, 130, 25); //x, y, width, height
    dateLabel.setBounds(50, 100, 130, 25);
    reportLabel.setBounds(50, 150, 130, 25);
    offenceLabel.setBounds(50, 200, 130, 25);
    descriptionLabel.setBounds(100, 50, 130, 25);

    caseLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    dateLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    reportLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    offenceLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    descriptionLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}

private void setupFrame()
{
    this.setTitle("Data Display");
    this.setSize (650, 700); //Width, Height
    this.setLocation(300, 10);
    this.setResizable(false);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(null);
}

Yes, I know I should be using a proper layout manager, but like I said i just wanted something quick and dirty. Plus, I will not be beaten by something that should be this simple. Any ideas would be appreciated.

EDIT: As Compass and Neophyte pointed out, my order of operations was off. Flipped my method calls and all is good again in the world. Thanks for the 2nd pair of eyes.


Solution

  • Your order of operations is incorrect.

    You initially call setupObjects();

    This plays your objects out onto a JFrame, which has the default LayoutManager of BorderLayout.

    By using the default add(Component comp) method, with BorderLayout, you end up putting the component into null for BorderLayout, which is not supposed to be normal. Furthermore, the reason you can't see the border for this object is because the border is actually the size of the frame. If you explicitly set a region for BorderLayout, then you'll see it work, but setting to no region seems to just break BorderLayout.

    Additional add calls appear to free the previous item from the BorderLayout null management, allowing the bounds to take over.

    Afterwards, you call setupFrame(); which removes the layout manager, but does not refresh what is currently rendered.

    This sets the layout to null, which does nothing to how the frame is displayed, but just removes the layout.

    To avoid this issue, call setupFrame(); prior to setupObjects();, and then setVisible(true) can be called at the end of setupObjects();