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

  • Home
  • SEARCH
  • 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 5841197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:51:31+00:00 2026-05-22T11:51:31+00:00

I have strange problem. I have class which behaves similar dropdown list. package test.view;

  • 0

I have strange problem. I have class which behaves similar dropdown list.

package test.view;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.List;

    import javax.swing.ComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.JTextComponent;
    import javax.swing.text.PlainDocument;

    public class AutoCompleteJComboBoxer extends PlainDocument   {
        private JComboBox comboBox;
        private ComboBoxModel model;
        private JTextComponent editor;
        private boolean hidePopupOnFocusLoss;

        public AutoCompleteJComboBoxer(JComboBox comboBox) {
            this.comboBox = comboBox;
            comboBox.setEditable(true);
            model = comboBox.getModel();
            editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
            editor.setDocument(this);
            // Bug 5100422 on Java 1.5: Editable JComboBox won't hide popup when tabbing out
            hidePopupOnFocusLoss = System.getProperty("java.version").startsWith("1.5");
            // Highlight whole text when focus gets lost
            editor.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    highlightCompletedText(0);
                    // Workaround for Bug 5100422 - Hide Popup on focus loss
                    if (hidePopupOnFocusLoss) AutoCompleteJComboBoxer.this.comboBox.setPopupVisible(false);
                }
            });
            // Highlight whole text when user hits enter
            editor.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(java.awt.event.KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        highlightCompletedText(0);
                    } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                        AutoCompleteJComboBoxer.this.comboBox.setSelectedIndex(0);
                        AutoCompleteJComboBoxer.this.editor.setText(AutoCompleteJComboBoxer.this.comboBox.getSelectedItem().toString());
                        highlightCompletedText(0);
                    }
                }
            });


            // Handle initially selected object
            Object selected = comboBox.getSelectedItem();
            if (selected != null) editor.setText(selected.toString());
        }


        public AutoCompleteJComboBoxer(List<String> list){
             comboBox = new JComboBox(list.toArray());
             comboBox.setEditable(true);
             comboBox.setMaximumRowCount(8);
             comboBox.setSelectedIndex(-1);

             model = comboBox.getModel();
             editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
             editor.setDocument(this);
             hidePopupOnFocusLoss = System.getProperty("java.version").startsWith("1.5");
             editor.addFocusListener(new FocusAdapter() {
                 @Override
                 public void focusLost(FocusEvent e) {
                     highlightCompletedText(0);
                     if (hidePopupOnFocusLoss) AutoCompleteJComboBoxer.this.comboBox.setPopupVisible(false);
                 }
             });

             editor.addKeyListener(new KeyAdapter() {
                 @Override
                 public void keyPressed(java.awt.event.KeyEvent e) {
                     if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                         highlightCompletedText(0);
                     } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                         AutoCompleteJComboBoxer.this.comboBox.setSelectedIndex(0);
                         AutoCompleteJComboBoxer.this.editor.setText(AutoCompleteJComboBoxer.this.comboBox.getSelectedItem().toString());
                         highlightCompletedText(0);
                     }
                 }
             });

             // Handle initially selected object
             Object selected = comboBox.getSelectedItem();
             if (selected != null) editor.setText(selected.toString());

             editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
             editor.setDocument(this);
             editor.setBackground(Color.WHITE);
        }

        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            // construct the resulting string
            String currentText = getText(0, getLength());
            String beforeOffset = currentText.substring(0, offs);
            String afterOffset = currentText.substring(offs, currentText.length());
            String futureText = beforeOffset + str + afterOffset;

            // lookup and select a matching item
            Object item = lookupItem(futureText);
            if (item != null) {

                    comboBox.setSelectedItem(item);

            } else {
                // keep old item selected if there is no match
                item = comboBox.getSelectedItem();
                // imitate no insert (later on offs will be incremented by str.length(): selection won't move forward)
                offs = offs-str.length();
                // provide feedback to the user that his input has been received but can not be accepted
                comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
            }

            // remove all text and insert the completed string
            super.remove(0, getLength());
            super.insertString(0, item.toString(), a);

            // if the user selects an item via mouse the the whole string will be inserted.
            // highlight the entire text if this happens.
            if (item.toString().equals(str) && offs==0) {
                highlightCompletedText(0);
            } else {
                highlightCompletedText(offs+str.length());
                // show popup when the user type
                comboBox.setPopupVisible(true); // THERE PROGRAM CRASHES



            }
        }

        private void highlightCompletedText(int start) {
            editor.setCaretPosition(getLength());
            editor.moveCaretPosition(start);
        }

        private Object lookupItem(String pattern) {
            Object selectedItem = model.getSelectedItem();
            // only search for a different item if the currently selected does not match
            if (selectedItem != null && startsWithIgnoreCase(selectedItem.toString(), pattern)) {
                return selectedItem;
            } else {
                // iterate over all items
                for (int i=0, n=model.getSize(); i < n; i++) {
                    Object currentItem = model.getElementAt(i);
                    // current item starts with the pattern?
                    if (startsWithIgnoreCase(currentItem.toString(), pattern)) {
                        return currentItem;
                    }
                }
            }
            // no item starts with the pattern =&gt; return null
            return null;
        }

        // checks if str1 starts with str2 - ignores case
        private boolean startsWithIgnoreCase(String str1, String str2) {
            return str1.toUpperCase().startsWith(str2.toUpperCase());
        }

        public void resetColor(){
            this.editor.setBackground(Color.WHITE);
        }
        public void setSuccessfulColor(){
            this.editor.setBackground(Color.GREEN);
        }
        public void setUnsuccessfulColor(){
            this.editor.setBackground(Color.RED);
        }
        public void setColor(Color color){
            this.editor.setBackground(color);
        }
        public JTextComponent getEditor(){
            return this.editor;
        }
        public JComboBox getComboBox(){
            return this.comboBox;
        }
        public void setComboBox(JComboBox comboBox){
            this.comboBox=comboBox;
        }


        private static void createAndShowGUI() {

            JComboBox comboBox = new JComboBox(new Object[] { "clear","delete", "read", "read_all", "write","write_all","a","b","c"});
            comboBox.setEditable(true);
            comboBox.setMaximumRowCount(8);
            comboBox.setSelectedIndex(-1);
            JTextComponent editor = (JTextComponent)comboBox.getEditor().getEditorComponent();
            editor.setDocument(new AutoCompleteJComboBoxer(comboBox));
            editor.setBackground(Color.WHITE);

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(comboBox);
            frame.pack(); frame.setVisible(true);
        }


        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }



    }

I put JComboBox from that class ( member ) in

DefaultCellEditor dceName=new DefaultCellEditor(cmbName);

and put that in JTable, but when I click, I get error

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)
    at java.awt.Component.getLocationOnScreen(Unknown Source)
    at javax.swing.JPopupMenu.show(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup.show(Unknown Source)
    at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(Unknown Source)
    at javax.swing.JComboBox.setPopupVisible(Unknown Source)
    at bacnet_debug_tool.view.AutoCompleteJComboBoxer.insertString(AutoCompleteJComboBoxer.java:142)

Does anybody have any Idea what is wrong ? I have ordinary JComboBox in JTable and it works.

  • 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-05-22T11:51:32+00:00Added an answer on May 22, 2026 at 11:51 am

    nothing complicated just

    1/ take TableRenderDemo.java from link that I posted to your previous topic
    2/ download Auto complete ComboBox (there are two separate Clases) and both Classes save to your project

    then just amend/change whole void public void setUpSportColumn(JTable table, TableColumn sportColumn) {...} from the TableRenderDemo.java and replace with this code

    public void setUpSportColumn(JTable table, TableColumn sportColumn) {
        //Set up the editor for the sport cells.
        ArrayList<String> listSomeString = new ArrayList<String>();        
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
        listSomeString.add("Pool");
        listSomeString.add("None of the above");
        Java2sAutoComboBox comboBox = new Java2sAutoComboBox(listSomeString);
        comboBox.setDataList(listSomeString);
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }
    

    EDIT: maybe better would be use for JTable second Class Java2sAutoTextField.class, looks like much finer in TableCell as JComboBox …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.