Karlovsky120
Karlovsky120

Reputation: 6352

JLabel resizing JPanel

I want to make a status bar with text bar, progress bar and what not. However, things are acting stranglely.

At first I only had the text bar and the status bar looked like in example below (I didn't need the parent container):

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main
{   
    public static void main(String[] args)
    {           
        //Display actual GUI
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Gui();
            }
        });
    }
}

class Gui
{   
    public Gui()
    {   
        JFrame masterWindow = new JFrame();

        masterWindow.setSize(1100, 800);
        masterWindow.setMinimumSize(new Dimension(400, 400));
        masterWindow.setLocationRelativeTo(null);
        masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel rootPanel = new JPanel();
        rootPanel.setLayout(new BorderLayout());

        StatusBar statusBar = new StatusBar();
        rootPanel.add(statusBar, BorderLayout.SOUTH);

        masterWindow.setContentPane(rootPanel);
        masterWindow.setVisible(true);
    }
}

class StatusBar extends JLabel
{
    private static final long serialVersionUID = 1L;

    StatusBar()
    {   
        super("welcomeText");
        add(textBar);

        setOpaque(true);
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
    }
}

That works as it should. But, keep the height of the status bar in mind. Now, if you change the StatusBar class to the class below, the height will almost double. Why is that happening?

It's not like this setup requires more vertical space:

public class StatusBar extends JPanel
{
    private static final long serialVersionUID = 1L;

    StatusBar()
    {
        super();

        JLabel textBar = new JLabel(Main.language.getString("welcomeText"));
        add(textBar);

        setOpaque(true);
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
    }
}

Why is this happening, and how do I stop it?

EDIT: BorderLayout did indeed work, but I wanted to use MigLayout.

I figured it out, but now comes another issue. Additional progresBar won't dock to the east, and separator is not even visible.

Here's the code:

public class StatusBar extends JPanel
{
    private static final long serialVersionUID = 1L;
    JLabel textBar;
    JProgressBar progressBar;

    StatusBar()
    {
        setLayout(new MigLayout("insets 0, gap 0!")); //Mig has some gaps inbetween components, this will remove the gaps

        textBar = new JLabel(Main.language.getString("welcomeText"));

        progressBar = new JProgressBar(0, 100);

        JSeparator separator = new JSeparator(SwingConstants.VERTICAL);

        add(textBar);
        add(separator);
        add(progressBar, "east");

        setOpaque(true);
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
    }
}

I can make this a new question, but I don't think it's worth it...

Upvotes: 0

Views: 1263

Answers (2)

Bnrdo
Bnrdo

Reputation: 5474

Its because, calling super() will execute the no-arg constructor of the superclass, in your case JPanel. According to javadoc

JPanel() - Creates a new JPanel with a double buffer and a flow layout.

So when you called super(), a JPanel with FlowLayout is created.

Try changing the layout to BorderLayout

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

Set the layout of the JPanel to BorderLayout

   class StatusBar extends JPanel
    {
        private static final long serialVersionUID = 1L;

        StatusBar()
        {
            super();
            this.setLayout(new BorderLayout());  //set layout
            JLabel textBar = new JLabel(Main.language.getString("welcomeText"));
            add(textBar);

            setOpaque(true);
            setBorder(BorderFactory.createLoweredBevelBorder());
        }
    }

Upvotes: 1

Related Questions