I'm using GBUI and love it. however I have the following problem:
I use a GBUI button to launch a url in a browser. this works fine. however when the user clicks the game window to go back to the game, the GBUI button retains focus, actuates a click, and relaunches the browser. this goes on forever (or if you click on the chrome of the game window first you can break out of the loop).
I'm including a simple test called FocusBrowserTest that demonstrates the issue. any help would be greatly appreciated.
(note: my platform is Mac OSX; i found the showInBrowser method online somewhere)
package com.jmex.bui.tests;
import java.io.IOException;
import com.jmex.bui.BButton;
import com.jmex.bui.BWindow;
import com.jmex.bui.BuiSystem;
import com.jmex.bui.event.ActionEvent;
import com.jmex.bui.event.ActionListener;
import com.jmex.bui.layout.GroupLayout;
public class FocusBrowserTest extends BaseTest2 {
String url = "http://www.google.com";
BButton button;
protected void createWindows() {
BWindow window = new BWindow(BuiSystem.getStyle(), GroupLayout.makeVStretch());
button = new BButton(url);
button.addListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showInBrowser(button.getText());
}
});
window.add(button);
window.setSize(400, 400);
BuiSystem.addWindow(window);
window.center();
}
public static void main(String[] args) {
new FocusBrowserTest().start();
}
public boolean showInBrowser(String url) {
String os = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
try {
if (os.indexOf( "win" ) >= 0) {
String[] cmd = new String[4];
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = "start";
cmd[3] = url;
rt.exec(cmd);
} else if (os.indexOf( "mac" ) >= 0) {
rt.exec( "open " + url);
} else {
//prioritized 'guess' of users' preference
String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror", "netscape","opera","links","lynx"};
StringBuffer cmd = new StringBuffer();
for (int i=0; i<browsers.length; i++)
cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" "" + url + "" ");
rt.exec(new String[] { "sh", "-c", cmd.toString() });
}
} catch (IOException e) {
System.err.println("showInBrowser("+url+") IOException");
e.printStackTrace();
return false;
}
return true;
}
}