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