Refresh ListBox After Button clicked - java

Basically I have a page with a button and listbox on it. When the button is clicked, I use a ClickHandler to add another item to the listbox. However, the listbox is not refreshed unless I use the browser refresh button. Is there a way to do this programmatically without refreshing the entire Window?
Thank you

The following code works for me without any manual refresh (tested on Firefox 3.6.12 and Safari 5.0.2 with GWT 2.0.3):
public void onModuleLoad() {
final RootPanel rootPanel = RootPanel.get();
final ListBox listBox = new ListBox();
listBox.addItem("Alpha");
rootPanel.add(listBox);
final Button button = new Button("Button");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
listBox.addItem("Beta");
}
});
rootPanel.add(button);
}
Please test, if my code works for you, too. Is there something special about your code (or maybe you're using a different browser that behaves differently?)

Related

popuppanel is called by clickhandler of hyperlink on another popuppanel

I have 2 gwt PopupPanel in one class. The second popup is expected to be shown when the user clicks the hyperlink on popuppanel1. How can I fix this problem? Thanks
Problem:
Click the hyperlink on popuppanel1
- the popuppanel2 is not shown at all.
PopupPanel popuppanel1=new PopupPanel();
PopupPanel popuppanel2=new PopupPanel();
//popuppanel1 and popuppanel2 add their components
popuppanel1.getHyperlink().addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
popuppanel2.center();
}
});

Cannot add GWT event handler

There is a problem with my code where GWT event handlers are not fired after Panel is appended to a DIV
final FormPanel form = new FormPanel();
VerticalPanel panel = new VerticalPanel();
// Other code omitted for simplicity
DOM.getElementById("modal_panel").appendChild(panel.getElement());
// Submit button
panel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.log("Submit clicked");
form.submit();
}
}));
When the submit button is clicked it should do the form submit action, but it even doesn't show the GWT log on the DevMode console. What could be the reason?
It is not a good idea to add a widget directly in the DOM. GWT does a lot of things, when adding a widget. Adding elements by hand to the DOM will cause some problems. One of the problem is, that the events are not sinked and handlers will not work.
Try something like this:
DOM.sinkEvent(panel.getElement(), Event.ONCLICK);
after adding the panel.
If you want to add your panel to an existing HTML page, try:
RootPanel.get("modal_panel").add(panel);

Can I put a JavaScript button next to the GWT button?

Is it possible to create a horizontal panel with 2 buttons, one in GWT and one in JavaScript?
For example, I have this object:
HorizontalPanel panelHeader = new HorizontalPanel();
Button buttonexample = new Button();
Now, I have created a .js file with the button:
function javascriptbutton(){
document.write('<input type="button" name="try" value="try">');
}
and created a jsni method to call
public native static void javascriptTest() /*-{
$wnd.javascriptbutton(); // JSNI
}-*/;
My question is: how can I add the jsni method that contains a button on horizontal panel? Usually for GWT, I do panelheadr.add(button), but how can I do it for a javascript button?
You have to separate JS from HTML.
Create both buttons in GWT. Assign an id to your button that needs an external JavaScript. Attach a click handler to your button, and call your native JS from this click handler:
myButton.getElement().setId("jsButton");
myButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Here you call your JSNI native method
javascriptTest();
}
});
In your native JS method you can reference this button by its id.
Creating such a simple widget like Button in javascript and using in GWT should be your last resort and tricky as it might cause a cross-browser compatibility issues. In addition it will be difficult if you have to debug that code. Anyway there is a better solution.
Your custom button should be a GWT widget in order to add it to layout containers. That means the button class should implement at least IsWidget this allows the layout container which the button will be added to call asWidget method and return a simple wrapper container for button which allows you to layout the button easily. The following example uses a SimplePanel as the wrapper.
public class MyTextButton implements IsWidget, HasClickHandlers
{
private SimplePanel panel = new SimplePanel();
private Button button;
public MyTextButton()
{
button = Button.create(panel.getElement());
}
#Override
public Widget asWidget()
{
return panel;
}
//Use JavaScriptObject to create the DOM element for button which resides in the SimplePanel
private static class Button extends JavaScriptObject
{
private static native Button create(Element parent)/*-{
//create your javascript button here using parent element as the wrapper.
//ex: if your function is correct
//$wnd.javascriptbutton(parent);
}-*/;
}
#Override
public void fireEvent(GwtEvent<?> event)
{
}
#Override
public HandlerRegistration addClickHandler(ClickHandler handler)
{
//bind this handler to native button's onClick, return the registration for removal, on remove you need to reset the onClick
return null;
}
}
But ideally the MyTextButton class can be extended by UIObject or any other specific class if you want to get built in features.

one button and two differents views

I'm building my application applying MVC pattern.Following this guide mvc guide, I would make an application made of a button.when I press button appear me another view when I repress the button appear me the previously view.how can I made ?some advices?
Well Button will act as the Controller here........
If you want always to show the same View again and again, by repressing the Button, use Singleton Principle
If not, you can initialize a new View again, from within the onClick() method of ActionListener...
Edited:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
new Frame(); // Creates a new frame
}
});

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

Categories