0

In a swing application I have, as I understand it, told the mainFrame of my application to DO_NOTHING_ON_CLOSE.

public SurveyView(SingleFrameApplication app) throws FileNotFoundException, IOException {
        super(app);

        initComponents();

        JFrame mainFrame = SurveyApp.getApplication().getMainFrame();
        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        //And on and on it goes...
}

Obviously I'll be adding a window listener and what not to dress it up later, but for now, I just need the window to not close when the X is clicked. But the window DOES close.

It seems so simple of an operation that I can't even think of how to go about debugging it. Am I missing something SUPER obvious?

Edit: And here is the getApplication method within the SurveyApp class:

public static SurveyApp getApplication() {
        return Application.getInstance(SurveyApp.class);
    }

Edit2: Since HoverCraft suggests that mainFrame is not the culprit, maybe an overview of the app structure will point us in the right direction. The entire app is contained in a single JPanel, mainPanel. The panel, in turn, is contained inside public class SurveyView extends FrameView. All of my compenents are added to the panel, which is a card layout which cycles through a few various other JPanels. As I understand it, the close operations are reserved for frames, not for views or panels. Hence why my first thought was to target the main frame of the app.

I know the main frame DOES exist. I tried System.out.println(SurveyApp.getApplication().getMainFrame()) and it output this monster(linebreaks for readability):

javax.swing.JFrame[mainFrame,0,0,0x0,invalid,hidden,
layout=java.awt.BorderLayout,title=Accounting Survey,
resizable,normal,defaultCloseOperation=DO_NOTHING_ON_CLOSE,
rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,
layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,
alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],
rootPaneCheckingEnabled=true]

And I noticed the title is, in fact, the title that displays in the title bar of my app. But then I saw the DO_NOTHING_ON_CLOSE was set, as it should be, but the app still closed, meaning, I think, Hovercraft is right - this isn't the right target.

Edit3: Further research shows that, indeed, netbeans constructs each panel and adds it to mainPanel and once all panels have been added it calls setComponent(mainPanel);, which I'm assuming is established somewhere else, but a Ctrl-F of the entire program turns up nothing. Does anyone with more experience with netbeans know where all of these methods it uses are defined? If I could find THAT I might be able to figure out what frame needs the no-close set.

Edit4: Alright I think I'm close. The mainPanel is set as the component of the FrameView, which seems to be the wrapper created by a java SingleFrameApplication. I did some reading here: http://bellquel.bo.cnr.it/appframework/org/jdesktop/application/SingleFrameApplication.html and it looks like I was actually correct: the frame created by a SFA is named mainFrame and, according to the docs, "getMainFrame() Returns the JFrame used to show this application." Meaning I AM targetting the right JFrame, and based on my earlier test, the default close operation IS in fact being set to do nothing. So what gives? I'm targetting the correct frame, it is setting the operation correctly, and then it is just being ignored? What could override the default close operation like that?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
fildred13
  • 2,280
  • 8
  • 28
  • 52
  • I don't think so. It's not closing for me. Please share your code. – Braj Apr 22 '14 at 18:50
  • What code do you think you need? This is everything, I think, that pertains to the close operation. Edit: I added the surrounding app declaration. – fildred13 Apr 22 '14 at 18:54
  • I want to know what are you doing in `SurveyApp.getApplication().getMainFrame()` method. What properties are you setting of `JFrame`? – Braj Apr 22 '14 at 18:57
  • How are you adding a window listener? – Braj Apr 22 '14 at 18:58
  • I'm going to add a window listener after the frame successfully fails to close - I need the app to NOT close before I bother adding the pop-up window saying "Are you SURE you want to close? - OK/Cancel" – fildred13 Apr 22 '14 at 19:01
  • Please share the code where you are adding the pop-up window. We are not here to guess. – Braj Apr 22 '14 at 19:04
  • I think I didn't explain clearly: this isn't a popup window. This is the main application window, which I am trying to make not close when the X is clicked. There will be a popup to confirm closure once this is set up, but for now I haven't created one because the whole application is closing before the window would appear. – fildred13 Apr 22 '14 at 19:07
  • You edit doesn't make any sense. Can you post the code where you are actually calling `new JFrame()` and other stub? How would we know that what is inside `Application.getInstance(SurveyApp.class)`? – Braj Apr 22 '14 at 19:08
  • Yup, I'm trying to pull together the relevant pieces, hang on... – fildred13 Apr 22 '14 at 19:11
  • 2
    read the api doc of SingleFrame/Application (fttomh, I think you need to register an ExitListener or override the method that's invoked on exit - myself, I don't have access to the doc right now ..) – kleopatra Apr 22 '14 at 21:45
  • If kleopatra's answer had been an answer instead of a comment, I would have accepted it. Check my comment on the accepted answer to see what kleopatra is referring to in the api doc. – fildred13 Apr 23 '14 at 23:16

1 Answers1

3

I just need the window to not close when the X is clicked. But the window DOES close.

Try this simple code

EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new JFrame();                
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

            frame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent we) {
                    System.out.println("closing...");
                }

                @Override
                public void windowClosed(WindowEvent we) {
                    System.out.println("closed..");
                }
            });

            frame.setVisible(true);
        }
 }

output (when the X is clicked)

closing...
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Naturally this works in a closed system. But how do I apply this to my app's mainFrame? I think the problem is in the selection of the target frame, not the actual method of blocking the close, no? – fildred13 Apr 22 '14 at 18:59
  • 1
    @fildred13: the error likely lies in code you've not shown us. I have to wonder, are you even using the mainFrame JFrame to create your GUI? Your code suggests otherwise. – Hovercraft Full Of Eels Apr 22 '14 at 19:01
  • Yeah, I'm starting to expect that. I was hoping I just did something super stupid and it could be fixed in the two lines. I'm trying to dig through for other relevant code. You say my code suggests I'm not creating the GUI in `mainFrame` - what do you mean by that? – fildred13 Apr 22 '14 at 19:09
  • @fildred13: do you add components to the mainFrame variable? Set it visible? etc? – Hovercraft Full Of Eels Apr 22 '14 at 19:15
  • Have you tried using `getMainFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);`? This would make more sense if you're using the Java SingleFrameApplication. Note that most folks don't use this any more since it is not supported. – Hovercraft Full Of Eels Apr 22 '14 at 19:18
  • I just tried `SurveyApp.getApplication().getMainFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);` for giggles since I AM using a SingleFrameApplication, but sadly it failed. – fildred13 Apr 22 '14 at 19:23
  • @fildred13 Just share the code if you are adding `WindowListener` and construction of `JFrame`. – Braj Apr 22 '14 at 19:24
  • And in response to your other question, I am currently digging through the netbeans generated code to see what all the components are being added to. If I can find that, I should be able to figure out which frame needs the no_close set. – fildred13 Apr 22 '14 at 19:25
  • 1
    @fildred13: if you aren't adding anything to the mainFrame, then NetBeans sure isn't. Get rid of that variable as it's doing nothing but misleading you. A strong suggestion that you avoid using Swing code generation until you first understand the rudiments of Swing. – Hovercraft Full Of Eels Apr 22 '14 at 19:28
  • Yes, yes but I have used code generation and I just need this app off my table, so I need to figure it out regardless. – fildred13 Apr 22 '14 at 19:30
  • For posterity: while this answer is NOT the solution to the SingleFrameApplication issue, it got me rolling toward this: http://www.oracle.com/technetwork/articles/javase/index-141957.html#lifecycle where, in the Application Shutdown section, the ExitListener interface is explained, showing how to do what I was trying to ask. – fildred13 Apr 23 '14 at 23:15
  • @fildred13: if you have a better solution then Braj's, then consider posting it as a separate and distinct answer to your own question. – Hovercraft Full Of Eels Apr 27 '14 at 17:34