-
Notifications
You must be signed in to change notification settings - Fork 156
/
MainPanel.java
118 lines (103 loc) · 3.78 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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout(5, 5));
String txt1 = "\n1\taaa\n12\tbbb\n123\tccc";
JTextPane textPane = new JTextPane();
textPane.setText("JTextPane:" + txt1);
JTextArea textArea = new JTextArea("JTextArea:" + txt1);
textArea.setTabSize(4);
JPanel p = new JPanel(new GridLayout(2, 1, 5, 5));
p.add(new JScrollPane(textPane));
p.add(new JScrollPane(textArea));
String txt2 = "aaa\tbbb\tccc";
JTextField field1 = new JTextField(txt2, 20);
initActionInputMap(field1);
int tabSize = 4;
Document doc = new PlainDocument();
doc.putProperty(PlainDocument.tabSizeAttribute, tabSize);
JTextField field2 = new JTextField(doc, txt2, 20);
initActionInputMap(field2);
SpinnerNumberModel model = new SpinnerNumberModel(tabSize, -2, 12, 1);
model.addChangeListener(e -> setTabSize(field2, model.getNumber().intValue()));
JSpinner spinner = new JSpinner(model);
Box box = Box.createVerticalBox();
box.add(field1);
box.add(Box.createVerticalStrut(5));
box.add(field2);
box.add(Box.createVerticalStrut(5));
box.add(spinner);
add(p);
add(box, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new Dimension(320, 240));
}
private static void initActionInputMap(JTextComponent editor) {
String mapKey = "insert-horizontal-tab";
editor.getActionMap().put(mapKey, new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
try {
editor.getDocument().insertString(editor.getCaretPosition(), "\t", null);
} catch (BadLocationException ex) {
UIManager.getLookAndFeel().provideErrorFeedback(editor);
}
}
});
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_DOWN_MASK);
editor.getInputMap(WHEN_FOCUSED).put(keyStroke, mapKey);
}
private static void setTabSize(JTextComponent editor, int size) {
Document doc = editor.getDocument();
if (doc != null) {
doc.putProperty(PlainDocument.tabSizeAttribute, size);
// int old = getTabSize(doc);
// firePropertyChange("tabSize", old, size);
// BasicTextUI#modelChanged();
editor.setEditable(false);
editor.setEditable(true);
// or: editor.firePropertyChange("font", 0, 1);
}
}
// private static int getTabSize(Document doc) {
// int size = 8;
// // Document doc = getDocument();
// if (doc != null) {
// Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
// if (i != null) {
// size = i;
// }
// }
// return size;
// }
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);
}
}