KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > jnlp > GUI


1 package hudson.jnlp;
2
3 import javax.swing.UIManager JavaDoc;
4 import javax.swing.UnsupportedLookAndFeelException JavaDoc;
5 import javax.swing.JPanel JavaDoc;
6 import javax.swing.JComponent JavaDoc;
7 import javax.swing.Icon JavaDoc;
8 import javax.swing.JLabel JavaDoc;
9 import java.awt.GridBagConstraints JavaDoc;
10 import java.awt.GridBagLayout JavaDoc;
11 import java.awt.Dimension JavaDoc;
12
13 /**
14  * GUI related helper code
15  * @author Kohsuke Kawaguchi
16  */

17 public class GUI {
18     /**
19      * Sets to the platform native look and feel.
20      *
21      * see http://javaalmanac.com/egs/javax.swing/LookFeelNative.html
22      */

23     public static void setUILookAndFeel() {
24         try {
25             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
26         } catch (InstantiationException JavaDoc e) {
27         } catch (ClassNotFoundException JavaDoc e) {
28         } catch (UnsupportedLookAndFeelException JavaDoc e) {
29         } catch (IllegalAccessException JavaDoc e) {
30         }
31     }
32
33     // Set up contraints so that the user supplied component and the
34
// background image label overlap and resize identically
35
private static final GridBagConstraints JavaDoc gbc;
36
37     static {
38         gbc = new GridBagConstraints JavaDoc();
39         gbc.gridx = 0;
40         gbc.gridy = 0;
41         gbc.weightx = 1.0;
42         gbc.weighty = 1.0;
43         gbc.fill = GridBagConstraints.BOTH;
44         gbc.anchor = GridBagConstraints.NORTHWEST;
45     }
46
47     public static JPanel JavaDoc wrapInBackgroundImage(JComponent JavaDoc component,
48              Icon JavaDoc backgroundIcon,
49              int verticalAlignment,
50              int horizontalAlignment) {
51
52          // make the passed in swing component transparent
53
component.setOpaque(false);
54
55          // create wrapper JPanel
56
JPanel JavaDoc backgroundPanel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
57
58          // add the passed in swing component first to ensure that it is in front
59
backgroundPanel.add(component, gbc);
60
61          // create a label to paint the background image
62
JLabel JavaDoc backgroundImage = new JLabel JavaDoc(backgroundIcon);
63
64          // set minimum and preferred sizes so that the size of the image
65
// does not affect the layout size
66
backgroundImage.setPreferredSize(new Dimension JavaDoc(1,1));
67          backgroundImage.setMinimumSize(new Dimension JavaDoc(1,1));
68
69          // align the image as specified.
70
backgroundImage.setVerticalAlignment(verticalAlignment);
71          backgroundImage.setHorizontalAlignment(horizontalAlignment);
72
73          // add the background label
74
backgroundPanel.add(backgroundImage, gbc);
75
76          // return the wrapper
77
return backgroundPanel;
78      }
79 }
80
Popular Tags