java 使用 Apache Wicket 将重点放在一个组件上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2632684/
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
Set focus on a component with Apache Wicket?
提问by vagabond
How do you set focus on a component with Apache Wicket? Searching leads to very little information, mostly on setting the default field. I do not want to set a default field, rather I am looking to set focus when, for example, a specific radio button is selected.
您如何使用 Apache Wicket 将注意力集中在一个组件上?搜索导致的信息很少,主要是关于设置默认字段。我不想设置默认字段,而是希望在例如选择特定单选按钮时设置焦点。
回答by martin-g
I suggest using the native org.apache.wicket.ajax.AjaxRequestTarget#focusComponent(). For example:
我建议使用本机org.apache.wicket.ajax.AjaxRequestTarget#focusComponent(). 例如:
/**
* Sets the focus in the browser to the given component. The markup id must be set. If
* the component is null the focus will not be set to any component.
*
* @param component
* The component to get the focus or null.
*/
org.apache.wicket.ajax.AjaxRequestTarget#focusComponent(Component component)
回答by schmimd04
Once you create your behavior to set the focus, you should be able to add it to the component on any event, just make sure that component is part of the AjaxRequestTarget. I don't see why this wouldn't work...
一旦你创建了你的行为来设置焦点,你应该能够将它添加到任何事件的组件中,只需确保该组件是 AjaxRequestTarget 的一部分。我不明白为什么这不起作用......
myRadioButton.add(new AjaxEventBehavior("onchange") {
@Override
protected void onEvent(AjaxRequestTarget target) {
myOtherComponent.add(new DefaultFocusBehavior());
target.addComponent(myForm);
}
});
Here's a link that shows how to create the default focus behavior if you do not have one already: http://javathoughts.capesugarbird.com/2009/01/wicket-and-default-focus-behavior.html
这是一个链接,它显示了如何创建默认焦点行为(如果您还没有):http: //javathoughts.capesugarbird.com/2009/01/wicket-and-default-focus-behavior.html
回答by Marcelo
If you only want to setFocus through javascript and don't want to reload a form or a component, you can use the following code:
如果只想通过javascript设置Focus,不想重新加载表单或组件,可以使用如下代码:
import org.apache.wicket.Component;
public class JavascriptUtils {
private JavascriptUtils() {
}
public static String getFocusScript(Component component) {
return "document.getElementById('" + component.getMarkupId() + "').focus();";
}
}
And then in any Ajax Method you can use:
然后在任何 Ajax 方法中,您都可以使用:
target.appendJavascript(JavascriptUtils.getFocusScript(componentToFocus));
回答by BlondCode
For a pop-up like modalWindowmy workaroundsolution was to use the attribute "autofocus" on the first input tag. An easy solution is to add it to the html directly.
对于像modalWindow这样的弹出窗口,我的解决方法是在第一个输入标签上使用属性“autofocus”。一个简单的解决方案是直接将其添加到 html 中。
<input ..... autofocus>
Another solution is to add it to the modalWindow itself:
另一种解决方案是将其添加到 modalWindow 本身:
@Override
public void show(AjaxRequestTarget target) {
super.show(target);
setUpFocus();
}
protected void setUpFocus() {
DeepChildFirstVisitor visitor = new DeepChildFirstVisitor() {
@Override
public void component(Component component, IVisit<Void> iVisit) {
if (isAutofocusable(component)) {
component.add(new AttributeAppender("autofocus", ""));
iVisit.stop();
}
}
@Override
public boolean preCheck(Component component) {
return false;
}
};
this.visitChildren(FormComponent.class, visitor);
}
protected boolean isAutofocusable(Component component) {
if (component instanceof TextArea ||
component instanceof DropDownChoice ||
// component instanceof RadioChoice ||
component instanceof AjaxCheckBox ||
component instanceof AjaxButton ||
component instanceof TextField) {
return true;
}
return false;
}
RadioChoice is commented out because this solution is not working on that. For RadioChoice i would recommend to implement a FocusedRadioChoice:
RadioChoice 已被注释掉,因为此解决方案无法解决此问题。对于 RadioChoice,我建议实施 FocusedRadioChoice:
public class FocusedRadioChoice<T> extends RadioChoice<T> {
//constructors...
@Override
protected IValueMap getAdditionalAttributes(int index, T choice) {
super.getAdditionalAttributes(0, choice);
AttributeMap am = new AttributeMap();
am.put("autofocus", "");
return am;
}
}
回答by Volksman
@martin-g 's solution was the only solution that got it working for my scenario - a modal/pop up.
@martin-g 的解决方案是使其适用于我的场景的唯一解决方案 - 模态/弹出窗口。
Note:I think autofocus embedded explicitly in HTML only works on page load, not modal load so any efforts to skillfully set the autofocus attribute in the HTML of a modal just fail miserably - always.
注意:我认为显式嵌入 HTML 中的自动对焦仅适用于页面加载,而不适用于模态加载,因此任何在模态的 HTML 中巧妙设置自动对焦属性的努力都会失败 - 总是如此。
Here I lay out the steps for setting the focus on an input field called 'myInput' using the full power of Wicket (no JS!):
在这里,我列出了使用 Wicket 的全部功能(没有 JS!)将焦点设置在名为“myInput”的输入字段上的步骤:
In onInitialize:
在初始化中:
// Make sure the field has an ID in markup
myInput.setOutoutMarkupId(true);
Provide an overridden show method where you call the focusComponent method:
提供一个覆盖的 show 方法,您可以在其中调用 focusComponent 方法:
public void show(AjaxRequestTarget target)
{
// Make sure you call the super method first!
super.show(target);
target.focusComponent(myInput);
}
This does require that your component is an attribute of your modal content class so that you can access it in the show method. To avoid creating a class attribute for your input component you could blend this solution with the solution from BlondCode by replacing that solution's
这确实要求您的组件是模态内容类的一个属性,以便您可以在 show 方法中访问它。为避免为输入组件创建类属性,您可以将此解决方案与 BlondCode 的解决方案混合,方法是替换该解决方案的
component.add(new AttributeAppender("autofocus", ""));
with
和
target.focusComponent(component);
This also works!
这也有效!
回答by Gregor
Is there a way to achieve the same withoutJavaScript?
有没有办法在没有JavaScript 的情况下实现相同的目标?
(I am implementing a form with a feedback-Panel that only comes up when Javascript is turned off, so it would not make sense to depend on JavaScript there...,-)
(我正在实现一个带有反馈面板的表单,该表单仅在 Javascript 关闭时才会出现,因此在那里依赖 JavaScript 是没有意义的......,-)
I could only find answers which use JS .focs()... maybe Wicket 1.5 will provide a method Component.setFocus()...
我只能找到使用 JS .focs() 的答案...也许 Wicket 1.5 会提供一个方法 Component.setFocus()...
回答by icecreamhead
If you happen to be using an Ajax button, you can simply call target.focusComponent(myComponent);in the button's onSubmitmethod.
如果您碰巧使用了 Ajax 按钮,则只需调用target.focusComponent(myComponent);该按钮的onSubmit方法即可。

