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

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.

Related

How to make text fields visible upon button click?

I have two text fields that I make invisible when the form is initialised.
What I want to happen is the following.
and then when the button is clicked, they appear like so.
I have tried making the text fields not visible when the form initialised then triggering a action performed event when the button is clicked making the text fields visible again.
import javax.swing.JFrame;
public class Weather extends javax.swing.JFrame {
public Weather() {
initComponents();
this.jTextField3.setVisible(false);
this.jTextField10.setVisible(false);
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(true);
this.jTextField3.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Weather().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
etc..
etc...
}
In C#, this method of making things visible and invisible works but the same logic doesn't apply to java. Nothing happens when I click the button. The two text fields just stay invisible.
When I don't make the text fields not visible when the form is initialised and make them invisible upon button click via the button clicked actionevent method, it works.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(false);
this.jTextField3.setVisible(false);
}
Why can I only make the text fields invisible via button click but I can't make the text fields visible via button click?
That's probably because the window and the GUI was already created with the buttons invisible. When you just set them to visible true it doesn't know how to rewrite them. You can try:
this.revalidate(); //Here this being the jframe
this.repaint();

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

Add Button to gwt widgets

How can i add a button without text only with marker for forward and backward action for example for widget ListBox in gwt? i tried something like this:
Button btn = new Button("Forward");
btn.setHTML(("<img border='0' src='image\\B_forwards.png' />"));
but can't get the picture to show on button.
You don't need a button. Add an Image widget, and add a ClickHandler to it.
If you just want to add an image to a normal GWT Button, then PushButton is the way to go:
PushButton pushButton = new PushButton(new Image("test.png"));
Otherwise, if you just want to have a clickable image, you still can do that:
public interface MyResources extends ClientBundle{
MyResources INSTANCE = GWT.create(AppImages.class);
#Source("image.gif")
ImageResource image();
}
Image image = new Image( MyResources.INSTANCE.image() );
image.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// do whatever you want
}
} );

how to change setIndeterminate jProgressbar in other frame

i have a two frame, frameA and frameB, in frameA i add one button with name buttonA to show frameB and progressbar (setIndeterminate(false)), in frameB i add one button with name buttonB , i want to when i click buttonB, progressbar in frameA.setindeterminate(true)
in frameA
frameB b;
public frameA() {
initComponents();
progressbar.setIndeterminate(false);
b = new frameB();
}
public JProgressBar getProgressbar() {
return progressbar;
}
private void buttonAActionPerformed(java.awt.event.ActionEvent evt)
{
b.setVisible(true);
}
in frameB
i use this code in event buttonB clicked
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
but it doesnt worked
This...
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
Isn't going to work, you've just created another instance of frameA that's not visible. It has no relationship to the frame that is currently open.
There are any number of ways you could achieve this...
You could...
Pass a reference of frameA to frameB as part of the constructor call for frameB. Then in you actionPerformed method, you would simply use this reference to change the progress bar state.
But this would create a tight coupling between frameA and frameB which would greatly reduce the re-use of frameB
You could...
Provide a means by which an interested party could attach an ActionListener to frameB which would be triggered when the button is clicked.
This assumes a work flow and exposes components to outside sources (via the ActionEvent#getSource), which could allow people to change your component...
You probably should...
Fire a PropertyChanged event.
This is probably the easiest and safest of all the options I've come up with. Using a property change listener in this manner means you don't need to expose the JProgressBar, the JButton or produce a tight link between the two frames.
Property change support is built in to Container so all components/controls have it.
For example, you would attach a PropertyChangeListener to b when you construct it.
b = new frameB();
b.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progressState")) {
progressbar.setIndeterminate((Boolean)evt.getNewValue());
}
}
});
Add in bFrame's actionPerformed method, you would simple call...
firePropertyChange("progressState", false, true);
When you want to set the indeterminate state (you could swap the boolean values to reset it if you wanted to)

Refresh ListBox After Button clicked

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?)

Categories