GWT button click event - gwt

i have one window panel in my project.
and i add one button to it.
when i click the button,i want two event to fire.
one event is to hide that window,which i achieve through
Button button = new Button("click");
button.addListener(new ButtonListenerAdapter(){
#Override
public void onClick(Button button, EventObject e) {
hide();
super.onClick(button, e);
}
});
Window.add(button);
and second i want to pop up another window at the same time on the same button click..what to do?
help me out

I think this should solve your problem :
final boolean evenClick = false;
Button button = new Button("click");
button.addListener(new ButtonListenerAdapter(){
#Override
public void onClick(Button button, EventObject e) {
if (!evenClick) {
hide();
super.onClick(button, e);
}
else {
//DO YOUR SECOND CLICK STUFF
}
evenClick = !evenClick;
}
});
Window.add(button);

Related

Add ClickHandler to every button

I am trying to implement a click logging system in GWT, so I know where people are going around my app.
I want to be able to do this automatically with out adding the handler to every single Button?
I tried in a Composite class:
this.addDomHandler(new ClickHandler() {...}, ClickEvent.getType());
But the ClickEvent didn't give me any specifics on what had been clicked. The below didn't work as well.
NodeList<Element> elements = Document.get().getElementsByTagName("a");
EventListener el = new EventListener() {
#Override
public void onBrowserEvent(Event event) {
System.out.println(event.toString());
}
};
for (int i = 0; i < elements.getLength(); i++) {
Element e = elements.getItem(i);
com.google.gwt.user.client.Element castedElem = (com.google.gwt.user.client.Element) e;
DOM.sinkEvents(castedElem, Event.ONCLICK);
DOM.setEventListener(castedElem, el);
}
Any tips?
Take a look here:
Notice every click in a gwt application
This will be called on every click in your applilcation.
So, if you have this code:
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getType().equals("click")) {
Element eventTarget = DOM.eventGetTarget(Event.as(event.getNativeEvent()));
// check if eventTarget is an a-tag
}
}
});
Any time the mouse is clicked, you will get an event. Exame the event to see, if an a-tag is clicked.
Hope that helps.

Old Screen From Activity Gets Displayed When Back Button Used

I guess I do not understand how things like the "Back Button" affect how Activities behave.
Here is my main activity screen shot:
If the user clicks the bookmark icon in the upper right, my BookmarksActivity displays like so:
Here the user clicks the Add button to add the bookmark and I call finish() in my BookmarksActivity class and the screen returns to the my MainActivity ...
Now let's say the user wants to delete a bookmark, they would again click the bookmark icon which presents the user with this screen:
Now the user can click on the delete button resulting in this screen:
Now the user wants to get back to the MainActivity's screen by pressing the Back Button, doing so removes the keyboard as one would expect, resulting in this screen:
But now the user still wants to get back the main screen, so they click the Back Button again, but instead of the main screen one would expect to see they see this one!
Now there is no bookmark in my SQLite database yet one is being displayed. If the user clicks that back button again they do, finally, get the main activity screen ...
... and if they click on the Bookamrk icon you can see that there is no bookmark:
Thanks for bearing with me and that lengthy description of the problem. Here is what I believe to be the pertinent snippets of code:
Here's where the delete happens:
public class BookMarksBaseAdapter extends BaseAdapter {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.bookmark, null);
TextView tv_bookmark_name = (TextView)vi.findViewById(R.id.bookmark_name);
TextView tv_bookmark_clock = (TextView)vi.findViewById(R.id.bookmark_clock);
Button deleteButton = (Button)vi.findViewById(R.id.btn_delete_bookmark);
final bookMark bookmark = new bookMark(data.get(position).get_bookmark_name(), data.get(position).get_bookmark_track(), data.get(position).get_bookmark_clock(), 0);
final String bookmark_name = bookmark.get_bookmark_name();
final int ibookmark_clock = bookmark.get_bookmark_clock();
// Setting all values in listview
tv_bookmark_name.setText(bookmark_name);
tv_bookmark_clock.setText(utils.milliSecondsToTimer(ibookmark_clock));
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d( TAG, "delete button clicked line 73" );
db.deleteBookmark(bookmark);
v.getContext().startActivity(new Intent(v.getContext(), com.redcricket.myApp.BookMarksActivity.class));
}
});
return vi;
}
And here is the an snippet from my BookmarkActivity onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
currentChapterTitle = com.redcricket.myApp.MainActivity.getTrackTitle(0);
currentTrack = com.redcricket.myApp.MainActivity.getCurrentSongIndex();
currentTrackPosition = "00:00:00";
db = new Databasehandler(this);
db.getWritableDatabase();
utils = new Utils();
try {
currentChapterTitle = com.redcricket.myApp.MainActivity.getCurrentTrackTitle();
} catch (Exception e) {
Log.d( TAG, "expection line 27" );
e.printStackTrace();
}
try {
icurrentTrackPosition = com.redcricket.myApp.MainActivity.getCurrentTrackPosition();
currentTrackPosition = utils.milliSecondsToTimer(icurrentTrackPosition);
} catch (Exception e) {
Log.d( TAG, "expection line 34" );
e.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
try {
new_bookmark_name = (EditText) findViewById(R.id.new_bookmark_name);
} catch (Exception e) {
Log.d( TAG, "expection line 43" );
e.printStackTrace();
}
try {
new_bookmark_name.setText( currentChapterTitle );
} catch (Exception e) {
Log.d( TAG, "expection line 49" );
e.printStackTrace();
}
try {
new_bookmark_clock = (TextView) findViewById(R.id.new_bookmark_clock);
} catch (Exception e) {
Log.d( TAG, "expection line 55" );
e.printStackTrace();
}
try {
new_bookmark_clock.setText( currentTrackPosition );
} catch (Exception e) {
Log.d( TAG, "expection line 61" );
e.printStackTrace();
}
try {
addButton = (Button) findViewById(R.id.btn_add_new_bookmark);
} catch (Exception e) {
Log.d( TAG, "expection line 43" );
e.printStackTrace();
}
addButton.setOnClickListener(this);
bookMarkList = db.getAllBookmarks();
// add list
bookmark_list=(ListView)findViewById(R.id.bookmarks_list);
adapter=new BookMarksBaseAdapter(this, bookMarkList, this);
bookmark_list.setAdapter(adapter);
I must be doing something wrong somewhere. I have tried to override the onBackButton method and have it call finish but that didn't help at all. My best guess is that this line in wrong:
v.getContext().startActivity(new Intent(v.getContext(), com.redcricket.myApp.BookMarksActivity.class));
I call that when the delete button get pressed.
Any help welcomed. Thanks!
I am assuming Main is one activity, book mark is another activity and book mark delete is another activity. Unless you tell an activity to have no history or explicitly finish it before going to another activity it will stay in the activity stack.
In your example the user goes MainActivity -> BookMarkActivty then back to MainActivity through on back pressed which removed BookMarkActivity from the stack. Its all good.
In your other example the user goes MainActivity -> BookMarkActivity -> DeleteActivity
the question here is when they click delete are you finishing DeleteActivty or starting a new BookMarkActivty?
It looks like you are starting a new BookMarkActivty, finished the old BookMarkActivity and ended up with a stack of Main - DeleteBookMark - BookMark after the deletion process.
Don't finish bookmark when they choose to do a delete and set the delete activity to have no history or explicitly finish it after delete.
Doh! I figured out what I needed to do. I need to save the Activity that gets passed to the constructor of my BookMarksBaseAdapter class as a private member like so ...
public class BookMarksBaseAdapter extends BaseAdapter {
private Activity activity;
...
BookMarksBaseAdapter (Activity a, ArrayList<bookMark> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
utils = new Utils();
db = new Databasehandler(a);
}
... then I call call activity.finish() when the delete button gets pressed like this ...
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.deleteBookmark(bookmark);
activity.finish();
}
});

Closing dialog on action of button in codename one

I make a dialog in which we have some buttons.
on action of one of that button i want to finish dialog.
I don't want to add any command in it.
Please help.
Here is my code.
Form form = (Form) createContainer("/theme", "MYDialog1");
Container container = (Container) findByName("Container", form);
button = new Button(new Command("Close"),i));
try
{
button.setUIID("LabelButton");
}
catch (Exception exception)
{
exception.printStackTrace();
}
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
??????
}
});
container.addComponent(button);
Dialog.show("", form, null);
If you add a command to a dialog it will dispose the dialog by default.
You can manually invoke dialog.dispose() to close the dialog, to get the current dialog just use Dialog dlg = (Dialog)Display.getInstance().getCurrent();
You Don't any relationship between the dialog and the container. Because this reason you can't dispose dialog.
What i understand from your question is that you won't to use command because you want your gui for button.
My advice is to create dialog like that (I think is can be work):
Dialog dialog = new Dialog();
Display.getInstance().callSerially(new Runnable() {
public void run() {
dialog.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
...
dialog.addComponent(dialog);
button = new Button(new Command("Close"),i));
try
{
button.setUIID("LabelButton");
}
catch (Exception exception)
{
exception.printStackTrace();
}
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();//???????????
}
dialog.addCommand(okCommand);
...
dialog.show();
}
});
this dialog need to be class member to be recognized by button.

GWT how can I modify the button value in the ClickHandler

I want to implement this function, when I click on the button, the value of the button changes. But I know I cannot call the button object itself in its clickHandler. So, How can I do this?
button.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event){
button.getButtonElement().setValue("X");
}
});
2 ways:
((Button)event.getSource()).setValue("x");
Or
Outside the handler, use
final Button btn = button;
Then you will be able to use btn as reference to the button inside the handler .
It's a very easy problem to solve:
final Button b = new Button("Old text here", new ClickHandler() {
public void onClick(ClickEvent event) {
b.setText("new text here");
}
});

Cannot add ui widget to Eclipse RCP view on mousedown event

I'm trying to add new widgets on an RPC view by clicking on an existing button. The code that I'm using is the following:
public void createPartControl(final Composite parent) {
parent.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnNewButton = new Button(parent, SWT.NONE);
btnNewButton.setText("New Button");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent e) {
Button b=new Button(parent,SWT.BUTTON1);
b.setText("asdasd");
}
});
}
The buttons are getting added on the view but are not visible. If I resize the view then they become visible. Why is this happening and how can it be solved?
I need somehow to refresh the view or call the event that the resize action calls.
The attached code works without problems in standard java applications.
Thank you,
Nick
Call the layout method of your parent Composite when you add a widget:
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent e) {
Button b=new Button(parent,SWT.BUTTON1);
b.setText("asdasd");
parent.layout();
}
});