57

I am in the process of creating a Java app and would like to have a bar on the bottom of the app, in which I display a text bar and a status (progress) bar.

Only I can't seem to find the control in NetBeans neither do I know the code to create in manually.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Naxels
  • 1,725
  • 2
  • 14
  • 19

5 Answers5

111

Create a JFrame or JPanel with a BorderLayout, give it something like a BevelBorder or line border so it is seperated off from the rest of the content and then add the status panel at BorderLayout.SOUTH.

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(200, 200);

// create the status bar panel and shove it down the bottom of the frame
JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("status");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);

frame.setVisible(true);

Here is the result of the above status bar code on my machine:

enter image description here

Mike Clark
  • 10,027
  • 3
  • 40
  • 54
krock
  • 28,904
  • 13
  • 79
  • 85
  • 2
    didn't work for me, the status bar ended up in the middle of the window – Alex R Dec 30 '15 at 22:08
  • 1
    You need to use a different layout inside the JPanel. As in the code snippet above, BoxLayout will yield the result like in the picture. – Kake_Fisk Mar 20 '17 at 10:39
6

Unfortunately Swing does not have a native support for StatusBars. You can use a BorderLayout and a label or whatever you need to display at the bottom:

public class StatusBar extends JLabel {

    /** Creates a new instance of StatusBar */
    public StatusBar() {
        super();
        super.setPreferredSize(new Dimension(100, 16));
        setMessage("Ready");
    }

    public void setMessage(String message) {
        setText(" "+message);        
    }        
}

Then in your Main Panel:

statusBar = new StatusBar();
getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);

From: http://www.java-tips.org/java-se-tips/javax.swing/creating-a-status-bar.html

Simon
  • 9,255
  • 4
  • 37
  • 54
4

I have used swing library from L2FProd. The Status bar library they have provided is very good.

Below is how you would use it:

  1. Download the JAR they are providing and put it in your classpath
  2. The status bar internally divides the bar area into zone. Each zone can contain a Component (JLabel, JButton, etc). Idea is to fill the bar with required zones and the components.

  3. Instantiate the status bar as below....

    import java.awt.Component;
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import com.l2fprod.common.swing.StatusBar;
    
    StatusBar statusBar = new StatusBar();
    statusBar.setZoneBorder(BorderFactory.createLineBorder(Color.GRAY));
    statusBar.setZones(
        new String[] { "first_zone", "second_zone", "remaining_zones" },
        new Component[] {
            new JLabel("first"),
            new JLabel("second"),
            new JLabel("remaining")
        },
        new String[] {"25%", "25%", "*"}
    );
    
  4. Now add the above statusBar to the main panel you have (BorderLayout and set it to the south side).

See a sample screenshot from one of the apps I am working on here (it has 2 zones). Let me know if you face any issues....

James P.
  • 19,313
  • 27
  • 97
  • 155
arcamax
  • 610
  • 1
  • 7
  • 14
3

I would recommend the status bar component in the SwingX library - Here is the API doc for the status bar is here

A good example of its use is here.

Have fun.

Richard Walton
  • 4,789
  • 3
  • 38
  • 49
  • 5
    Pulling in the entire SwingX for a component that's trivial to implement doesn't seem like the best idea to me... If you're already using SwingX in the app, that's fine, but otherwise it's a complete overkill... – Bozhidar Batsov Jun 14 '10 at 10:10
1

For a more modern look than the accepted answer go with a GridBagLayout and a JSeparator:

JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.weighty = 0;

JPanel menuJPanel = new JPanel();
menuJPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
outerPanel.add(menuJPanel, gbc);

gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;

JPanel contentJPanel = new JPanel();
contentJPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLUE));
outerPanel.add(contentJPanel, gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 0;
gbc.insets = new Insets(0, 0, 0, 0);

outerPanel.add(new JSeparator(JSeparator.HORIZONTAL), gbc);
outerPanel.add(new JPanel(), gbc);

screenshot of the result

t.animal
  • 3,012
  • 1
  • 24
  • 25