• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add a Status Bar in a frame(at the Bottom ofcourse)

 
Murali Obla
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,
How would I add a Status Bar at the Bottom of the frame in Swings.Is there any component.
Thanks in Advance,
Murli
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use JProgressBar. Check API for details
 
Rajendar Goud
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi murali,
here is an example on the statusBar.
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
{
public Main()
{
getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Sex");
JMenuItem menuItem1 = new JMenuItem("male");
JMenuItem menuItem2 = new JMenuItem("female");
JMenuItem menuItem3 = new JMenuItem("androgyne");
// add the MenuItems to the Menu
menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
StatusBar statusBar = new StatusBar();
new JMenuItemHelpText(menuItem1, "The weak sex", statusBar);
new JMenuItemHelpText(menuItem2, "The strong sex", statusBar);
new JMenuItemHelpText(menuItem3, "The average sex, I guess", statusBar);
menuBar.add(menu);
this.setJMenuBar(menuBar);
getContentPane().add(BorderLayout.SOUTH, statusBar);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
setSize(400, 400);
}
public static void main(String[] args)
{
(new Main()).show();
}
}
class JMenuItemHelpText implements ChangeListener
{
JMenuItem menuItem;
String helpText;
StatusBar statusBar;
JMenuItemHelpText(JMenuItem menuItem, String helpText, StatusBar statusBar)
{
this.menuItem = menuItem;
this.helpText = helpText;
this.statusBar = statusBar;
menuItem.addChangeListener(this);
}
public void stateChanged(ChangeEvent evt)
{
if (menuItem.isArmed())
statusBar.setStatus(helpText);
else
statusBar.setStatus("");
}
}

class StatusBar extends JPanel
{
private JLabel statusLabel;
public StatusBar()
{
setLayout(new BorderLayout(2, 2));
statusLabel = new JLabel("Ready");
statusLabel.setBorder(BorderFactory.createLoweredBevelBorder());
statusLabel.setForeground(Color.black);
add(BorderLayout.CENTER, statusLabel);
JLabel dummyLabel = new JLabel(" ");
dummyLabel.setBorder(BorderFactory.createLoweredBevelBorder());
add(BorderLayout.EAST, dummyLabel);
}
public void setStatus(String status)
{
if (status.equals(""))
statusLabel.setText("Ready");
else
statusLabel.setText(status);
}
public String getStatus()
{
return statusLabel.getText();
}
}
cheers,
Raj
 
Murali Obla
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pete
Thanks for ur code Rajendar,
Cool it is working,
I am really gainin' some Swings knowledge ,
Murli
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic