我在TabLayoutPanel中添加了一个按钮,点击按钮后我想显示一些内容,但注意到了。
以下是我正在尝试的示例代码:
public void onModuleLoad() {
final TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM);
final Button sendButton = new Button("Send");
final TextBox nameField = new TextBox();
//Tab
sendButton.addStyleName("sendButton");
nameField.getElement().setId("PID");
sendButton.getElement().setId("sendButton");
String row = searchAnyTransaction(sendButton, nameField);
tabPanel.add(new HTML(row), "Transaaction Status");
tabPanel.add(new HTML("that"), "[that]");
tabPanel.add(new HTML("the other"), "[the other]");
tabPanel.setPixelSize(500, 400);
final RootLayoutPanel rp = RootLayoutPanel.get();
rp.add(tabPanel);
nameField.setFocus(true);
nameField.selectAll();
//On sendButton click , Clickhandler event should call.
// Create a handler for the sendButton and nameField
sendButton.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
rp.add(sendButton);
//how to add dialog box to display
// Want to display something.
}
});
}
public static String searchAnyTransaction(Button sendbutton, TextBox namefield2){
String htmlForm = ""
+ "<TABLE BORDER=\"2\" ><TR><TH height=\"40\" style=\"width:20%;\"><h3 class=“Search” role=\"tab\" aria-expanded=\"true\" aria-selected=\"true\" tabindex=\"0\">Search</h3></TH>"
+ "<TH height=\"40\" style=\"width:20%;\"><label for=\"fname\">PID:</label></TH><TD height=\"40\" style=\"width:50%;\">" + namefield2
+ "<TD height=\"40\" style=\"width:20%;\">"+ sendbutton +"</TD></TR>"
+ "</TABLE>";
return htmlForm;
}
所以我想在按钮点击上显示一些东西。请帮帮我。
答案 0 :(得分:2)
不要将表单构建为字符串。
当您尝试使用字符串连接窗口小部件(Button
或TextBox
)时,将调用窗口小部件的toString()
方法。
"string" + sendbutton + "string"
确实是:
"string" + sendbutton.toString() + "string"
toString()
返回小部件的html代码段,但不包含任何处理程序。这就是ClickHandler
永远不会被执行的原因。
相反,您应该使用某个面板构建表单,例如Grid
或FlexTable
。
例如:
public static Grid searchAnyTransaction(Button sendbutton, TextBox namefield2){
Grid grid = new Grid(1, 4);
grid.setText(0, 0, "Search");
grid.setText(0, 1, "PID:");
grid.setWidget(0, 2, namefield2);
grid.setWidget(0, 3, sendbutton);
return grid;
}
并将其添加到TabPanel,如下所示:
tabPanel.add(searchAnyTransaction(sendButton, nameField), "Transaaction Status");