/
MainPanel.java
116 lines (105 loc) · 3.96 KB
/
MainPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Optional;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
// [XP Style Icons - Download](https://xp-style-icons.en.softonic.com/)
String path = "example/wi0124-48.png";
URL url = Thread.currentThread().getContextClassLoader().getResource(path);
Icon icon = Optional.ofNullable(url).map(u -> {
try (InputStream s = u.openStream()) {
return new ImageIcon(ImageIO.read(s));
} catch (IOException ex) {
return UIManager.getIcon("html.missingImage");
}
}).orElseGet(() -> UIManager.getIcon("html.missingImage"));
JLabel l1 = new JLabel("ToolTip icon using JLabel") {
@Override public JToolTip createToolTip() {
JLabel iconLabel = new JLabel(icon);
iconLabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
JToolTip tip = new JToolTip() {
@Override public Dimension getPreferredSize() {
// if (getLayout() == null) {
// Insets i = getInsets();
// Dimension d = iconLabel.getPreferredSize();
// d.width += i.left + i.right;
// d.height += i.top + i.bottom;
// return d;
return getLayout().preferredLayoutSize(this);
}
@Override public void setTipText(String tipText) {
String oldValue = iconLabel.getText();
iconLabel.setText(tipText);
firePropertyChange("tiptext", oldValue, tipText);
}
};
tip.setComponent(this);
tip.setLayout(new BorderLayout());
tip.add(iconLabel);
return tip;
}
};
l1.setToolTipText("Test1");
JLabel l2 = new JLabel("ToolTip icon using MatteBorder") {
@Override public JToolTip createToolTip() {
JToolTip tip = new JToolTip() {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
Insets i = getInsets();
d.height = Math.max(d.height, icon.getIconHeight() + i.top + i.bottom);
return d;
}
};
tip.setComponent(this);
Border b1 = tip.getBorder();
Border b2 = BorderFactory.createMatteBorder(0, icon.getIconWidth(), 0, 0, icon);
Border b3 = BorderFactory.createEmptyBorder(1, 1, 1, 1);
Border b4 = BorderFactory.createCompoundBorder(b3, b2);
tip.setBorder(BorderFactory.createCompoundBorder(b1, b4));
return tip;
}
};
l2.setToolTipText("Test2");
JLabel l3 = new JLabel("ToolTip icon using HTML tags");
l3.setToolTipText(String.format("<html><img src='%s'>Test3</img>", url)); // align='middle'
Box box = Box.createVerticalBox();
box.add(l1);
box.add(Box.createVerticalStrut(20));
box.add(l2);
box.add(Box.createVerticalStrut(20));
box.add(l3);
box.add(Box.createVerticalGlue());
add(box);
setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}