Reputation: 14318
I am trying to write a simple desktop application for managing accounts. I am in the beginning phase.
I am running it in my old computer and I sometimes get strange behavior.
Java Version
java version "1.6.0_05" Java(TM) SE Runtime Environment (build 1.6.0_05-b13) Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
My computer configuration
Microsoft Windows XP SP2 Intel(R) Celeron(R) CPU 2.53GHz 1.96GHz 736MB of RAM
I get strange behaviour. When I compile my program and the jframe loads, i sometimes get menu and sometimes I don't get menu as shown in the figure. Also when I try to resize my jframe, jframe shows menu.
My code
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.AWTException;
public class Main {
public static void main(String[] args) {
new Login();
}
}
class Login extends JFrame{
private int height=450;
private int width=300;
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
JTextArea textArea1, textArea2;
public Login()
{
initWindow();
initMenu();
}
private void initWindow()
{
setVisible(true);
setSize(this.height, this.width);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
setLocation((dim.width-this.width)/2, (dim.height-this.height)/2);
Image image = toolkit.createImage("account.gif");
setIconImage(image);
}
private void initMenu()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
JMenuItem configureAction = new JMenuItem("Configure");
JMenuItem exitAction = new JMenuItem("Exit");
fileMenu.add(configureAction);
fileMenu.add(exitAction);
JMenuItem helpAction = new JMenuItem("Help");
JMenuItem aboutAction = new JMenuItem("About");
helpMenu.add(helpAction);
helpMenu.add(aboutAction);
}
}
Window with no menu
Window with menu
Any suggestion to improve code with be highly appreciated.
Thank you
Upvotes: 0
Views: 211
Reputation: 11986
Note that you get this erratic behaviour (rather than a consistent fail or consistent success) because of what “events” are delivered. For instance if you mouse over where the menubar is, or change window sizes you will see bits being painted “properly” because the underlying graphics stack detected these events and marked the affected regions for update.
You can mark GUI components for update as well using repaint()
and/or revalidate()
.
Note that the above doesn't explain why your code did not work, the reason for that is as explained by the first answer, that you made the window visible before it was realised.
A few other tips: it's good practice to ensure all GUI creation occurs on the AWT EventQueue, by using something like
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// call code which creates the GUI in here
}
}
Or the SwingUtilities.invokeLater()
version.
Additionally, it is a good idea to call pack()
on the frame before making it visible, since this will make the Window size it components properly and validate them (and without such validation, scrollpanes for instance won't update the scrollbars properly).
Upvotes: 2
Reputation: 28568
Don't call setVisible(true)
until the window is completely built, ie. do that statement as the last thing you do with the window.
Once the window is visible, any changes you make to the window must be done on the GUI thread.
Upvotes: 1