Java JCombobox 可编辑启用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1594122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:33:07  来源:igfitidea点击:

JCombobox editable enabled

javajcombobox

提问by Fortega

What is the difference between the setEditable() and the setEnabled() in a jCombobox? Can a combobox be editable but not enabled and other way around? In which situation would you use which method?

jCombobox 中的 setEditable() 和 setEnabled() 有什么区别?组合框可以编辑但不能启用吗?在哪种情况下会使用哪种方法?

Can you imagine a situation in which you would do setEnabled(false) together with setEditable(true)?

您能想象一下将 setEnabled(false) 与 setEditable(true) 一起执行的情况吗?

采纳答案by Nate

setEditable(boolean)determines if the JComboBoxallows text entry in addition to selecting a value via pull-down.

setEditable(boolean)确定JComboBox除了通过下拉选择值之外是否还允许文本输入。

setEnabled(boolean)determines if the JComboBoxis able to be interacted with at all. If it is not enabled, it is displayed as grayed out.

setEnabled(boolean)确定是否JComboBox能够与之交互。如果未启用,则显示为灰色。

A JComboBoxcan have any mix of these properties -

AJComboBox可以具有这些属性的任意组合 -

  • setEditable(true)+ setEnabled(true)= JComboBoxallows text input in addition to pull down values and user can interact with it.
  • setEditable(false)+ setEnabled(true)= JComboBoxonly allows values from the pull down to be selected and user can interact with it.
  • setEditable(true)+ setEnabled(false)= JComboBoxallows text input in addition to pull down values but user cannot interact with it.
  • setEditable(false)+ setEnabled(false)= JComboBoxonly allows values from the pull down to be selected and user cannot interact with it.
  • setEditable(true)+ setEnabled(true)=JComboBox除了下拉值之外还允许文本输入,并且用户可以与之交互。
  • setEditable(false)+ setEnabled(true)=JComboBox只允许选择下拉菜单中的值并且用户可以与之交互。
  • setEditable(true)+ setEnabled(false)=JComboBox除了下拉值之外还允许文本输入,但用户无法与之交互。
  • setEditable(false)+ setEnabled(false)=JComboBox只允许选择下拉菜单中的值,用户无法与之交互。

A situation where you may have a JComboBoxwith setEnabled(false)and setEditable(true)would be where you want a JComboBoxthat allows text input, but the form is in a state where the value of the JComboBoxisn't applicable. You would usually have some action that would call setEnabled(true)on the JComboBoxonce it does become applicable.

在这种情况下,您可能有一个JComboBoxwithsetEnabled(false)并且setEditable(true)将是您想要JComboBox允许文本输入的地方,但表单处于 的值JComboBox不适用的状态。你通常会有些行动,将调用setEnabled(true)JComboBox一次它开始适用。

For example, if you have something like a student housing form, there may be a question on the form like 'Do you need a parking space?' with a JCheckbox. There's a JComboBoxfor the brand of car and a JTextFiedfor the license plate number. You may have the JComboBoxpre-populated with common car brands - Ford, Chevy, Toyota, Honda, etc. - but decide you also want to allow it to be editable in case someone owns something like a Lamborghini (and is staying in student housing - yeah, right...). The value for car brand and license plate number aren't needed unless the user selects the JCheckBoxsignifying that they need a parking space. You would add a listener to the JCheckBoxthat would call setEnabled(true)on the JComboBoxand JTextFieldwhen it was selected, and setEnabled(false)when it wasn't.

例如,如果您有学生住房表格之类的东西,表格上可能会出现“您需要停车位吗?”这样的问题。与JCheckbox. 有一个JComboBox汽车品牌和一个JTextFied车牌号。您可能已经JComboBox预先填充了常见的汽车品牌 - 福特、雪佛兰、丰田、本田等 - 但决定您还希望允许它可编辑,以防有人拥有兰博基尼之类的东西(并且住在学生公寓中)对对对……)。除非用户选择JCheckBox表示他们需要停车位,否则不需要汽车品牌和车牌号的值。您将一个监听器添加到JCheckBox这将要求setEnabled(true)JComboBoxJTextField被选中时,并且setEnabled(false)当它不是。

回答by TBH

SetEnable() - Enables the combo box so that items can be selected.

SetEnable() - 启用组合框以便可以选择项目。

SetEditable() - Determines whether the JComboBox field is editable.

SetEditable() - 确定 JComboBox 字段是否可编辑。

回答by SLaks

If you call setEditable(true), the JComboBox's text field becomes editable, allowing the user to type text with the keyboard in addition to selecting an item from the list.

如果您调用setEditable(true),JComboBox 的文本字段将变为可编辑,允许用户除了从列表中选择一个项目之外,还可以使用键盘输入文本。

If you call setEnabled(false), the entire control becomes disabled, preventing the user from interacting with it at all.

如果您调用setEnabled(false),则整个控件将被禁用,从而完全阻止用户与其进行交互。