Libraryless. Click here for Pure Java version (1100L/8K/31K).
1 | !7 |
2 | |
3 | p-awt { |
4 | new Test().initUI(); |
5 | } |
6 | |
7 | sclass Test { |
8 | class SuggestionPanel { |
9 | private JList list; |
10 | private JPopupMenu popupMenu; |
11 | private String subWord; |
12 | private final int insertionPosition; |
13 | |
14 | public SuggestionPanel(JTextArea textarea, int position, String subWord, Point location) { |
15 | this.insertionPosition = position; |
16 | this.subWord = subWord; |
17 | popupMenu = new JPopupMenu(); |
18 | popupMenu.removeAll(); |
19 | popupMenu.setOpaque(false); |
20 | popupMenu.setBorder(null); |
21 | popupMenu.add(list = createSuggestionList(position, subWord), BorderLayout.CENTER); |
22 | popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0) + location.y); |
23 | } |
24 | |
25 | public void hide() { |
26 | popupMenu.setVisible(false); |
27 | if (suggestion == this) { |
28 | suggestion = null; |
29 | } |
30 | } |
31 | |
32 | private JList createSuggestionList(final int position, final String subWord) { |
33 | Object[] data = new Object[10]; |
34 | for (int i = 0; i < data.length; i++) { |
35 | data[i] = subWord + i; |
36 | } |
37 | JList list = new JList(data); |
38 | list.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); |
39 | list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
40 | list.setSelectedIndex(0); |
41 | list.addMouseListener(new MouseAdapter() { |
42 | @Override |
43 | public void mouseClicked(MouseEvent e) { |
44 | if (e.getClickCount() == 2) { |
45 | insertSelection(); |
46 | } |
47 | } |
48 | }); |
49 | return list; |
50 | } |
51 | |
52 | public boolean insertSelection() { |
53 | if (list.getSelectedValue() != null) { |
54 | try { |
55 | final String selectedSuggestion = ((String) list.getSelectedValue()).substring(subWord.length()); |
56 | textarea.getDocument().insertString(insertionPosition, selectedSuggestion, null); |
57 | return true; |
58 | } catch (BadLocationException e1) { |
59 | e1.printStackTrace(); |
60 | } |
61 | hideSuggestion(); |
62 | } |
63 | return false; |
64 | } |
65 | |
66 | public void moveUp() { |
67 | int index = Math.min(list.getSelectedIndex() - 1, 0); |
68 | selectIndex(index); |
69 | } |
70 | |
71 | public void moveDown() { |
72 | int index = Math.min(list.getSelectedIndex() + 1, list.getModel().getSize() - 1); |
73 | selectIndex(index); |
74 | } |
75 | |
76 | private void selectIndex(int index) { |
77 | final int position = textarea.getCaretPosition(); |
78 | list.setSelectedIndex(index); |
79 | SwingUtilities.invokeLater(new Runnable() { |
80 | @Override |
81 | public void run() { |
82 | textarea.setCaretPosition(position); |
83 | }; |
84 | }); |
85 | } |
86 | } |
87 | |
88 | private SuggestionPanel suggestion; |
89 | private JTextArea textarea; |
90 | |
91 | protected void showSuggestionLater() { |
92 | SwingUtilities.invokeLater(new Runnable() { |
93 | @Override |
94 | public void run() { |
95 | showSuggestion(); |
96 | } |
97 | |
98 | }); |
99 | } |
100 | |
101 | protected void showSuggestion() { |
102 | hideSuggestion(); |
103 | final int position = textarea.getCaretPosition(); |
104 | Point location; |
105 | try { |
106 | location = textarea.modelToView(position).getLocation(); |
107 | } catch (BadLocationException e2) { |
108 | e2.printStackTrace(); |
109 | return; |
110 | } |
111 | String text = textarea.getText(); |
112 | int start = Math.max(0, position - 1); |
113 | while (start > 0) { |
114 | if (!Character.isWhitespace(text.charAt(start))) { |
115 | start--; |
116 | } else { |
117 | start++; |
118 | break; |
119 | } |
120 | } |
121 | if (start > position) { |
122 | return; |
123 | } |
124 | final String subWord = text.substring(start, position); |
125 | if (subWord.length() < 2) { |
126 | return; |
127 | } |
128 | suggestion = new SuggestionPanel(textarea, position, subWord, location); |
129 | awt { textarea.requestFocusInWindow(); } |
130 | } |
131 | |
132 | private void hideSuggestion() { |
133 | if (suggestion != null) { |
134 | suggestion.hide(); |
135 | } |
136 | } |
137 | |
138 | protected void initUI() { |
139 | final JFrame frame = new JFrame(); |
140 | frame.setTitle("Test frame on two screens"); |
141 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
142 | JPanel panel = new JPanel(new BorderLayout()); |
143 | textarea = new JTextArea(24, 80); |
144 | textarea.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); |
145 | textarea.addKeyListener(new KeyListener() { |
146 | |
147 | @Override |
148 | public void keyTyped(KeyEvent e) { |
149 | if (e.getKeyChar() == KeyEvent.VK_ENTER) { |
150 | if (suggestion != null) { |
151 | if (suggestion.insertSelection()) { |
152 | e.consume(); |
153 | final int position = textarea.getCaretPosition(); |
154 | SwingUtilities.invokeLater(new Runnable() { |
155 | @Override |
156 | public void run() { |
157 | try { |
158 | textarea.getDocument().remove(position - 1, 1); |
159 | } catch (BadLocationException e) { |
160 | e.printStackTrace(); |
161 | } |
162 | } |
163 | }); |
164 | } |
165 | } |
166 | } |
167 | } |
168 | |
169 | @Override |
170 | public void keyReleased(KeyEvent e) { |
171 | if (e.getKeyCode() == KeyEvent.VK_DOWN && suggestion != null) { |
172 | suggestion.moveDown(); |
173 | } else if (e.getKeyCode() == KeyEvent.VK_UP && suggestion != null) { |
174 | suggestion.moveUp(); |
175 | } else if (Character.isLetterOrDigit(e.getKeyChar())) { |
176 | showSuggestionLater(); |
177 | } else if (Character.isWhitespace(e.getKeyChar())) { |
178 | hideSuggestion(); |
179 | } |
180 | } |
181 | |
182 | @Override |
183 | public void keyPressed(KeyEvent e) { |
184 | |
185 | } |
186 | }); |
187 | panel.add(textarea, BorderLayout.CENTER); |
188 | frame.add(panel); |
189 | frame.pack(); |
190 | frame.setVisible(true); |
191 | } |
192 | } |
from https://stackoverflow.com/questions/10873748/how-to-show-autocomplete-as-i-type-in-jtextarea
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1008913 |
Snippet name: | JTextArea auto-complete test |
Eternal ID of this version: | #1008913/1 |
Text MD5: | 6580653918ed28f5fd3b45cd35846273 |
Transpilation MD5: | 6fd2efce8d1124deac6d5d5f6ca9ecd8 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-06-18 17:00:15 |
Source code size: | 6398 bytes / 192 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 451 / 548 |
Referenced in: | [show references] |