JFrame *sometimes* updating text on JLabel

M

mas2df

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.
 
A

Andrew Thompson

mas2df said:
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.
 
M

mas2df

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top