We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Sorry to bother once again. I'd used a GButton to create a GWindow every time it is clicked. Inside, there is a GTextArea that displays some text. Strangely, half the time it is working well (after adding spaces to \n), half the time, it returns a Buffers have not been created
error on closing the window:
===================================================
G4P V4.1.1 created by Peter Lager
===================================================
java.lang.IllegalStateException: Buffers have not been created
at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:1018)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)
at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4050)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)
at processing.awt.PSurfaceAWT.render(PSurfaceAWT.java:296)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1541)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Sometimes, I get another error while trying to open the window, I am not sure of the errors are related:
===================================================
G4P V4.1.1 created by Peter Lager
===================================================
################ EXCEPTION IN EVENT HANDLER ################
An error occured during execution of the eventhandler:
CLASS: Defense_Spread_Calculator_0_11_0 METHOD: btn_details_click
Caused by java.lang.NullPointerException
g4p_controls.StyledString.getLines(Unknown Source)
g4p_controls.GTextArea.setStyledText(Unknown Source)
g4p_controls.GTextArea.setTextImpl(Unknown Source)
g4p_controls.GTextArea.setText(Unknown Source)
Defense_Spread_Calculator_0_11_0.createDetailsWindow(Defense_Spread_Calculator_0_11_0.java:1835)
Defense_Spread_Calculator_0_11_0.btn_details_click(Defense_Spread_Calculator_0_11_0.java:2110)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
g4p_controls.GAbstractControl.fireEvent(Unknown Source)
g4p_controls.GButton.mouseEvent(Unknown Source)
g4p_controls.GWindowImpl.mouseEvent(Unknown Source)
sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1408)
processing.core.PApplet.handleMethods(PApplet.java:1603)
processing.core.PApplet.handleMouseEvent(PApplet.java:2695)
processing.core.PApplet.dequeueEvents(PApplet.java:2618)
processing.core.PApplet.handleDraw(PApplet.java:2429)
processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)
processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
##############################################################
Here is a portion of my code:
void createDetailsWindow(){
int winX=windowX()+width+20, winY=windowY();
int winW=400, winH=height;
PImage icon = loadImage(iconFn);
win_details = GWindow.getWindow(this, "Details", winX, winY, winW, winH, JAVA2D);
win_details.addDrawHandler(this, "win_details_draw");
win_details.setActionOnClose(G4P.CLOSE_WINDOW);
win_details.addOnCloseHandler(this, "win_details_close");
//Set windows Icon
JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) win_details.getSurface().getNative()).getFrame();
frame.setIconImage((java.awt.Image) loadImage(iconFn).getNative());
txt_details = new GTextArea(win_details, 20, 20, winW-40, winH-40-30, G4P.SCROLLBARS_VERTICAL_ONLY);
txt_details.setPromptText("No Results.");
txt_details.addEventHandler(this, "txt_details_change");
btn_detailsCopy = new GButton(win_details, winW-20-90, winH-20-20, 90, 20);
btn_detailsCopy.setText("Copy");
btn_detailsCopy.addEventHandler(this, "btn_detailsCopy_click");
txt_details.setText(generateFullResults());
win_aboutOpen=true;
}
The function is called on clicking one of the GButton:
public void btn_details_click(GButton source, GEvent event) { //_CODE_:btn_details:922113:
createDetailsWindow();
source.setEnabled(false);
} //_CODE_:btn_details:922113:
Once again, I thank you for helping out.
Answers
The first error is related to Processing closing windows apparently you get similar problems with other GUI libraries and only started happening with Processing V3. Creating and closing windows uses a lot of resources and I recommend hiding and reusing windows if possible. See my G4P User Guides on multiple windows.
The second stack trace is interesting, starting at line 20 we see that the mouse event has been passed to the g4p_controls.GWindowImpl which tells me that you have clicked on the main display. Now we see that this calls the GButton event (line 19) which calls the fireEvent method which will call your method btn_details_click (line 13) which calls your method createDetailsWindow (line 12).
This then creates the new window and tries to set the text in the text area (line 11). The NPE was generated in the getLines() method inside the StyledString class. This method formats and styles the text ready for rendering inside the text area.
After all that I am still not sure of the cause but you might try this
I had read your post on reusing windows (open once, then set the visibility to true or false). However, the animation of opening the window will still show before it vanishes, and it might be confusing for the user (thinking the sudden opening and closing of the window as a bug).
I'll try your method and report back soon!
No problem, create the Window when the button is first clicked then set the visibility to true/false thereafter.
Why didn't I thought of that?!
Sadly, it does not work. I made some observations:
I have another window created in a similar way and no matter how many times I open and close it, no errors occur.
Changing my code so the text area outputs a fixed simple one line of text makes the NPE goes away, but the "Buffers have not been created" still occurs at times. I've check my function to generate the String to be passed on and it is free from NPE.
It seems like some problems with the Text Area not being able to handle large amount of text. The String I passed to
txt_details
has about 1500 characters.My code for opening and closing the other window, for reference:
A sample of the String I passed to
txt_details
:Try moving this line to the end of the method after you have created the TextArea control.
win_about.addDrawHandler(this, "win_about_draw");
The second window event loop will start immediately after the window is created with
win_about = GWindow.getWindow(this, "About", winX, winY, winW, winH, JAVA2D);
but adding a large amout of text takes some time and may not be ready when the windows draw method is called.
I tried moving the addDrawHandler towards the end, interesting, I consistently get another error this time:
Also i realised that even without any errors, repeatedly opening and closing the windows distorts the text that is supposed to appear in the text area, where portions of the text will go missing or get repositioned and highlighting the text area will produce the selection at elsewhere other than where my mouse had selected. Example:
Notice how the caret is so far away from the highlighted text and the a whole chunk of text is missing from the beginning.
The example G4P_EditTextControls has a textarea control with 1850 characters that works fine so I am not sure what the problem is.
Without being able to run the sketch I am not sure what to suggest next.
Here is the source code: https://we.tl/mXuJIgJbma It is too long to paste everything in the forum and to make sense of it. Warning though: It is extremely amateurish and messy, pardon me for me.
OK I have had a look at the code and some points to note.
In the 'createDetailsWindow() method' you
1) use the variable
win_aboutOpen
, I suspect an incomplete copy and paste job ;) (It is also used in the win close handler.2) You have added a text change handler for the text area but it is empty. So why add the handler in the first place.
3) The window is still allowed to close by clicking on the red cross so will have to be recreated when the details button is clicked.
I have modified this method (see below) so that the window CANNOT be closed. To simiulate closing/opening of the details method I have added an extra button 'Close' to the details window. The button event handler makes the window invisible and clears the text area.
The results are generated before the window is created or made visible. This method is time consuming so makes sense to get it done before we worry about creating the window.
Immediately after the window is looping is disabled until the GUI is ready and re-enabled just before we leave the method.
The textArea no longer has an event handlet to detect changes because it is not needed.
Hopefully this should simplify the management of the details window and make the NPE less likely to happen. It seems to work well on my iMac.