I’m trying to use a GWinData to save data used by a child window. I thought I would be able to declare the controls used by the child window (buttons, droplists, etc.) inside the GWinData object, but it doesn’t seem to work, at least for a droplist. The program runs, but the current state of the droplist (the current selection, in other words) isn’t being saved. If I declare the droplist as a global variable outside of the GWinData object, it works, though.
If you open the child window and select something from the droplist (other than the first item), close the child window (click on the upper right corner), then re-open it, you’ll see that the first item in the droplist is still selected.
The reason I want to declare it in the GWinData object is just to avoid a proliferation of global variables, which is considered a bad programming practice in general. And in my case, I’m going to have several child windows, so I want to encapsulate the controls within the child window that uses them, if I can. If that isn’t possible, I can live with it. I just wanted to know if it is possible or not.
import g4p_controls.*;
class MyWinData extends GWinData {
GDropList dropList;
String[] dest = {"Selection 1","Selection 2","Selection 3"};
int selected = 0;
}
GButton button = null;
GWindow window;
public void setup() {
size(600,400,JAVA2D);
G4P.setCtrlMode(GControlMode.CORNER);
G4P.setGlobalColorScheme(GCScheme.GREEN_SCHEME);
G4P.setMouseOverEnabled(true);
button = new GButton(this, 15, 40, 60, 20);
button.setText("SETUP");
button.addEventHandler(this,"buttonHandler");
}
public void draw() {
background(179,237,179);
}
public void buttonHandler (GButton source, GEvent event) {
makeChildWindow();
button.setEnabled(false);
}
public void makeChildWindow () {
window = GWindow.getWindow(this,"Child Window",20,40,400,200,JAVA2D);
window.setActionOnClose(G4P.CLOSE_WINDOW);
window.addDrawHandler(this,"windowDraw");
window.addOnCloseHandler(this,"windowClose");
window.addData(new MyWinData());
((MyWinData)window.data).dropList = new GDropList(window, 20, 20, 120, 100, 5);
((MyWinData)window.data).dropList.addEventHandler(this, "dropListHandler");
((MyWinData)window.data).dropList.setItems(
((MyWinData)window.data).dest,
((MyWinData)window.data).selected);
((MyWinData)window.data).dropList.setEnabled(true);
}
public void windowDraw(PApplet app, GWinData data) {
app.background(179,237,179);
}
public void windowClose(GWindow window) {
button.setEnabled(true);
}
public void dropListHandler (GDropList source, GEvent event) {
((MyWinData)window.data).selected = source.getSelectedIndex();
}