Java Swing Hello World
Let us move on to write our code that will display a Hello World graphics window. We wish to keep the code as small as possible. Our purpose here is to make sure that your environment is set up and working.
Copy paste the following code in a notepad and save it in a file, say, Hello.java
|
Go to your command line and give the following commands to compile and run this program
javac hello.java
java HelloWorldGUI
If everything goes right, you should see a GUI something like
Make changes into the code
Here are some exercises that you may like to take to get a feel of the code.
1. On Line number 10 change
JFrame frame = new JFrame("Hello World Swing ");
to something like
JFrame frame = new JFrame("This is my label");
and see the effect
2. On Line number 14 change
JLabel label = new JLabel("Hello World from ReferenceDesigner.com");
to something line
JLabel label = new JLabel("Some new text inside the window");
3. Comment out the Line Numebr 11 as
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Recompile and run the program. What happens. When you close the Hello World Window, you are stuck at the command.com DOS prompt. You will need to close the DOS command prompt and begin again.
4. On Line number 18, change
frame.setVisible(true );
to
frame.setVisible(false);
what happens ? Although the program runs, it does not display the window. Also if you comment Line number 18, the windows still diplays. The frame visibility is set to true by default.
Understanding the Hello World
By now you mush have understood most of the program by way of making changes. There are couple of other things that need explanation
1. Invokelater is a method in java on swing package and belongs to swingutilities class. Invokelater is used by java swing developer to update or perform any task on Event dispatcher thread asynchronously.invokeLater has been added into Java API from swing extension and belong to SwingUtilities class.
InvokeLater takes a Runnable object and queues it to be processed by EventDispatcher thread ( EDT) . EDT thread will process this request only after sorting out all AWT pending events or requests. Even if invokeLater is called directly form Event dispatches thread processing of Runnable task still be done only after processing all pending AWT Events. An important point to note is that in case if run method of Runnable task throw any exception then AWT Event dispatcher thread will unwind and not the current thread.
2. Notice that the program will still function if you replace the code between lines 23 and 27
|
by just
createAndShowGUI();
3. The next thing we need to understand is the JFrame pack method used in line 17
frame.pack();
The pack method Causes the Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
If you replace the line
frame.pack();
with
frame.setSize(300,200);
you get slightly bigger window - something like this one