Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:36:07+00:00 2026-06-05T02:36:07+00:00

I need to show suggestions (autocomplete) as the user types in a JTextArea ,

  • 0

I need to show suggestions (autocomplete) as the user types in a JTextArea, kind of like cell phone T9.

I don’t know how to do this in myTextAreaKeyTyped() event.

This app is a typing helper. It shows variants of characters non-present on the keyboard.
E.G. You press ‘A’, it shows Â:1, Á:2 ,À:3… ‘A’ will be replaced if you press 1,2 or 3.
It’s already done, but the variants are shown in a JLabel at the bottom of my JFrame, because I don’t know how to do this.

Can you please help me out? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:36:08+00:00Added an answer on June 5, 2026 at 2:36 am

    Here is a snippet to get yourself inspired. You will probably need to reorganize a bit the code to make it more maintainable, but it should give you the gist.

    Basically, we listen for key events (I don’t find it relevant to listen to document events, for example if the user pastes some text, I don’t want the suggestion panel to appear), and when the caret has at least 2 characters behind, we make some suggestions, using a popupmenu containing a JList of suggestions (here suggestions are really not meaningful, but it would not be too hard to bind this to a dictionnary). As for the shortcuts you are mentionning, it should not be too hard to do so.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.JTextArea;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.text.BadLocationException;
    
    public class Test {
    
        public class SuggestionPanel {
            private JList list;
            private JPopupMenu popupMenu;
            private String subWord;
            private final int insertionPosition;
    
            public SuggestionPanel(JTextArea textarea, int position, String subWord, Point location) {
                this.insertionPosition = position;
                this.subWord = subWord;
                popupMenu = new JPopupMenu();
                popupMenu.removeAll();
                popupMenu.setOpaque(false);
                popupMenu.setBorder(null);
                popupMenu.add(list = createSuggestionList(position, subWord), BorderLayout.CENTER);
                popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0) + location.y);
            }
    
            public void hide() {
                popupMenu.setVisible(false);
                if (suggestion == this) {
                    suggestion = null;
                }
            }
    
            private JList createSuggestionList(final int position, final String subWord) {
                Object[] data = new Object[10];
                for (int i = 0; i < data.length; i++) {
                    data[i] = subWord + i;
                }
                JList list = new JList(data);
                list.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                list.setSelectedIndex(0);
                list.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2) {
                            insertSelection();
                        }
                    }
                });
                return list;
            }
    
            public boolean insertSelection() {
                if (list.getSelectedValue() != null) {
                    try {
                        final String selectedSuggestion = ((String) list.getSelectedValue()).substring(subWord.length());
                        textarea.getDocument().insertString(insertionPosition, selectedSuggestion, null);
                        return true;
                    } catch (BadLocationException e1) {
                        e1.printStackTrace();
                    }
                    hideSuggestion();
                }
                return false;
            }
    
            public void moveUp() {
                int index = Math.min(list.getSelectedIndex() - 1, 0);
                selectIndex(index);
            }
    
            public void moveDown() {
                int index = Math.min(list.getSelectedIndex() + 1, list.getModel().getSize() - 1);
                selectIndex(index);
            }
    
            private void selectIndex(int index) {
                final int position = textarea.getCaretPosition();
                list.setSelectedIndex(index);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        textarea.setCaretPosition(position);
                    };
                });
            }
        }
    
        private SuggestionPanel suggestion;
        private JTextArea textarea;
    
        protected void showSuggestionLater() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    showSuggestion();
                }
    
            });
        }
    
        protected void showSuggestion() {
            hideSuggestion();
            final int position = textarea.getCaretPosition();
            Point location;
            try {
                location = textarea.modelToView(position).getLocation();
            } catch (BadLocationException e2) {
                e2.printStackTrace();
                return;
            }
            String text = textarea.getText();
            int start = Math.max(0, position - 1);
            while (start > 0) {
                if (!Character.isWhitespace(text.charAt(start))) {
                    start--;
                } else {
                    start++;
                    break;
                }
            }
            if (start > position) {
                return;
            }
            final String subWord = text.substring(start, position);
            if (subWord.length() < 2) {
                return;
            }
            suggestion = new SuggestionPanel(textarea, position, subWord, location);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    textarea.requestFocusInWindow();
                }
            });
        }
    
        private void hideSuggestion() {
            if (suggestion != null) {
                suggestion.hide();
            }
        }
    
        protected void initUI() {
            final JFrame frame = new JFrame();
            frame.setTitle("Test frame on two screens");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel(new BorderLayout());
            textarea = new JTextArea(24, 80);
            textarea.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
            textarea.addKeyListener(new KeyListener() {
    
                @Override
                public void keyTyped(KeyEvent e) {
                    if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                        if (suggestion != null) {
                            if (suggestion.insertSelection()) {
                                e.consume();
                                final int position = textarea.getCaretPosition();
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            textarea.getDocument().remove(position - 1, 1);
                                        } catch (BadLocationException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
    
                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_DOWN && suggestion != null) {
                        suggestion.moveDown();
                    } else if (e.getKeyCode() == KeyEvent.VK_UP && suggestion != null) {
                        suggestion.moveUp();
                    } else if (Character.isLetterOrDigit(e.getKeyChar())) {
                        showSuggestionLater();
                    } else if (Character.isWhitespace(e.getKeyChar())) {
                        hideSuggestion();
                    }
                }
    
                @Override
                public void keyPressed(KeyEvent e) {
    
                }
            });
            panel.add(textarea, BorderLayout.CENTER);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see this image: I need something like this which will show quote's gradually
I need to show a modal popup if the user has selected a treeview's
I need to show a StatusStrip control docked top instead of bottom. User requirement.
I need to use autocomplete (In particular I try it with this plugin http://scottreeddesign.com/project/jsuggest
I need a way to show a YouTube video on my page. I don't
I need show a notication modal window.. But since its a fluid layout the
me again... I need show 10 or 20 or 50 results number of results
I need to show an integer value in a TextBox in my C# Windows
I need to show the number of results for a given category, and hide
I need to show a page of contents inside my page. ie; when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.