1 package net.sf.josceleton.commons.util;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Cursor;
6 import java.awt.Dimension;
7 import java.awt.Font;
8 import java.awt.Toolkit;
9 import java.awt.event.MouseAdapter;
10 import java.awt.event.MouseEvent;
11
12 import javax.swing.JLabel;
13
14
15
16
17 public final class GuiUtil {
18
19 private static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
20
21 private static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
22
23
24 private GuiUtil() {
25
26 }
27
28
29
30
31 public static void setCenterLocation(final Component component) {
32 GuiUtil.setCenterLocation(component, 0, 0);
33 }
34
35
36
37
38 public static void setCenterLocation(final Component component, final int xOffset, final int yOffset) {
39 if(component == null) { throw new IllegalArgumentException("component == null"); }
40 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
41
42 final int x = (screenSize.width - component.getWidth()) / 2;
43 final int y = (screenSize.height - component.getHeight()) / 2;
44
45 component.setLocation(x + xOffset, y + yOffset);
46 }
47
48
49
50
51 @SuppressWarnings("synthetic-access")
52 public static void enableHandCursor(final Component component) {
53 component.addMouseListener(new MouseAdapter() {
54 @Override public void mouseEntered(final MouseEvent event) {
55 component.setCursor(GuiUtil.HAND_CURSOR);
56 }
57 @Override public void mouseExited(final MouseEvent event) {
58 component.setCursor(GuiUtil.DEFAULT_CURSOR);
59 }
60 });
61 }
62
63
64
65
66 public static JLabel newJLabel(final String text, final Font font) {
67 return GuiUtil.newJLabel(text, font, null);
68 }
69
70
71
72
73 public static JLabel newJLabel(final String text, final Font font, final Color fontColor) {
74 final JLabel lbl = new JLabel(text);
75
76 lbl.setFont(font);
77 if(fontColor != null) {
78 lbl.setForeground(fontColor);
79 }
80
81 return lbl;
82 }
83
84
85 }