1
2
3
4
5
6
7 package org.astrogrid.ui;
8
9
10 /***
11 * Some convenience routines for making and arranging icon buttons
12 *
13 * @author M Hill
14 */
15
16 import java.awt.Dimension;
17 import javax.swing.BorderFactory;
18 import javax.swing.Icon;
19 import javax.swing.JButton;
20 import org.astrogrid.ui.IconFactory;
21
22 public class IconButtonHelper
23 {
24 /*** Helper conveience method for making the little icon buttons on top right */
25 public static JButton makeIconButton(String title)
26 {
27 return makeIconButton(title, title, null);
28 }
29
30 /*** Helper conveience method for making the little icon buttons on top right */
31 public static JButton makeIconButton(String title, String iconname, String tooltip)
32 {
33 JButton b = null;
34 Icon i = IconFactory.getIcon(iconname, IconFactory.MEDIUM);
35 if (i == null)
36 {
37 b = new JButton(title);
38 }
39 else
40 {
41 b = new JButton(i);
42 b.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
43 int size = Math.max(i.getIconWidth(), i.getIconHeight());
44 b.setPreferredSize(new Dimension(size+2, size+2));
45 }
46
47 b.setToolTipText(tooltip);
48 return b;
49 }
50
51 /*** Runs through making sure they are all the (height) of the largest, and
52 * non-text ones are made the same width
53 * */
54 public static void sizeButtons(JButton[] buttons)
55 {
56
57
58 int size = 0;
59 for (int b=0;b<buttons.length;b++)
60 {
61 if (buttons[b].getIcon() != null) {
62 size = Math.max(size, buttons[b].getIcon().getIconHeight());
63 }
64 if (buttons[b].getText() == null) {
65 if (size<buttons[b].getWidth()) size = buttons[b].getWidth();
66 }
67 }
68
69 for (int b=0;b<buttons.length;b++)
70 {
71 if (buttons[b].getText() == null) {
72 buttons[b].setPreferredSize(new Dimension(size+2, size+2));
73 } else {
74 buttons[b].setSize(buttons[b].getWidth(), size+2);
75 }
76 }
77 }
78
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96