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
JCombobox editable enabled
提问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 JComboBox
allows text entry in addition to selecting a value via pull-down.
setEditable(boolean)
确定JComboBox
除了通过下拉选择值之外是否还允许文本输入。
setEnabled(boolean)
determines if the JComboBox
is able to be interacted with at all. If it is not enabled, it is displayed as grayed out.
setEnabled(boolean)
确定是否JComboBox
能够与之交互。如果未启用,则显示为灰色。
A JComboBox
can have any mix of these properties -
AJComboBox
可以具有这些属性的任意组合 -
setEditable(true)
+setEnabled(true)
=JComboBox
allows text input in addition to pull down values and user can interact with it.setEditable(false)
+setEnabled(true)
=JComboBox
only allows values from the pull down to be selected and user can interact with it.setEditable(true)
+setEnabled(false)
=JComboBox
allows text input in addition to pull down values but user cannot interact with it.setEditable(false)
+setEnabled(false)
=JComboBox
only 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 JComboBox
with setEnabled(false)
and setEditable(true)
would be where you want a JComboBox
that allows text input, but the form is in a state where the value of the JComboBox
isn't applicable. You would usually have some action that would call setEnabled(true)
on the JComboBox
once it does become applicable.
在这种情况下,您可能有一个JComboBox
withsetEnabled(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 JComboBox
for the brand of car and a JTextFied
for the license plate number. You may have the JComboBox
pre-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 JCheckBox
signifying that they need a parking space. You would add a listener to the JCheckBox
that would call setEnabled(true)
on the JComboBox
and JTextField
when it was selected, and setEnabled(false)
when it wasn't.
例如,如果您有学生住房表格之类的东西,表格上可能会出现“您需要停车位吗?”这样的问题。与JCheckbox
. 有一个JComboBox
汽车品牌和一个JTextFied
车牌号。您可能已经JComboBox
预先填充了常见的汽车品牌 - 福特、雪佛兰、丰田、本田等 - 但决定您还希望允许它可编辑,以防有人拥有兰博基尼之类的东西(并且住在学生公寓中)对对对……)。除非用户选择JCheckBox
表示他们需要停车位,否则不需要汽车品牌和车牌号的值。您将一个监听器添加到JCheckBox
这将要求setEnabled(true)
对JComboBox
与JTextField
被选中时,并且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)
,则整个控件将被禁用,从而完全阻止用户与其进行交互。