[英]Disable up and down arrow buttons on JSpinner
I have a JSpinner on which I would like to take control of when editing is enabled.我有一个 JSpinner,我想在启用编辑时控制它。 It's easy enough with the keyboard, but how about those little arrow widgets at the side?
使用键盘很容易,但是侧面的那些小箭头小部件怎么样? I can't even find references to them in the JSpinner source or any of its enclosed classes.
我什至无法在 JSpinner 源代码或其任何封闭类中找到对它们的引用。
You can use setUI()
method to hide Jspinner arrow.您可以使用
setUI()
方法隐藏 Jspinner 箭头。
public void hideSpinnerArrow(JSpinner spinner) {
Dimension d = spinner.getPreferredSize();
d.width = 30;
spinner.setUI(new BasicSpinnerUI() {
protected Component createNextButton() {
return null;
}
protected Component createPreviousButton() {
return null;
}
});
spinner.setPreferredSize(d);
}
As you see, just make createNextButton()
and createPreviousButton()
return null
.如您所见,只需让
createNextButton()
和createPreviousButton()
返回null
。
Because we used a BasicUI so we have to setup Spinner size again.因为我们使用了 BasicUI,所以我们必须再次设置 Spinner 大小。 I used
PreferredSize
.我使用了
PreferredSize
。
you can't take control of these two arrow buttons but you can do like this你不能控制这两个箭头按钮,但你可以这样做
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
p.setEnabled(false);
}
suppose you want a button pressed and the user will not able to use jspinner at all this is a hint for more actions you can modified it as well假设您想要按下一个按钮,而用户根本无法使用 jspinner,这是一个提示,您可以进行更多操作,您也可以对其进行修改
you can also allow use to use the jspinner untill for a specific value using您还可以允许使用 jspinner 直到特定值使用
if(spinner.getValue()==10){
//show error message and
spinner.setEnabled(false);
}
If the UI class used derives from BasicSpinnerUI
, the arrow buttons can be removed with:如果使用的 UI 类派生自
BasicSpinnerUI
,则可以使用以下命令删除箭头按钮:
for (Component component : spinner.getComponents()) {
if (component.getName() != null && component.getName().endsWith("Button")) {
spinner.remove(component);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.