Problems when creating containers in CodenameOne - java

For my main App I would need a custom container for navigation purpose, see here (Stackoverflow).
As the solution posted did not work for me (I tried with simple System.out.println), I began a new Project to understand, how the container embedding works, but it does not work the way I expected.
So the new App is the Hi World application with the orange color.
I created a new blank container in the GUI designer and added 3 Buttons.
I created a new blank container in the GUI designer and added 3 Buttons.
I added an actionListener to them within the container
I added the container to the form
My StateMachine looked like this:
#Override
protected void onButtonCont_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 clicked");
}
#Override
protected void onButtonCont_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 clicked");
}
#Override
protected void onButtonCont_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 clicked");
}
(The container I created was named ButtonCont..) Nothing else modified in StateMachine or elsewhere!
Now I started the app and clicked the buttons - but nothing happened.
So I
Opened the GUI Builder
Selected MainForm
selected the three Buttons one after another
added ActionListeners to each one
Now my StateMachine looks like this:
#Override
protected void onMain_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 -now- clicked");
}
#Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 -now- clicked");
}
#Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 -now- clicked");
}
(in addition to the previous onButtonCont- methods)
Starting the app and clicking the buttons results in this output:
button1 -now- clicked
button3 -now- clicked
button2 -now- clicked
What am I doing wrong?

I found the answer myself.
Instead of adding the container to the Form under the section "user defined", you simply need to add an embedded container and select "embedded | [null]" to your own container

Related

Switching theme by button Codename One

I'm having issues when I change my theme, I have a default theme and a theme called Blue, I change to Blue by pressing a button then it's working good untill I get back to my main Menu then it changes back to my default theme by overriding my Blue theme. I want to avoid it.
This is my initVars:
protected void initVars(Resources res){
Toolbar.setOnTopSideMenu(false);
}
This is my button to do the change:
#Override
protected void onMain_Button4Action(Component c, ActionEvent event) {
UIManager.initNamedTheme("/theme", "Blue");
Display.getInstance().getCurrent().refreshTheme();
}
And this is my button function to back to my main Menu:
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
new StateMachine("/theme");
}
});
Should I set something in initVars or in my ActionListener to avoid overriding?
I would suggest staring over with a new project. You are using the old GUI builder which is deprecated.
Assuming both themes are in the main res file you don't need to do that. You just need a reference to the resource file which you can get in the old GUI builder using fetchResourceFile().
Hashtable themeData = theme.getTheme("Theme Name");
UIManager.getInstance().setThemeProps(themeData);
Display.getInstance().getCurrent().refreshTheme();

RightClick Menu not working after JFrame Resize

I am developing a Java Swing application that will have several panels in it, one of which is NASA's WorldWind. In this WorldWind panel I need to have a right click menu for various actions. I was successful in adding the right click menu and all is good, until the user resizes the window.
I am not sure what about that action would be causing the right click menu to stop working. The mouse listener is working fine still, but the menu no longer shows up. None of the objects get garbage collected, nothing seems to change.
Here is a quick sample of how my code is set up:
class ClickListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e) == true) {
showRightClickMenu(e);
} else {
addPoint(e);
}
}
}
void showRightClickMenu(MouseEvent e) {
System.out.println("In Right Click");
if (menu == null) {
setupRightClickMenu();
}
menu.show(e.getComponent(), e.getX(), e.getY());
}
void setupRightClickMenu() {
menu = new JPopupMenu("RightClick");
JMenuItem button1 = new JMenuItem("Show Waypoint Table");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// ptTable.showTable();
}
});
menu.add(button1);
JMenuItem button2 = new JMenuItem("Clear Waypoints");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ptLayer.removeAllRenderables();
}
});
menu.add(button2);
}
I can set the size to whatever I want at application launch and the right click works, but once I resize, it fails. Any ideas?
UPDATE
Actually after playing with it a bit more and talking with someone else I am working with, it looks like it may be drawing the menu behind the WorldWind canvas. I am not sure why this is, but I need to make sure it shows in front of it.
I tried changing the JPopupMenu property talked about here, but that does not seem to help.

Can we add a ClickHandler and a KeyDownHandler to the same PushButton with an image? I get the click events but the key press event is not captured

Simplified code
play = new PushButton("Play");
play.getUpFace().setImage(new Image(pathToImages+offImage));
play.getUpHoveringFace().setImage(new Image(pathToImages+hoverImage));
play.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//This event is captured
}
});
I require a keyboard button as a shortcut to play button accordingly I have added
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event)
{ //Call the functionality of play button for the approriate key press
}
});
But once I click the play button using mouse the above nativePreviewHandler does not capture the key press event from keyboard.
So I added a keyDownHandler to the play button
play.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
//This event is never fired
}
});
But the above onKeyDown() method is never called.
Only after I click at some other place on screen is the key press event captured by the nativePreviewHandler.
Just as additional info, I tried following without success
Added DomHandler to the play button instead of keyDownHandler
Removed the images set to the play button
Tried to unfocus the play button every time it is pressed using play.setFocus(false);
Any possible solutions or suggestions are appreciated.
The play button need to have the focus in order to be able to catch the keyDownEvent.
Could you check this ?
If you can't set the focus on that button: check this answer. You need to set a keyDownHandler to a container and ensure it is focused at all time in order to catch the events even if your button is not focused.
This will work:
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getType().equals("keydown")) {
Window.alert("Code: " + Integer.toString(event.getNativeEvent().getKeyCode()));
}
}
});

GWT addSubmitCompleteHandler called twice

I have a form panel (myForm) with a submit button added onto a simple panel. Every time the submit is pressed myForm.addSubmitCompleteHandler is called twice
mySubmit.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
myForm.submit();
}});
...
myForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// what ever's here happens twice
}
});
I've double checked my code and ordering and placing of widgets and panels. What could possibly be causing this?
What I am trying to achieve is an alert that submission is complete.
It seems like adding the line
event.preventDefault()
in the addClickHandler of the submit button helps. Also make sure you don't have duplicate setAction Calls for the form

GWT:adding clickHandler on HyperLink

I am having this HyperLInk in my cellTable ,but its Clickhandler is not working
myHyperLInk.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
Window.alert("test");
}});
It displays a warning :
The method addClickHandler(ClickHandler) from the type Hyperlink is
deprecated
When i click on the link, it simple go to the previous page from history. How can i simply say show a message dialog on pressing a hyperLink?
I think what you want may be an anchor widget vs a hyperlink.
http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/gwt/user/client/ui/Anchor.html
Hyperlinks are usually used for working with the history.
Hyperlink link0 = new Hyperlink("link to foo","foo");
link0.addDomHandler(handler, ClickEvent.getType());
ClickHandler handler = new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("test");
}
};

Categories

Resources