/
MainPanel.java
122 lines (108 loc) · 3.85 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
117
118
119
120
121
122
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.util.stream.Stream;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
String[] columnNames = {"String", "Integer", "Boolean"};
Object[][] data = {
{"aaa", 12, true}, {"bbb", 5, false}, {"CCC", 92, true}, {"DDD", 0, false}
};
TableModel model = new DefaultTableModel(data, columnNames) {
@Override public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model) {
@Override public void updateUI() {
super.updateUI();
TableCellRenderer r = getDefaultRenderer(Boolean.class);
String key = "JComponent.sizeVariant";
Object value = getClientProperty(key);
if (r instanceof JComponent && value != null) {
((JComponent) r).putClientProperty(key, value);
}
}
};
table.setAutoCreateRowSorter(true);
JPanel p1 = new JPanel(new GridLayout(2, 1));
p1.add(new JScrollPane(table));
p1.add(new JScrollPane(new JTree()));
JPanel p2 = new JPanel(new GridLayout(1, 2));
p2.add(new JLabel("abc"));
p2.add(new JCheckBox("def"));
p2.add(new JButton("ghi"));
JMenuBar mb = new JMenuBar();
mb.add(SizeVariantUtils.createSizeVariantMenu());
EventQueue.invokeLater(() -> getRootPane().setJMenuBar(mb));
add(new JSlider(), BorderLayout.NORTH);
add(p1);
add(p2, BorderLayout.SOUTH);
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} 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);
}
}
final class SizeVariantUtils {
private SizeVariantUtils() {
/* Singleton */
}
public static JMenu createSizeVariantMenu() {
JMenu menu = new JMenu("Resizing a Component");
ButtonGroup bg = new ButtonGroup();
Stream.of("regular", "mini", "small", "large")
.forEach(cmd -> menu.add(createSizeVariantItem(cmd, bg)));
return menu;
}
private static JRadioButtonMenuItem createSizeVariantItem(String cmd, ButtonGroup bg) {
JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(cmd, "regular".equals(cmd));
menuItem.addActionListener(e -> setSizeVariant(bg.getSelection().getActionCommand()));
menuItem.setActionCommand(cmd);
bg.add(menuItem);
return menuItem;
}
private static void setSizeVariant(String cmd) {
Stream.of(Window.getWindows()).forEach(window -> {
setSizeVariantAllComponents(window, cmd);
SwingUtilities.updateComponentTreeUI(window);
window.pack();
});
}
private static void setSizeVariantAllComponents(Container me, String cmd) {
if (me instanceof JComponent) {
JComponent jc = (JComponent) me;
jc.setFont(new FontUIResource(jc.getFont()));
jc.putClientProperty("JComponent.sizeVariant", cmd);
}
for (Component c : me.getComponents()) {
if (c instanceof Container) {
setSizeVariantAllComponents((Container) c, cmd);
}
}
}
}