Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

JFrame *sometimes* updating text on JLabel

Thread view: 
mas2df - 11 Dec 2006 05:02 GMT
I am building an application that has the same functionality as Gmail
Notifier using Java Swing components.  The application starts by
showing a login jframe where the user enters username, password, etc.
and then hits a "connect" button.  Then I want to pop up a message box
that says "connecting to the server...".  In the background, an http
request is fired off and when the app receives a response from the
server, the message pop-up will disappear.

I have implemented the pop-up box as a singleton JFrame class that has
a single panel with a JLabel. When the user hits connect, I use setText
to set the "connecting..." message, then do a setVisible to display the
message box.

The problem is that when the pop-up box appears, only *sometimes* does
the "connecting to server..." text appear on it, the other times, a
blank window shows up with no text.

I assume I am missing something to do with setting the visibility, the
focus, or repainting the frame, but I am confused that it *sometimes*
updates properly. But I am wondering if it could also be an environment
problem with my IDE (Eclipse 3.1.2), older Java version (compiling with
1.3), or the multi-threaded nature of the app with all the Timers going
off.

Here is the code for the message pop-up (condensed where possible)
class:
**********************************
public class SystemMessageBox extends JFrame {
    JLabel systemMessageLabel;
    JPanel mainPanel, buttonPanel;

    // Singleton pattern
    private static class SystemMessageBoxHolder {
        private static SystemMessageBox instance = new SystemMessageBox();
    }

    public static SystemMessageBox getInstance() {
        return SystemMessageBoxHolder.instance;
    }

    private SystemMessageBox() {
        super("CLC2S RRTS+ Notifier");
        this.setVisible(false);
        create();
        this.setResizable(false);
        setIconImage(NotifierUtilMethods
                .loadImage(NotifierConstants.CLC2S_LOGO_ICON));
    }

    public void create() {

        // Create the panel to hold request info
        JPanel messagePanel = new JPanel();

        // Create main panel
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.add(messagePanel);

        // Add panel for labels
        systemMessageLabel = new JLabel();
        systemMessageLabel.setHorizontalTextPosition(JLabel.CENTER);
        messagePanel.add(systemMessageLabel);

        setContentPane(mainPanel);
        addWindowListener(new CloseWindowListener());
        pack();
    }

    public void updatePopUp(String message) {

        // updates message
        systemMessageLabel.setText(message);

        pack();
        SystemMessageBox.getInstance().repaint();
        this.requestFocusInWindow();
        SystemMessageBox.getInstance().setVisible(true);
    }
}
**********************************

(*In the update method, I have been throwing the kitchen sink at the
problem, so I realize there could be some redundancy in termns of
repainting, requesting focus, setting visibility, etc.)

Again, sometimes the JLabel updates correctly, other times, a blank
window shows up.

Any ideas, thoughts, or comments?
Thanks.
Andrew Thompson - 11 Dec 2006 05:13 GMT
> I am building an application that has the same functionality as Gmail
> Notifier

What's that?  (And how is the mention of it, relevant
to this this post?)

>.. using Java Swing components.  The application starts
...
> The problem is that when the pop-up box appears, only *sometimes* does
> the "connecting to server..." text appear on it, the other times, a
> blank window shows up with no text.

I see no "connecting to server" text anywhere in the code
snippets you posted.  The code is porbably blocking the EDT,
but it is hard to make further comments, without an SSCCE.
<http://www.physci.org/codes/sscce>

Andrew T.
mas2df - 11 Dec 2006 18:48 GMT
Thanks for the comment.  After further review of Swing, threads, and
the EDT, it was an issue with the EDT and how background tasks were
handled.

Initially, I created a Swing Timer to handle polling the server and put
the code to do that in an ActionListener.  But after looking at this
article (http://www.javapractices.com/Topic160.cjp), I moved the server
polling code out of the  ActionListener and instead created a daemon
thread to handle the polling.

*******Updated Code*********
       public void startPollingTimer() {
             timerBtwPolling = new Timer(pollingInterval, new
PollingListener());
        timerBtwPolling.start();
       }

    private class PollingListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
           Thread worker = new Thread( new PollingWorker() );
           worker.setDaemon(true);
           worker.start();
        }
    }

    private class PollingWorker implements Runnable{
        public void run(){
            PollingController retriever = PollingController.getInstance();
            retriever.startServerPolling();
        }
    }

*******Original Code*********
       public void startPollingTimer() {
             timerBtwPolling = new Timer(pollingInterval, new
PollingListener());
        timerBtwPolling.start();
       }

    private class PollingListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
        PollingController retriever = PollingController.getInstance();
            retriever.startServerPolling();
        }
    }

    private class PollingWorker implements Runnable{
        public void run(){
            PollingController retriever = PollingController.getInstance();
            retriever.startRrtsPolling();
        }
    }

The code's not compilable, but hopefully the idea comes across.  This
fix seems to have cleared up the EDT race conditions that were causing
the Message Pop-Up to sometimes show the updated text.

> > I am building an application that has the same functionality as Gmail
> > Notifier
[quoted text clipped - 14 lines]
>
> Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.