简体   繁体   中英

Java - Allow using backspace in an editable JComboBox with Substance L&F

I am using Substance L&F and I have set a JComboBox to editable so that i can select the value that i want from its popup, or type a new value in its Editor.

Typing a new value works fine, but if i want to delete a miss-typed letter from the Combo editor, and i click Backspace to do that it selects the letters in the editor instead of erasing them. Here is a screenshot :

在此处输入图片说明

I want the Combo editor to work like a JTextField when typing keyboard letters or Backspace or Delete in it, so is there a way to do that ? Or what is causing this?

See below for the importance of an SSCCE not everything that goes wrong is a bug, most of the times it something wrong in your actual code, which without an SSCCE we are non the wiser.

Seems to work fine for me:

On start up:

在此处输入图片说明

After selecting JComboBox and pressing Backspace :

在此处输入图片说明

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


                JComboBox jc = new JComboBox(new String[]{"Hello", "Bye", "World", "Cruel"});
                jc.setEditable(true);
                frame.add(jc);

                frame.pack();
                frame.setVisible(true);

            }
        });
    }

    public static void main(String[] args) {
        new Test();
    }
}

UPDATE

As per your comment:

I did not think it would work fine ! ... It is a problem in Substance; the L&F i am using.

See below for details on the Bug:

Substance: Editable JComboBox does not support backspace key

As stated:

This behavior is by design. This is a "feel" part of Substance that adds auto-completion on editable comboboxes.

Alternatively see my answer here for more L&Fs and/or a way to make your own

UPDATE 2 :

Thanks to @Brad (for deciphering the bug log I linked from Substance :P) to fix this simply do:

UIManager.put( LafWidget.COMBO_BOX_NO_AUTOCOMPLETION, Boolean.TRUE ); 
UIManager.put(LafWidget.COMBO_BOX_NO_AUTOCOMPLETION, Boolean.TRUE);

这项工作对我来说!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM