KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > swing > JFrameUtilities


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.swing;
16
17 import javax.swing.*;
18 import java.awt.event.*;
19 import java.awt.Window JavaDoc;
20 import java.awt.Toolkit JavaDoc;
21 import java.awt.Dimension JavaDoc;
22
23 /**
24  * Swing utility class
25  */

26 public class JFrameUtilities {
27
28     /**
29      * Create a title string from the class name.
30      */

31     public static String JavaDoc title(Object JavaDoc o) {
32         String JavaDoc t = o.getClass().toString();
33         // Remove the word "class":
34
if(t.indexOf("class") != -1)
35             t = t.substring(6);
36         if(t.lastIndexOf(".") != -1)
37             t = t.substring(t.lastIndexOf(".")+1);
38         return t;
39     }
40
41     public static void run(JFrame frame, int width, int height) {
42         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43         frame.setSize(width, height);
44         frame.setVisible(true);
45     }
46
47     public static void run(JApplet applet, int width, int height) {
48         JFrame frame = new JFrame(title(applet));
49         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50         frame.getContentPane().add(applet);
51         frame.setSize(width, height);
52         applet.init();
53         applet.start();
54         frame.setVisible(true);
55     }
56
57     public static void run(JPanel panel, int width, int height) {
58         JFrame frame = new JFrame(title(panel));
59         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60         frame.getContentPane().add(panel);
61         frame.setSize(width, height);
62         frame.setVisible(true);
63     }
64
65     public static void setNativeLookAndFeel() {
66         try {
67           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
68         } catch(Exception JavaDoc e) {
69           System.out.println("Error setting native LAF: " + e);
70         }
71     }
72
73   public static void setJavaLookAndFeel() {
74     try {
75       UIManager.setLookAndFeel
76         (UIManager.getCrossPlatformLookAndFeelClassName());
77     } catch(Exception JavaDoc e) {
78       System.out.println("Error setting Java LAF: " + e);
79     }
80   }
81
82   public static void setMotifLookAndFeel() {
83     try {
84       UIManager.setLookAndFeel
85         ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
86     } catch(Exception JavaDoc e) {
87       System.out.println("Error setting Motif LAF: " + e);
88     }
89   }
90
91   public static void centerWindow(Window JavaDoc window) {
92       Dimension JavaDoc dim = window.getToolkit().getScreenSize();
93       window.setLocation(dim.width/2 - window.getWidth()/2,
94       dim.height/2 - window.getHeight()/2);
95   }
96 }
97
Popular Tags