Unintentional Closing and Wrong Size if setVisible(false) Problem of Second Window With JFrame

Hello there. I currently have a serial control sketch. I have a secondary window to act as the serial monitor, and I have everything working except the two problem as noted in the title which I will now explain in further detail.

The idea is to click the serial monitor button, and have a serial monitor of sorts popup in a separate window. I am using a JFrame, as well as a new PApplet to do so. (setVisible() refers to if the Serial Monitor Window is visible when when it is created (sets it to not, so that we can change it with the button later))

However problem a) occurs as such:

  Correct Behaviour:
    1.   Open Program
    2.   Click Serial Monitor Button
    2b. Monitor Window Opens
    3.   Click back into main window
    3b. Everything is fine

  What really happens:
    1.   Open Program
    2.   Click Serial Monitor Button
    2b. Monitor Window Opens
    3.   Click back into main window
    4.   -->Monitor window closes!<--
    4b. Unless clicked into the new window


Whilst problem b) operates this way:

  Correct Behaviour:
    1.   Set setVisible() to true or false
    2.   Open Program
    3.   Click Serial Monitor Button
    3b. Monitor Window Opens
    4.   Window is at proper dimensions
    4b. Everything is fine

  What really happens:
    Part A:
      1.   Set setVisible() to true
      2.   Open Program
      3.   Click Serial Monitor Button
      3b. Monitor Window Opens
      4.   Window is at proper dimensions
      4b. Everything is fine

    Part B:
      1.   Set setVisible() to false
      2.   Open Program
      3.   Click Serial Monitor Button
      3b. Monitor Window Opens
      4.   Window is at much smaller dimensions
            (~100x100) and compressing frame
            inside to ~10x10
      4b. Problem above continues through 
            opening or closing monitor

Help is greatly appreciated. And I hope the formatting made the explanation of what is happening very clear. I figured it would be easiest to explain that way.

Commented core code of the problem is below.

-Mad

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;

SecondWindow smw; //Second window object
SecondApplet secApp; //Second PApplet object

int smwSize = 320; //Height size for second window
int s = 720; //Main window size

boolean serialMonitor = false;
boolean overSerialMonitorButton = false;

void setup()
{
  size(s, s);
  frame.setTitle("Serial Control Pad");

  secApp = new SecondApplet();
  smw = new SecondWindow(secApp, "Serial Monitor");
}

void draw()
{
  //Display a green rect with borders (for testing JFrame window placemnt and insets etc...)
  secApp.background(0);
  secApp.fill(0, 255, 0);
  secApp.stroke(0, 191, 0);
  secApp.strokeWeight(10);
  secApp.rectMode(CENTER);
  secApp.rect(secApp.width/2, secApp.height/2, secApp.width, secApp.height);
  secApp.fill(255, 127, 63);
  secApp.textAlign(RIGHT, BOTTOM);
  secApp.text(secApp.mouseX + "," + secApp.mouseY, secApp.mouseX, secApp.mouseY);
  secApp.redraw();

  //Check if over the Serial button
  float distToSerialMonitor = PVector.dist(new PVector(mouseX, mouseY), new PVector(width-150, 50));
  if (distToSerialMonitor < 30) overSerialMonitorButton = true;   
  else overSerialMonitorButton = false;

  if (overSerialMonitorButton) cursor(HAND);
  else cursor(ARROW);
  smooth(); 

  //Display Button
  strokeWeight(2);
  stroke(70);
  textAlign(CENTER, CENTER);
  textSize(28);
  if (serialMonitor)
  {
    fill(0, 255, 0);
    ellipse(width - 150, 50, 60, 60);
    fill(0, 0, 255);
    text(">>", width - 150, 45);
  } else
  {
    fill(0, 0, 255, 63);
    ellipse(width - 150, 50, 60, 60);
    fill(0, 255, 0);
    text(">>", width - 150, 45);
  }
}

/////////////////////////////////////////////////////////////////////////////////

void mousePressed()
{
  //Toggle serial monitor
  if(overSerialMonitorButton && mouseButton == LEFT)
  {
    serialMonitor = !serialMonitor;
    smw.setVisible(serialMonitor);
  }
}

/////////////////////////////////////////////////////////////////////////////////

//Second PApplet class
public class SecondApplet extends PApplet 
{

  public void setup() 
  {
    //Dimension size = new Dimension(smwSize, smwSize);
    //setPreferredSize(size);
    size(smwSize*2, smwSize);
    noLoop();
  }
  public void draw() 
  {
  }
}

/////////////////////////////////////////////////////////////////////////////////

//Second window Class, extends JFrame
class SecondWindow extends JFrame
{
  Dimension d;

  SecondWindow(SecondApplet sa, String title)
  {
    super(title); //Window Title
    //setDefaultCloseOperation(EXIT_ON_CLOSE);
    //setLayout(new FlowLayout());
    setLayout(new GridBagLayout()); //Layout (handles insets)


    // Set some preferred size
    d = new Dimension(smwSize*2, smwSize);
    setSize(d); //Sets the size of the window  
    //setPreferredSize(d);

    add(sa); //Adds SecondApplet to window to display (like as if it were a JPanel)
    sa.init(); //Start the applet

    setResizable(false); //Dont allow the user to change the window size

    //The mentioned setVisible dilema
    setVisible(false);
    //setVisible(true);

    // Pack the frame so that no/very little extra space is visible
    pack();
  }
}

Answers

  • GoToLoop, I see and appreciate all the help you give, but alas sadly that did not answer my question nor help... :/ I'm not looking for a new method of nesting Applets, ect... I simply want my JFrame to work, if anyone has experience with them. Even if I don't have to use an applet that would be cool. I've heard terms like paint and canvas, I simply need to be able to draw to another window, and this route seems to work not too bad or complicated, except those problems.

  • Answer ✓
    import javax.swing.*; 
    SecondApplet s;
    void setup() {
      size(640, 480);
      PFrame f = new PFrame(width, height);
      frame.setTitle("first window");
      f.setTitle("second window");
      fill(0);
    }
    void draw() {
      background(255);
      ellipse(mouseX, mouseY, 10, 10);
      s.setGhostCursor(mouseX, mouseY);
    }
    public class PFrame extends JFrame {
      public PFrame(int width, int height) {
        setBounds(100, 100, width, height);
        s = new SecondApplet();
        add(s);
        s.init();
        show();
      }
    }
    public class SecondApplet extends PApplet {
      int ghostX, ghostY;
      public void setup() {
        background(0);
        noStroke();
      }
    
      public void draw() {
        background(50);
        fill(255);
        ellipse(mouseX, mouseY, 10, 10);
        fill(0);
        ellipse(ghostX, ghostY, 10, 10);
      }
      public void setGhostCursor(int ghostX, int ghostY) {
        this.ghostX = ghostX;
        this.ghostY = ghostY;
      }
    }
    

    there you go.

  • Try altering lines 121 - 126 to

    pack(); // May not be needed
    // Momentarily display the frame
    setVisible(true);
    setVisible(false);
    

    I think you will find that setVisible(true) will pack the components so try with and without.

  • Quark, you method did fix the problem, however with a momentary appearance of the window, so I thank you. However playing around with lolnyancats example, it works! The bug with the click back I also was able to fix. Long story short, have to check if mouseReleased(), not mousePressed(). It seems upon clicking back it, it saw the click, and not the location check if it was indeed over button.

    Thank you all

  • How in the world after all this time did I miss that... I have been using mousePressed for so long now. Well you learn something new everyday. And hopefully this can help other people.

    Thanks! :)

  • Wait... This worked for my mac at work, however not for my home Win7 PC. The edges are still cutoff some pixels, and the initial code isn't helping either :( I see why people have troubles with JFrame.

  • I see why people have troubles with JFrame.

    Well, you've rejected the link I've posted about multi-sketches w/o JFrame and any 3rd party libs.
    Besides that, it also covers controlling sub-sketches via setVisible() from the main sketch. O:-)

  • I appreciate the link but I'm trying to learn this without libs, and with JFrame. Afterwards using those may be fun.

  • edited June 2015

    ... but I'm trying to learn this without libs,

    That's what I've said: w/o any 3rd party libs. And even w/o JFrame.
    I don't get why would you need JFrame for... :-/

  • I just thought that's how you make new windows. I've seen a few examples using this type of system with extending JFrame and making an PApplet.

    What are your recommended libs? So long it handles all the insets correctly and so can draw to it I'm happy.

  • Oh I misread the above as without JFrame and with libs.

  • edited June 2015

    When I first looked at the code in the link, it was brief. However now that I had the chance go fully read it, I am very happy.

    Thanks GoToLoop, my apologies for the extra troubles.

  • edited June 2015

    Getting this error I cannot figure out. I essentially copy pasted the code from the link. Any help would be greatly appreciate. AAAAA

  • edited June 2015

    Getting this error...

    • I believe you haven't mentioned what is "this error" about? (:|
    • "Multiple Nested PApplets" example was tested w/ Processing 3.0a5 under 64-bit Win8.1.
    • And after replacing smooth(4); w/ smooth(); & printArray() w/ println(), it even runs @ Processing 1.5.1 under 64-bit Java 8! \m/
  • The example was working in a test sketch, it was when I transferred it over there where problems. The error is when I run it, it dosent compile and that error bar shows up.

    I'll test it more when Inget the chance, but something in the code it not running with the additional code.

    I will also post all the code when I get the chance.

  • edited June 2015

    Problem is, the image is blurred enough to impede me to read the error message...
    That's why we should always describe the errors and where it happened.
    Relying solely on screenshots aren't enough! [-X

  • Still a screenshot, but much more zoomed. Should be easier to see than a typed version.AAAAAAB

    I will begin troubleshooting -- I wanted to test to see if it was conflicting with the JFrame code as I had just hastily added the new code without commenting out the JFrame stuff. So before posting this I checked and commented out the JFrame stuff, and it worked.

    So it works, and thank yous are in order, however for learning reasons, do you know why that JFrame code above, messes with the code you provided in the link?

  • edited June 2015

    I did some trouble shooting, and the code provided was conflicting with my JFrame stuff. So upon removing that everything seems fine.

    One more thing though, is that whenever I try to access my sketches variables, I get non-static errors ex. "Cannot make static reference to non-static field variableName".

    I'm would also rather not have to pass in everything. So I tried the public, keyword and that did not help. I have seen the static keyword as well, and so looked into it, and hoped it might work, especially considering it is right in the error. So I put it into the variables I needed, and it worked. Everything is working! Next step is the transfer the neural network view into a new window freeing up the room for stats on the world.

    Thank you so very much! Multiple Windows First Working

Sign In or Register to comment.