Status bar or jlabel

hi all,
how can i create a status bar on my java app
that shows a message if it is connected on the internet or not.
i already have an application that checks for internet connection
every 6seconds, and has a getter method that returns a
message.
also is this possible?
JLabel.setText(CheckConnection.getMessage())
where CheckConnection.getMessage() returns a String value.
how can i make my JLabel act like it was refreshing when the string passed to it changed?
so that my JLabel notifies my application that it is disconnected.
thanks.

Hi,
Create a thread that calls checkConnection.getMessage(), and sets the message on the JLabel.
Kaj

Similar Messages

  • How can I use the same thread to display time in both JPanel & status bar

    Hi everyone!
    I'd like to ask for some assistance regarding the use of threads. I currently have an application that displays the current time, date & day on three separate JLabels on a JPanel by means of a thread class that I created and it's working fine.
    I wonder how would I be able to use the same thread in displaying the current time, date & day in the status bar of my JFrame. I'd like to be able to display the date & time in the JPanel and JFrame synchronously. I am developing my application in Netbeans 4.1 so I was able to add a status bar in just a few clicks and codes.
    I hope somebody would be able to help me on this one. A simple sample code would be greatly appreciated.
    Thanks in advance!

    As you're using Swing, using threads directly just for this kind of purpose would be silly. You might as well use javax.swing.Timer, which has done a lot of the work for you already.
    You would do it something like this...
        ActionListener timerUpdater = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                // DateFormat would be better, but this is an example.
                String timeString = new Date().toString();
                statusBar.setText(timeString);
                someOtherLabel.setText(timeString);
        new Timer(1000, timerUpdater).start();That code will update the time once a second. If you aren't going to display seconds, you might as well increase the delay.
    The advantage of using a timer over using an explicit thread, is that multiple Swing timers will share a single thread. This way you don't blow out your thread count. :-)

  • How to Insert a Status Bar to my application

    Hello all
    I use JBuilder v9 and would like to add a status bar at the bottom of the main
    frame in my program. I checked the manual pages but there is no such class as StatusBar or JStatusBar.
    When I type StatusBar (or JStatusBar) sb = new StatusBar (or JStatusBar) ();
    it gives errror message.
    Does anyone know how to add a status bar to a frame?

    A quick a dirty example:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class StatusBarDemo extends JFrame
      private JLabel statusBar;
      public StatusBarDemo()
        JMenu menu = new JMenu("File");
        JMenuItem item = new JMenuItem("Open");
        item.addMouseListener(new StatusBarUpdator("Open a new file"));
        menu.add(item);
        item = new JMenuItem("Print");
        item.addMouseListener(new StatusBarUpdator("Print the current file"));
        menu.add(item);
        item = new JMenuItem("Save");
        item.addMouseListener(new StatusBarUpdator("Save the current file"));
        menu.add(item);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        setJMenuBar(menuBar);
        statusBar = new JLabel(" ");
        statusBar.setForeground(Color.black);
        statusBar.setBorder(new CompoundBorder(new EmptyBorder(2, 5, 2, 5),
                                               new SoftBevelBorder(SoftBevelBorder.LOWERED)));
        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(statusBar, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      public static void main(String[] args)
        StatusBarDemo frame = new StatusBarDemo();
        frame.setSize(400,300);
        frame.setVisible(true);
      class StatusBarUpdator implements MouseListener
        private String hoverText;
        public StatusBarUpdator(String text)
          hoverText = text;
        public void mouseEntered(MouseEvent e)
          statusBar.setText(hoverText);
        public void mouseExited(MouseEvent e)
          statusBar.setText(" ");
        public void mouseClicked(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
    }

  • Status bar question-applet !!

    I wrote an applet (below) that is supposed (among other things) to write to the status bar but what happens is that "Applet started." always overwrites the text I'm trying to write to the status bar. Why is that and how can I fix that? The code is:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Multiplication extends JApplet implements ActionListener {
         JLabel showAnswer;
         JTextField answer;
         int usersAnswer, product, value1, value2;
         public void init()
                   Container c=getContentPane();
                   c.setLayout(new FlowLayout());
                   showAnswer=new JLabel("Type in the answer: ");
                   answer=new JTextField(10);
                   answer.addActionListener(this);
                   c.add(showAnswer);
                   c.add(answer);
              public void actionPerformed(ActionEvent e)
                   multiplication();
              public void multiplication()
                   value1=0+(int) (Math.random()*9);
                   value2=0+(int) (Math.random()*9);
                   showStatus("How much is "+value1+" times "+value2+"?");
                   product=value1*value2;
                   usersAnswer=Integer.parseInt(answer.getText());
                   if (usersAnswer==product) {
                        showStatus("Very good!");
                   while (usersAnswer!=product) {
                        usersAnswer=Integer.parseInt(answer.getText());
                        showStatus("No.Please try again.");
    }

    I modified my code a little bit and now it works but I still can't figure out how to overwrite the initial string ("Applet started."). Can someone help me,please !!!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Multiplication extends JApplet implements ActionListener {
         JLabel showAnswer;
         JTextField answer;
         int usersAnswer, product, value1, value2;
         public void init()
                   Container c=getContentPane();
                   c.setLayout(new FlowLayout());
                   showAnswer=new JLabel("Type in the answer: ");
                   answer=new JTextField(10);
                   answer.addActionListener(this);
                   c.add(showAnswer);
                   c.add(answer);
                   showStatus("");
              public void start()
                   showStatusBar();
              public void actionPerformed(ActionEvent e)
                   multiplication();
              public void multiplication()
                   usersAnswer=Integer.parseInt(answer.getText());
                   if (usersAnswer==product) {
                        showStatus("Very good!");
                   if (usersAnswer!=product) {
                        showStatus("No.Please try again.");
              public void showStatusBar()
                   value1=0+(int) (Math.random()*9);
                   value2=0+(int) (Math.random()*9);
                   showStatus("How much is "+value1+" times "+value2+"?");
                   product=value1*value2;
    }

  • Status Bar

    Hallo.
    I am writing a Java program that comprises several JInternalFrames to display info. However, the main outer JFrame needs a status bar of some sort, e.g. the one that says "Done" at the bottom of your browser. I have added a JLabel to South of the BorderLayout but it doesnt show. If I remove the desktop thing that they say has to be set for the internal frames to show, the label is seen. How can I work around this for the label to be seen, independent of the JInternalFrames?
    Thanks.

    when I try to do this:
    setContentPane(desktop, BorderLayout.CENTER);
    this is the compiler error that I get:
    setContentPane(java.awt.Container) in javax.swing.JFrame cannot be applied to (javax.swing.JDesktopPane,java.lang.String)
    a part of the code is:
    //snip---
    // this method makes it possible so that the internal frames are visible
         desktop = new JDesktopPane(); //a specialized layered pane
         setContentPane(desktop, BorderLayout.CENTER);
    desktop.setBackground(Color.GRAY);
         getContentPane().add(statusBar, BorderLayout.SOUTH);
    // give this window a title, size and location
    setTitle("Spin Master");
    setDefaultLookAndFeelDecorated(true);
    setSize(900, 650);
    //snip---
    This compiles well, but no status bar gets shown. However, on commenting out sone few lines:
    //snip---
    // this method makes it possible so that the internal frames are visible
         desktop = new JDesktopPane(); //a specialized layered pane
         //setContentPane(desktop, BorderLayout.CENTER);
    desktop.setBackground(Color.GRAY);
         getContentPane().add(statusBar, BorderLayout.SOUTH);
    // give this window a title, size and location
    setTitle("Spin Master");
    setDefaultLookAndFeelDecorated(true);
    setSize(900, 650);
    //snip---
    One sample method I use to add the internal frames is:
    private void showAllScores()
    ScoresDisplayer shower = new ScoresDisplayer();
    shower.setVisible(true);
    shower.displayAllScores();
    desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    desktop.add(shower);
    try
    shower.setSelected(true);
    catch (java.beans.PropertyVetoException bean_error)
    showInternalFrameDisplayErrorMessage();
    The status bar gets displayed fine, but no internal frames are shown. All I can think of doesnt bear fruit...

  • How to display the file status in the status bar?

    Hi all,
    Can anyone tell me how to display the file status in the status bar?
    The file status can consists: the type of the file, the size of the file etc..
    thanx alot..

        class StatusBar extends JComponent {
              JLabel l = new JLabel("ready");
            public StatusBar() {
                  super();
                  setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
                  add(l);
              public void setText( String prompt ) {
                   l.setText(prompt);
                   setVisible( true );
            public void paint(Graphics g) {
                  super.paint(g);
        }

  • Displaying a status bar through swings

    Hi everybody,
    My problem is that i want to display a status bar, using Java Swings. In this regard i am facing somwe difficulty, and i would be really thankful to those, who in some way can help me.
    thanks

    Create a Static JLabel in the main application constructor and add it to the South position of your main window.
    Now set the text dynamically whereever u want to show a status message to this static label.
    I hope this can solve your problem

  • Displaying Status Bar

    Hi,
    I am having an application which consists of JFrames on which JInternal Frames are added through JDeskTopPanes. I am facing two problems :
    1. When I iconify the JInternalFrames, I am unable to see the incons in the bottom of the JFrame.
    2. How can I construct a status bar to JFrame with provision to display messages?
    Thanks in anticipation of reply
    Srini

    Here is some example code that shows internal frames inside a JFrame. There is a status bar at the bottom of the JFrame. If you run this code, you will see the status bar being updated whenever you move or resize one of the JInternalFrames. Hope this helps...
    cheers,
    dave
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class IntFrameTest extends JFrame
         private StatusBar sbar = new StatusBar();
         private JDesktopPane desktop = new JDesktopPane();
         public IntFrameTest()
              desktop.setPreferredSize(new Dimension(500, 400));
              desktop.setDesktopManager(new MyDtopManager());
              // Create Internal frames
              for (int i = 0, j = 0; i < 50; i += 10, j++)
                   JInternalFrame iframe = new JInternalFrame("Int Frame# " + j,
                        true, true, true, true);
                   iframe.setBounds(i, i, 250, 100);
                   iframe.setVisible(true); //necessary as of 1.3
                   desktop.add(iframe);
                   try
                        iframe.setSelected(true);
                   } catch (java.beans.PropertyVetoException e) { }
              getContentPane().setLayout(new BorderLayout());
              getContentPane().add(desktop, BorderLayout.CENTER);
              getContentPane().add(sbar, BorderLayout.SOUTH);
              pack();
         public static void main(String[] args)
              IntFrameTest jt = new IntFrameTest();
              jt.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
              jt.addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent event)
                        System.exit(0);
              jt.setVisible(true);
         class MyDtopManager extends DefaultDesktopManager
              public void dragFrame(JComponent frame, int newX, int newY)
                   super.dragFrame(frame, newX, newY);
                   sbar.setStatus1(((JInternalFrame)frame).getTitle() + " - Moving");
                   sbar.setStatus2(String.valueOf(newX)+","+String.valueOf(newY));
              public void resizeFrame(JComponent frame, int newX, int newY, int newWidth, int newHeight)
                   super.resizeFrame(frame, newX, newY, newWidth, newHeight);
                   sbar.setStatus1(((JInternalFrame)frame).getTitle() + " - Resizing");
                   sbar.setStatus2(String.valueOf(newX)+","+String.valueOf(newY)+","+
                        String.valueOf(newWidth)+","+String.valueOf(newHeight));
              public void endDraggingFrame(JComponent frame)
                   super.endDraggingFrame(frame);
                   sbar.setStatus1(null);
                   sbar.setStatus2(null);
              public void endResizingFrame(JComponent frame)
                   super.endResizingFrame(frame);
                   sbar.setStatus1(null);
                   sbar.setStatus2(null);
    class StatusBar extends JPanel
         private final JLabel status1;
         private final JLabel status2;
         public StatusBar()
              status1 = new JLabel(" "); // Need the space to get correct size!
              status2 = new JLabel(" ");
              status1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
              status1.setHorizontalAlignment(JLabel.LEFT);
              status2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
              Dimension size = new Dimension(125, status2.getPreferredSize().height);
              status2.setMinimumSize(size);
              status2.setMaximumSize(size);
              status2.setPreferredSize(size);
              status2.setHorizontalAlignment(JLabel.LEFT);
              GridBagLayout gbl = new GridBagLayout();
              GridBagConstraints gbc = new GridBagConstraints();
              setLayout(gbl);
              gbc.insets    = new Insets(2, 0, 0, 4);
              gbc.fill      = GridBagConstraints.HORIZONTAL;
              gbc.anchor    = GridBagConstraints.WEST;
              gbc.gridwidth = 1;
              gbc.weightx   = 1.0;
              gbl.setConstraints(status1, gbc);
              add(status1);
              gbc.insets    = new Insets(2, 0, 0, 0);
              gbc.fill      = GridBagConstraints.NONE;
              gbc.anchor    = GridBagConstraints.WEST;
              gbc.gridwidth = GridBagConstraints.REMAINDER;
              gbc.weightx   = 0.0;
              gbl.setConstraints(status2, gbc);
              add(status2);
         public void setStatus1(String text)
              status1.setText((text == null || text.length() < 1) ? " " : text);
         public void setStatus2(String text)
              status2.setText((text == null || text.length() < 1) ? " " : text);

  • Create status bar...... like explorer or word, excel

    is it possible to create a status bar like other application in which we can see the full path of file ane numlock on --off like that                                                                                                                                                                                                                                                                            

    hi,
    yes you can, just add a jpanel to the bottom (SOUTH if you're using BorderLayout) of your jframe, jdialog, whatever, and place whatever you want on it, such as a jlabel to display a file's full path.

  • To create a status bar

    hai friends,
    how can we create a status bar in a window/frame/textbox.

    I got this answer. Any one have other choices..
    class MenuHelpTextAdapter implements ChangeListener
       private JMenuItem menuItem;
       private String helpText;
       private JLabel statusBar;
       public MenuHelpTextAdapter(JMenuItem menuItem, String helpText, JLabel statusBar)
          this.menuItem = menuItem;
          this.helpText = helpText;
          this.statusBar = statusBar;
          menuItem.addChangeListener(this);
       public void stateChanged(ChangeEvent evt)
          if (menuItem.isArmed())
             statusBar.setText(helpText);
          else
             statusBar.setText(" ");
    }The code that creates the adapters for the menu items is quite simple:
    new MenuHelpTextAdapter(item1, "Help text for item 1", statusBar);
       new MenuHelpTextAdapter(item2, "Item 2 info", statusBar);Using this adapter -- as an inner class in this case -- we can create a complete sample program:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class StatusBarTest
       JLabel statusBar;
       JMenuItem item1;
       JMenuItem item2;
       private void init()
          JFrame frame = new JFrame("Status Bar Test");
          JMenuBar menuBar = new JMenuBar();
          frame.setJMenuBar(menuBar);
          JMenu fileMenu = new JMenu("File");
          menuBar.add(fileMenu);
          item1 = new JMenuItem("Item 1");
          item2 = new JMenuItem("Item 2");
          fileMenu.add(item1);
          fileMenu.add(item2);
          Container contentPane = frame.getContentPane();
          contentPane.setLayout(new BorderLayout());
          contentPane.add("Center", new JButton("Main application area"));
          statusBar = new JLabel(" ");
          contentPane.add("South", statusBar);
          new MenuHelpTextAdapter(item1, "Help text for item 1", statusBar);
          new MenuHelpTextAdapter(item2, "Item 2 info", statusBar);
          frame.setSize(400,400);
          frame.setVisible(true);
       public static void main(String[] args)
          StatusBarTest t = new StatusBarTest();
          t.init();
       class MenuHelpTextAdapter implements ChangeListener
          private JMenuItem menuItem;
          private String helpText;
          private JLabel statusBar;
           public MenuHelpTextAdapter(JMenuItem menuItem, String helpText,
           JLabel statusBar)
             this.menuItem = menuItem;
             this.helpText = helpText;
             this.statusBar = statusBar;
             menuItem.addChangeListener(this);
          public void stateChanged(ChangeEvent evt)
             if (menuItem.isArmed())
                statusBar.setText(helpText);
             else
                statusBar.setText(" ");
    }

  • Dynamic status bar

    I made up a status bar, but couldn't get it to be updated while some other task is being executed, like several printing jobs. The bar is a simple JPanel with a JLabel. I tried to revalidate the panel and another thread.
    What do I have to do to get the message updated?

    but couldn't get it to be updated while some other task is being executed, like several printing jobs.Its probably because you aren't using Threads. If your printing jobs are executing in the Event Thread then the GUI can't be updated until the printing is finished. The printing code should be moved to a separate Thread.

  • Icon on the status bar

    Hello,
    How can place an image on the status bar?
    Also is possible to place it at any position on the status bar?
    -Kathik

    It's like BullJ says, if it's a JLabel you sure can. A status bar is not a Swing feature, you would have to implement it your self (e.g. using a JLabel). So it all depends on the component you're using.
    Rene'

  • Can't create a status bar for my window

    I'm trying to add a status bar to a window I've created. I've tried to get this to work by doing this:
    frame = new JFrame();
    canvas = new CanvasPane();
    frame.setContentPane(canvas);
    frame.setTitle(title);
    canvas.setPreferredSize(new Dimension(width, height));
    backgroundColour = bgColour;
    Container contentPane = frame.getContentPane();
    JLabel statusLabel = new JLabel("This is the status bar");
    contentPane.add(statusLabel, BorderLayout.SOUTH);
    Everything works except for the status bar. What am I doing wrong?

    in the above example, the poster is taking it for granted that you class extends the JFrame class. In case not then you have do as follows:
    myFrameName.getContentPane()."What ever else";Also before starting to put thing using the border layout do as follows:

  • How to put Status bar

    How to put a status bar in a JFrame by which some informations that will be done in the application should be displayed in the status bar.

    Add a JPanel to your JFrame and tell the program this is your contentPane
    Next you set the contentPane's Layout a BorderLayout.
    Last you've to add a JLabel to the contentPane.
    Now you can set a statusText or an Icon...
    hope it helps,
    Thof

  • Jframe on the Status Bar

    I want that JFrame which is currently opened should not appear on the WindowXP task bar.Can any one of u help me.

    To clarify the post above, the JLabel is typically added to the south part of the window. Lowered BevelBorder gives the status bar look familiar to most users.
    Good luck.

Maybe you are looking for