Java Swing Example

View previous topic View next topic Go down

Java Swing Example

Post by BIT0112-Rokon on Mon Oct 26, 2009 5:19 am

Problem: I want to create a frame window with a specific size and display it on the screen at a specific location.

Solution: You can use the JFrame.setBounds() method define the frame location and size. Then use the setVisible() method to make the frame visible on the screen. The following sample code, JFrameTest.java, shows you how to do this.
Code:

import javax.swing.*;
public class JFrameTest {
     public static void main(String[] a) {  
       JFrame f = new JFrame("Frame Title");
           f.setBounds(50,50,150,150);
       f.setVisible(true);
    }
}
If you run this example, you will get:
Note 1: The size specified in the setBounds() method includes both the content area and the title area of the frame. 

Note 2: After executing the f.setVisible(true) statement, the main() method reaches the end of execution. But the execution of the entire program is not terminated. This is due the fact that the f.setVisible(true) statement actually launches new execution threads, AWT threads. Those threads are still running even after the main thread reaches the end.

Note 3: When you click the close icon, the frame will be removed from the screen. But the execution of the entire program is not terminated. Removing all frames from the screen does not force all AWT threads to end.

Problem: I have a frame window displayed on the screen and I want to close the frame and terminate the application, when user invokes the close command from the window's system menu, or clicks on the close icon from the window's system icon list.
Solution 1: You can use the JFrame.setDefaultCloseOperation() method to change the default behavior option of JFrame responding to the window closing event. Select the EXIT_ON_CLOSE option will terminate the application immediately. Of course, when the application is terminated, all frames will be closed automatically. The following sample code, JFrameClose1.java, shows you how to do this. 
Code:

import javax.swing.*;
     public class JFrameClose1 {
           public static void main(String[] a) {
                  JFrame f = new JFrame();
                  f.setTitle("Closing Frame with Default Close Operation");
                  f.setBounds(100,50,500,300);
                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  f.setVisible(true);
          }
  }
Solution 2: You can extend JFrame class to have your own frame class, so that you can override the default processWindowEvent() method to terminate the application. Of course, calling System.exit(0) is the quickest way to terminate an application. The following sample code, JFrameClose2.java, shows you how to do this.
Code:

import java.awt.event.*;
import javax.swing.*;
public class JFrameClose2  {
      public static void main(String[] a) {
            MyJFrame f = new MyJFrame();
            f.setTitle("Closing Frame with Process Window Event");
            f.setBounds(100,50,500,300);
            f.setVisible(true);
    } 
    static class MyJFrame extends JFrame {
         protected void processWindowEvent(WindowEvent e) {
                  if (e.getID() == WindowEvent.WINDOW_CLOSING) {    
                              System.exit(0);
                   }
         }
    }
}

Solution 3: You can create a new window event listener class and add an object of this listener class to the JFrame object. In the new window event listener, you can implement your owner version of windowClosing() handler method. Of course, the quickest way to create a new window event listener class is to extend the WindowAdapter class. The following sample code, JFrameClose3.java, shows you how to do this.
Code:
import java.awt.event.*;
import javax.swing.*;
public class JFrameClose3  {
       public static void main(String[] a) {
         JFrame f = new JFrame();
         f.setTitle("Closing Frame with Window Listener");
         f.setBounds(100,50,500,300);
         f.addWindowListener(new MyWindowListener());
         f.setVisible(true);
     }
    static class MyWindowListener extends WindowAdapter {
           public void windowClosing(WindowEvent e) { 
                     System.exit(0);
           }
     }
}

For more about it CLICK here.
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum