Bug with button in a window (button is not clickable)

Hello,
I think there is a bug with button and window. When I add a button in Window it is not clickable anymore :frowning:
Here is my code:

[java]public class HelloGui extends SimpleApplication{

public static void main(String[] args) {
	new HelloGui().start();
}

@Override
public void simpleInitApp() {
	flyCam.setEnabled(false);
	
	this.setDisplayFps(false);
	this.setDisplayStatView(false);
	
	Screen screen = new Screen(this);
	guiNode.addControl(screen);
	
	Window dropWindow = new Window(screen, "Drop Window", new Vector2f(15, 15),
		    new Vector2f(200, 300)
		);
	dropWindow.setResizeE(false);
	dropWindow.setResizeW(false);
	dropWindow.setResizeS(false);
	dropWindow.setResizeN(false);
	//dropWindow.setText("  Items drop");
	
	screen.addElement(dropWindow);

	
	dropWindow.addChild(new ItemButton(screen, "btn1", new Vector2f(5, 50), "Monkey head"));
}

} [/java]

[java]public class ItemButton extends Button {

public ItemButton(ElementManager screen, String UID, Vector2f position, String texte) {
	super(screen, UID, position);
	this.setWidth(190);
	this.setHeight(30);
	this.setText(texte);
}

@Override
public void onButtonFocus(MouseMotionEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void onButtonLostFocus(MouseMotionEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void onButtonMouseLeftDown(MouseButtonEvent arg0, boolean arg1) {
	System.out.println("ouch");
	
}

@Override
public void onButtonMouseLeftUp(MouseButtonEvent arg0, boolean arg1) {
	// TODO Auto-generated method stub
	
}

@Override
public void onButtonMouseRightDown(MouseButtonEvent arg0, boolean arg1) {
	// TODO Auto-generated method stub
	
}

@Override
public void onButtonMouseRightUp(MouseButtonEvent arg0, boolean arg1) {
	// TODO Auto-generated method stub
	
}

}[/java]

Is it a bug of the library or is my bad ?
Thanks in advance.

Well, when I add the code to my game it is working without problem, so it was probably my fault, sorry.

Need to go test this… however, a couple of observations:

[java]
dropWindow.setIsResizable(false);
[/java]

and

[java]
public class ItemButton extends ButtonAdapter {
public ItemButton(ElementManager screen, String UID, Vector2f position, String texte) {
super(screen, UID, position);
this.setWidth(190);
this.setHeight(30);
this.setText(texte);
}

@Override
public void onButtonMouseLeftDown(MouseButtonEvent arg0, boolean arg1) {
	System.out.println(“ouch”);

}

}
[/java]

Will help minimize your code. Even if you originally extend Button… ALT+ENTER to implement abstract methods… then change it to ButtonAdapter and ditch the methods you don’t need.

And… now I’ll go test this and see what the issue is =)

@toggy said: Well, when I add the code to my game it is working without problem, so it was probably my fault, sorry.

I’ll still go and test to see if I can find the issue for you

Ok, thank you for your advice.
I do not understand why I have no problem in my little test and why, when I copy past the code in my game it is working fine.
Anyway, really nice GUI :slight_smile: .

1 Like

Well I have again the problem when I added the name of my item (“Monkey Head”) as text for button :frowning:

Try inverting the order of these two lines:

screen.addElement(dropWindow);

dropWindow.addChild(new ItemButton(screen, “btn1″, new Vector2f(5, 50), “Monkey head”));

1 Like
@toggy said: Well I have again the problem when I added the name of my item ("Monkey Head") as text for button :(

Haven’t forgotten about this… trying to get some stuff wrapped up (fixes for things like the scrollable dropdowns account for text padding and such). I should be able to get to this in just a bit here… need to run a few more tests and commit the changes if all goes well… then this.

@loopies said: Try inverting the order of these two lines:

screen.addElement(dropWindow);

dropWindow.addChild(new ItemButton(screen, “btn1″, new Vector2f(5, 50), “Monkey head”));

This solution does work… however, you should be able to add this after adding the window to the screen, so I need to figure out why this is happening in this case.

@toggy said: Well I have again the problem when I added the name of my item ("Monkey Head") as text for button :(

For the time being, I’m going to have to say to go with @loopies solution. I’m not entire sure how to fix this as it is actually the collision results being returned in the improper order (i.e. result.getContactPoint().z should be in order of closest to furthest while iterating through the results and for some reason this is not the case when adding these in this order)

I’ll continue to try and resolve this, but use the suggested fix for now.

Ok thank you I will do it.
If I still have problem I wll take the source and see if I can do something (Probably not, but I prefer to try ^^)

@toggy said: Ok thank you I will do it. If I still have problem I wll take the source and see if I can do something (Probably not, but I prefer to try ^^)

Please do! Let me know if you are able to find something I have missed. At some point tomorrow I’ll put together the test case and find out if I can repro this using quads + bitmaptext

Well the @loopies solution looks like to not work :confused:

@toggy said: Well the @loopies solution looks like to not work :/

I’m still looking into this. Can you post the problem code from your game (if it is possible)?

Yes I can give you my code but it will be a bit hard to read, if you agree I give you only the part with the “drop window” in order to be more readable (I am not scary to give the whole code but it is not a reallly proper code actually, I do not like GUI so I do ugly code ^^ ):
Here is my init:
[java] dropWindow = new Window(ConstClient.gc.getScreenUI(), “Drop Window”, new Vector2f(15, 15),
new Vector2f(200, 300)
);

	dropWindow.setTextPosition(10, 8);
	dropWindow.setText("Drop Select");
	
	dropWindow.setIsVisible(false);
	dropWindow.setIsResizable(false);
	ConstClient.gc.getScreenUI().addElement(dropWindow);[/java]

(I try different order of addElement(dropWindow but doesn’t work properly.

And here is my updateDropWindow, it called from simpleUpdate(float tpf):
[java]private void updateDropWindow(){

	if(NPCDrop != null){
		
		if(!dropWindow.getIsVisible()){
			dropWindow.setIsVisible(true);
			int j = 0;
			for(Item i : NPCDrop.getDrop().getItems()){
				ItemButton ib = new ItemButton(ConstClient.gc.getScreenUI(), "item#"+i.getID()+"|"+NPCDrop.getDrop().getItems().indexOf(i.getID()),
						new Vector2f(5, 50+(j*50)), i.getItemInfo().getName(), i);
				itemButtons.add(ib);
				dropWindow.addChild(ib);
				j++;
			}
		}
		
	}else{
		if(dropWindow.getIsVisible()){
			
			
			for(ItemButton ib : itemButtons){
				dropWindow.removeChild(ib);
			}
			
			dropWindow.setIsVisible(false);
		}
	}
	
}[/java]

In fact this way to code is bad I think, and when I copy past I have found an other way to do it, I will initialize all buttons in my init, but set their visible false, and changer their content when I open the drop window. I think it is the good way to do it, I do not know why I do not think like that before ^^"
So do not worry about that bug “for me”, I will correct it easily, but I understand this bug is not “cool” for your work.
I want to say with a simple little text like “#0” the button was working pretty good, the text is probably the reason. I do not know how your code work, but you could check if the text.getParent() is an instance of button ? So it could reduce the bug.

I’ve tried your code with the button that you said didn’t work, after inverting the two lines and it works for me.

This is what I tried (copy paste of your code + inversion of the two lines):

[java]
import com.jme3.app.SimpleApplication;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.Vector2f;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.windows.Window;
import tonegod.gui.core.ElementManager;
import tonegod.gui.core.Screen;

/**
*

  • @author jo
    */
    public class HelloGUI extends SimpleApplication {

    public class ItemButton extends Button {

     public ItemButton(ElementManager screen, String UID, Vector2f position, String texte) {
         super(screen, UID, position);
         this.setWidth(190);
         this.setHeight(30);
         this.setText(texte);
     }
    
     @Override
     public void onButtonFocus(MouseMotionEvent arg0) {
         // TODO Auto-generated method stub
     }
    
     @Override
     public void onButtonLostFocus(MouseMotionEvent arg0) {
         // TODO Auto-generated method stub
     }
    
     @Override
     public void onButtonMouseLeftDown(MouseButtonEvent arg0, boolean arg1) {
         System.out.println("ouch");
    
     }
    
     @Override
     public void onButtonMouseLeftUp(MouseButtonEvent arg0, boolean arg1) {
         // TODO Auto-generated method stub
     }
    
     @Override
     public void onButtonMouseRightDown(MouseButtonEvent arg0, boolean arg1) {
         // TODO Auto-generated method stub
     }
    
     @Override
     public void onButtonMouseRightUp(MouseButtonEvent arg0, boolean arg1) {
         // TODO Auto-generated method stub
     }
    

    }

    public static void main(String[] args) {
    new HelloGUI().start();
    }

    @Override
    public void simpleInitApp() {
    flyCam.setEnabled(false);

     this.setDisplayFps(false);
     this.setDisplayStatView(false);
    
     Screen screen = new Screen(this);
     guiNode.addControl(screen);
    
     Window dropWindow = new Window(screen, "Drop Window", new Vector2f(15, 15),
             new Vector2f(200, 300));
     dropWindow.setResizeE(false);
     dropWindow.setResizeW(false);
     dropWindow.setResizeS(false);
     dropWindow.setResizeN(false);
     //dropWindow.setText("  Items drop");
    
    
     dropWindow.addChild(new ItemButton(screen, "btn1", new Vector2f(5, 50), "Monkey head"));
     screen.addElement(dropWindow);
    

    }
    }
    [/java]

When the lines aren’t inverted, the button doesn’t react but does when the lines are inverted like here.

I always follow this rule: fill the children before adding them to the parents. So fill your button then add it to the window, fill other window stuff then add them to the window and then add the window to the screen.

I fallow this rule but I still have problem 'cause of my usage, but I have found an other usage that will probably fix the problem :slight_smile:

In order to update the content of a list control:
Either:

  • only change what you need:
  • remove window from screen
  • remove list control whose content is being updated
  • modify content of that control list
  • attach list to window
  • attach window to screen
  • rebuild whole window

Ok thank you.
I finally change the way of how I use my window, it is a better way and it is working really well.

@toggy
Happy to report I finally found and fixed this issue.

Please be patient with me getting the update out there, as I have a huge list of things I am changing / updating to try and square away the library solid Beta. Until you gave me this test case, the problem was intermittent, so I had no reliable way of reproducing it to fix it.

BIG thanks for taking the time to do this!

I’ll post a new thread soon discussing all of the updates and changes and when I expect to have them available.