Closing dialog on action of button in codename one - lwuit

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.

Related

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();
}
});

how to call to context menu in swtboot

I want to call to context menu in my application.
The issue that I don't have any items in the tree.
I active my view and then I want to open context menu.
SWTBotView view = bot.viewByTitle("Project Explorer");
view.bot.tree().contextMenu("New").click();
then I got error message
Could you please advise me how I can open contextMeny without any item in the tree ?
As there is no direct way to do this. I assume you have a shortcut for opening your context menu.
bot.activeShell().pressShortcut(<your shortcut>);
bot.waitUntil(new ContextMenuAppears(tree,
"New"));
tree.contextMenu("New").click();
Where ContextMenuAppears is an ICondition which waits for the desired context menu to appear.
public class ContextMenuAppears implements ICondition {
private SWTBotTree swtBotTree;
private String mMenuText;
public TekstContextMenuAppears(SWTBotTree pSwtBotTree, String menuText) {
swtBotTree = pSwtBotTree;
mMenuText = menuText;
}
#Override
public boolean test() throws Exception {
try {
return swtBotTree.contextMenu(mMenuText).isVisible();
} catch (WidgetNotFoundException e) {
return false;
}
}
#Override
public void init(SWTBot bot) {
// TODO Auto-generated method stub
}
#Override
public String getFailureMessage() {
// TODO Auto-generated method stub
return null;
}
}
depending on what you're trying to achieve, you could try going via the file menu instead of the context menu. "new" should work that way.

GWT button click event

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

Trying to Add More tabs to tab panel upon clicking on a button

I would like to add more tabs to the tab panel upon receiving a reponse from a servelet.. the problem is that It only adds the last one and not the the others see part of the code below. It seems like it is only adding the last panel "Time Reports" but not the other two
Thank you
btnLogin.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if(getLoginResult())
{
HorizontalPanel temp = new HorizontalPanel();
panel.add(temp, "Add Hours");
panel.add(temp, "Time Sheets");
panel.add(temp, "Time Reports");
}
}
});
RootPanel.get().add(panel);
}
private boolean getLoginResult() {
AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result) {
isAuthenticated = true;
}
public void onFailure(Throwable caught) {
Window.alert("Error when invoking the pageable data service :" + caught.getMessage());
isAuthenticated = false;
}
};
timesheetLoginServlet.isAuthenticated("1","rapidjava", callback);
return isAuthenticated;
}
}
You can add any widget to its parent only once. Change the temp to temp1, temp2 and temp3

Tying/binding Eclipse Commands to a swt button

In Eclipse you can use menu contributions to add toolbar buttons and menus that will call a command. Is there any way to do this to normal swt buttons, apart from programmatically calling the command onclick?
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
IHandlerService handlerService = (IHandlerService) getSite()
.getService(IHandlerService.class);
try {
handlerService.executeCommand("my command id", null);
} catch (Exception ex) {
throw new RuntimeException("command with id \"my command id\" not found");
}
}
});
No. You have to listen for the button event and the invoke the command programmatically.
You can use CommandContributionItems in a View or Wizard like that:
CommandContributionItemParameter param = new CommandContributionItemParameter(getSite(),
"myCommand", "com.voo.myCommand", CommandContributionItem.STYLE_PUSH);
param.label = "My Label";
CommandContributionItem item = new CommandContributionItem(param);
item.fill(parent);