Java – autocomplete jtextfield and arrow keys

I'm trying to use javax swing. JList builds a javax swing. Jtextfield, used for automatic completion, such as Google

When you write a word, Google displays several games

>When I press ▼, I can use ▲ and ▼ to select some games > available ◀ and ▶ Edit my input. > When I press enter, search the contents of the box. > When you press ESC, the box changes to the original input

I mean about Bible, I want to find a specific word when I learn this word I saw java2s autotextfield, but there is no special behavior of arrow keys

Solution

This requires a custom encoding component It is definitely a class that extends jtextfield, and there is a JPopupMenu containing JList in this class You must position JPopupMenu directly below the text field to make it look like a component

Your next trick is to filter your input I usually use java6 tablerowsorter in combination with JTable that I fill in data in advance You will need to make some changes to the listener on the jtextfield and intercept each type and get the data

>Press key > execute query in dB (or get similar entries in some data stores) > fill JTable with these contents > set rowfilter using regular expression based on jtextfield entries to filter the retrieved data > manage your operation with key listeners

edit

I whipped a sample swing app to show what I said This is a copy / paste example and should work properly (JDK 1.6 is required) I basically got what you wanted. I put the comments where I told you to fill in the blanks For example, the escape key event is consumed, and you can do anything

The inittablemodel () method simply initializes the table model with data Typically, you will want to dynamically populate the table model with a database or other data Many can be adjusted, but this is the reason for the exception;) So this should be a good example for you to modify to achieve your goal More than that, you have to pay me $$$:)

package test.text.googleclone;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.PatternSyntaxException;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.jpopupmenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Keystroke;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class SearchAutoFillTest {

private JFrame frame = null;
private JTextField searchField = null;
private jpopupmenu popup = null;

private JTable searchTable = null;
private TableRowSorter<DefaultTableModel> rowSorter = null;
private DefaultTableModel searchTableModel = null;

public SearchAutoFilltest() {
    searchTableModel = new DefaultTableModel();
    initTableModel();

    rowSorter = new TableRowSorter<DefaultTableModel>(searchTableModel);
    searchTable = new JTable(searchTableModel);
    searchTable.setRowSorter(rowSorter);
    searchTable.setFillsViewportHeight(true);
    searchTable.getColumnModel().setColumnSelectionAllowed(false);
    searchTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    searchTable.getTableHeader().setReorderingAllowed(false);
    searchTable.setPreferredSize(new Dimension(775,100));
    searchTable.setGridColor(Color.WHITE);

    searchField = new JTextField();
    searchField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
            showPopup(e);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            showPopup(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            showPopup(e);
        }
    });

    searchField.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyReleased(KeyEvent e) {
            int code = e.getKeyCode();
            switch(code)
            {
                case KeyEvent.VK_UP:
                {
                    cycleTableSelectionUp();
                    break;
                }

                case KeyEvent.VK_DOWN:
                {
                    cycleTableSelectionDown();
                    break;
                }

                case KeyEvent.VK_LEFT:
                {
                    //Do whatever you want here
                    break;
                }

                case KeyEvent.VK_RIGHT:
                {
                    //Do whatever you want here
                    break;
                }
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {

        }
    });

    Keystroke keystroke = Keystroke.getKeystroke("ESCAPE");
    searchField.getInputMap().put(keystroke,"ESCAPE");
    searchField.getActionMap().put("ESCAPE",new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //Do what you wish here with the escape key.
        }
    });

    popup = new jpopupmenu();
    popup.add(searchTable);
    popup.setVisible(false);
    popup.setBorder(BorderFactory.createEmptyBorder());

    JPanel searchPanel = new JPanel(new BorderLayout(5,5));
    searchPanel.setBorder(BorderFactory.createEmptyBorder(4,4,4));
    searchPanel.add(searchField,BorderLayout.CENTER);

    frame = new JFrame();
    frame.setLayout(new BorderLayout(5,5));
    frame.add(searchPanel,BorderLayout.NORTH);
    frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800,500);
    center(frame);
    frame.setVisible(true);
}

private final void newFilter() {
    RowFilter<DefaultTableModel,Object> rf = null;

    try {
        rf = RowFilter.regexFilter(getFilterText(),0);
    }
    catch(PatternSyntaxException e) {
        return;
    }

    rowSorter.setRowFilter(rf);
}

private final String getFilterText() {
    String orig = searchField.getText();
    return "("+orig.toLowerCase()+")|("+orig.toUpperCase()+")";
}

private void showPopup(DocumentEvent e) {
    if(e.getDocument().getLength() > 0) {
        if(!popup.isVisible()) { 
            Rectangle r = searchField.getBounds();
            popup.show(searchField,(r.x-4),(r.y+16));
            popup.setVisible(true);
        }

        newFilter();
        searchField.grabFocus();

    }
    else {
        popup.setVisible(false);
    }
}

private void cycleTableSelectionUp() {
    ListSelectionModel selModel = searchTable.getSelectionModel();
    int index0 = selModel.getMinSelectionIndex();
    if(index0 > 0) {
        selModel.setSelectionInterval(index0-1,index0-1);
    }
}

private void cycleTableSelectionDown() {
    ListSelectionModel selModel = searchTable.getSelectionModel();
    int index0 = selModel.getMinSelectionIndex();
    if(index0 == -1) {
        selModel.setSelectionInterval(0,0);
    }
    else if(index0 > -1) {
        selModel.setSelectionInterval(index0+1,index0+1);
    }
}

private void initTableModel() {
    String[] columns = new String[] {"A"};
    String[][] data = new String[][]
    {
        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"},};

    searchTableModel.setDataVector(data,columns);
}

private void center(Window w) {
    int screenWidth  = Toolkit.getDefaultToolkit().getScreenSize().width;
    int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

    int windowWidth = w.getWidth();
    int windowHeight = w.getHeight();

    if (windowHeight > screenHeight) {
        return;
    }

    if (windowWidth > screenWidth) {
        return;
    }

    int x = (screenWidth - windowWidth) / 2;
    int y = (screenHeight - windowHeight) / 2;

    w.setLocation(x,y);
}

public static void main(String ... args) {
    new SearchAutoFilltest();
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>