java – 自动完成JTextField和箭头键

前端之家收集整理的这篇文章主要介绍了java – 自动完成JTextField和箭头键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 javax.swing.JList构建一个javax.swing.JTextField,用于自动完成,如Google.

当写一个字时,Google会显示几场比赛

>当按▼时,我可以使用▲和▼选择一些比赛
>可以用◀和▶编辑我的输入.
>当我按Enter键搜索框中的内容.
>当按Esc键时,盒子将更改为原始输入.

我的意思是关于Bible,我想在我学习这个词时寻找一个特定的单词.我看到了Java2sAutoTextField,但是没有箭头键的这个特殊行为.

解决方法

这需要一个自定义编码组件.绝对是一个扩展JTextField的类,并且在该类中有一个包含JList的JPopupMenu.您必须将JPopupMenu定位在文本字段的正下方,使其看起来像1个组件.

你的下一个诀窍就是在输入时进行过滤.我通常使用Java6 TableRowSorter与我预先填写数据的JTable结合使用.您将需要在JTextField上进行一些变更监听器,并拦截每个键入并获取数据.

>按键
>在DB中执行查询(或者某些数据存储获取类似的条目)
>用这些内容填充JTable
>使用基于JTextField条目的正则表达式设置RowFilter,以过滤检索到的数据
>使用关键听众管理您的操作

编辑

我鞭打了一个样品摇摆应用程序来显示我所说的话.这是一个复制/粘贴示例,应该正常工作(需要JDK 1.6).我基本上得到了你想要的,我把评论放在我告诉你填写空白的地方..例如,Escape键事件被消耗掉,你可以随便做任何事情.

initTableModel()方法只是使用数据初始化表模型.通常,您将要使用数据库或其他数据的动态填充表模型.很多可以调整,但这是例外的缘故;)所以这应该是一个很好的例子,让您修改完成您的目标.不止这个,你必须支付我$$$:)

  1. package test.text.googleclone;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Rectangle;
  7. import java.awt.Toolkit;
  8. import java.awt.Window;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.util.regex.PatternSyntaxException;
  13. import javax.swing.AbstractAction;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17. import javax.swing.JPopupMenu;
  18. import javax.swing.JTable;
  19. import javax.swing.JTextField;
  20. import javax.swing.KeyStroke;
  21. import javax.swing.ListSelectionModel;
  22. import javax.swing.RowFilter;
  23. import javax.swing.event.DocumentEvent;
  24. import javax.swing.event.DocumentListener;
  25. import javax.swing.table.DefaultTableModel;
  26. import javax.swing.table.TableRowSorter;
  27.  
  28. public class SearchAutoFillTest {
  29.  
  30. private JFrame frame = null;
  31. private JTextField searchField = null;
  32. private JPopupMenu popup = null;
  33.  
  34. private JTable searchTable = null;
  35. private TableRowSorter<DefaultTableModel> rowSorter = null;
  36. private DefaultTableModel searchTableModel = null;
  37.  
  38. public SearchAutoFillTest() {
  39. searchTableModel = new DefaultTableModel();
  40. initTableModel();
  41.  
  42. rowSorter = new TableRowSorter<DefaultTableModel>(searchTableModel);
  43. searchTable = new JTable(searchTableModel);
  44. searchTable.setRowSorter(rowSorter);
  45. searchTable.setFillsViewportHeight(true);
  46. searchTable.getColumnModel().setColumnSelectionAllowed(false);
  47. searchTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  48. searchTable.getTableHeader().setReorderingAllowed(false);
  49. searchTable.setPreferredSize(new Dimension(775,100));
  50. searchTable.setGridColor(Color.WHITE);
  51.  
  52. searchField = new JTextField();
  53. searchField.getDocument().addDocumentListener(new DocumentListener() {
  54. @Override
  55. public void changedUpdate(DocumentEvent e) {
  56. showPopup(e);
  57. }
  58.  
  59. @Override
  60. public void insertUpdate(DocumentEvent e) {
  61. showPopup(e);
  62. }
  63.  
  64. @Override
  65. public void removeUpdate(DocumentEvent e) {
  66. showPopup(e);
  67. }
  68. });
  69.  
  70. searchField.addKeyListener(new KeyListener() {
  71. @Override
  72. public void keyTyped(KeyEvent e) {
  73.  
  74. }
  75.  
  76. @Override
  77. public void keyReleased(KeyEvent e) {
  78. int code = e.getKeyCode();
  79. switch(code)
  80. {
  81. case KeyEvent.VK_UP:
  82. {
  83. cycleTableSelectionUp();
  84. break;
  85. }
  86.  
  87. case KeyEvent.VK_DOWN:
  88. {
  89. cycleTableSelectionDown();
  90. break;
  91. }
  92.  
  93. case KeyEvent.VK_LEFT:
  94. {
  95. //Do whatever you want here
  96. break;
  97. }
  98.  
  99. case KeyEvent.VK_RIGHT:
  100. {
  101. //Do whatever you want here
  102. break;
  103. }
  104. }
  105. }
  106.  
  107. @Override
  108. public void keyPressed(KeyEvent e) {
  109.  
  110. }
  111. });
  112.  
  113. KeyStroke keyStroke = KeyStroke.getKeyStroke("ESCAPE");
  114. searchField.getInputMap().put(keyStroke,"ESCAPE");
  115. searchField.getActionMap().put("ESCAPE",new AbstractAction() {
  116. @Override
  117. public void actionPerformed(ActionEvent e) {
  118. //Do what you wish here with the escape key.
  119. }
  120. });
  121.  
  122. popup = new JPopupMenu();
  123. popup.add(searchTable);
  124. popup.setVisible(false);
  125. popup.setBorder(BorderFactory.createEmptyBorder());
  126.  
  127. JPanel searchPanel = new JPanel(new BorderLayout(5,5));
  128. searchPanel.setBorder(BorderFactory.createEmptyBorder(4,4,4));
  129. searchPanel.add(searchField,BorderLayout.CENTER);
  130.  
  131. frame = new JFrame();
  132. frame.setLayout(new BorderLayout(5,5));
  133. frame.add(searchPanel,BorderLayout.NORTH);
  134. frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  135. frame.setSize(800,500);
  136. center(frame);
  137. frame.setVisible(true);
  138. }
  139.  
  140. private final void newFilter() {
  141. RowFilter<DefaultTableModel,Object> rf = null;
  142.  
  143. try {
  144. rf = RowFilter.regexFilter(getFilterText(),0);
  145. }
  146. catch(PatternSyntaxException e) {
  147. return;
  148. }
  149.  
  150. rowSorter.setRowFilter(rf);
  151. }
  152.  
  153. private final String getFilterText() {
  154. String orig = searchField.getText();
  155. return "("+orig.toLowerCase()+")|("+orig.toUpperCase()+")";
  156. }
  157.  
  158. private void showPopup(DocumentEvent e) {
  159. if(e.getDocument().getLength() > 0) {
  160. if(!popup.isVisible()) {
  161. Rectangle r = searchField.getBounds();
  162. popup.show(searchField,(r.x-4),(r.y+16));
  163. popup.setVisible(true);
  164. }
  165.  
  166. newFilter();
  167. searchField.grabFocus();
  168.  
  169. }
  170. else {
  171. popup.setVisible(false);
  172. }
  173. }
  174.  
  175. private void cycleTableSelectionUp() {
  176. ListSelectionModel selModel = searchTable.getSelectionModel();
  177. int index0 = selModel.getMinSelectionIndex();
  178. if(index0 > 0) {
  179. selModel.setSelectionInterval(index0-1,index0-1);
  180. }
  181. }
  182.  
  183. private void cycleTableSelectionDown() {
  184. ListSelectionModel selModel = searchTable.getSelectionModel();
  185. int index0 = selModel.getMinSelectionIndex();
  186. if(index0 == -1) {
  187. selModel.setSelectionInterval(0,0);
  188. }
  189. else if(index0 > -1) {
  190. selModel.setSelectionInterval(index0+1,index0+1);
  191. }
  192. }
  193.  
  194. private void initTableModel() {
  195. String[] columns = new String[] {"A"};
  196. String[][] data = new String[][]
  197. {
  198. new String[] {"a"},new String[] {"aa"},new String[] {"aaab"},new String[] {"aaabb"},new String[] {"aaabbbz"},new String[] {"b"},new String[] {"bb"},new String[] {"bbb"},new String[] {"bbbbbbb"},new String[] {"bbbbbbbeee"},new String[] {"bbbbbbbeeexxx"},new String[] {"ccc"},new String[] {"cccc"},new String[] {"ccccc"},new String[] {"cccccaaaa"},new String[] {"ccccccaaaa"},};
  199.  
  200. searchTableModel.setDataVector(data,columns);
  201. }
  202.  
  203. private void center(Window w) {
  204. int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
  205. int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
  206.  
  207. int windowWidth = w.getWidth();
  208. int windowHeight = w.getHeight();
  209.  
  210. if (windowHeight > screenHeight) {
  211. return;
  212. }
  213.  
  214. if (windowWidth > screenWidth) {
  215. return;
  216. }
  217.  
  218. int x = (screenWidth - windowWidth) / 2;
  219. int y = (screenHeight - windowHeight) / 2;
  220.  
  221. w.setLocation(x,y);
  222. }
  223.  
  224. public static void main(String ... args) {
  225. new SearchAutoFillTest();
  226. }
  227. }

猜你在找的Java相关文章