NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Anybody know how to close a JPanel?

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
matt_thumper



Joined: 13 Feb 2010
Posts: 13

PostPosted: Mon Mar 22, 2010 1:01 pm    Post subject: Anybody know how to close a JPanel? Reply with quote

My NetBeans java application
( public class My_DB_GUI extends javax.swing.JFrame {... )
has a secondary window that it pops up to grab user input
(in this case they can type in a bunch of text)
and it is a JPanel:

public class TextDemo extends javax.swing.JPanel {...

Well, when the user (me) is finished adding text (in the TextArea) they have to close the 'window' with the "X" in the upper right corner.
None of the buttons or methods I've called will shut it down
(i.e., a button called "Done")

Like I said, the only way (currently) is to use the upper right "X"


Just a bit more code as an explanation.
The code is called from within my main (NetBeans) GUI code:

Code:
public class My_DB_GUI extends javax.swing.JFrame {...
 
           textArea = new JTextArea(5, 20);
           JScrollPane scrollPane = new JScrollPane(textArea);
           textArea.setEditable(true);
 
           // Display
           JFrame frame = new JFrame("My_DB: Append Data");
 
           //Add contents to the window.
           frame.add(new TextDemo(sFile));
 
           //Display the window.
           frame.pack();
 
           frame.setVisible(true);
}


This line is the JPanel class - the TextDemo class is the text input area and window:
frame.add(new TextDemo(sFile));

As I mentioned it (TextDemo) looks like this:
public class TextDemo extends javax.swing.JPanel {...

How do I add a button that will close this JPanel?

TIA,
Matt
Back to top
walec51



Joined: 17 Mar 2010
Posts: 9

PostPosted: Tue Mar 23, 2010 12:09 am    Post subject: Reply with quote

What do you mean by closing a JPanel ?

You can close the parent JFrame by setVisible(false) but closing a JPanel ?

Do you want it to disappear form the JFrame ? Then use frame.remove(panel) method
Back to top
matt_thumper



Joined: 13 Feb 2010
Posts: 13

PostPosted: Tue Mar 23, 2010 12:39 pm    Post subject: Here is how Reply with quote

For others to reference (if they want to):

1st:
The JPanel (child, secondary) window does not close itself.
Like all the Google references tell us, it is the parent window that must do the closing.

But I wanted the child/secondary window to close itself.
How????

Here is how:

1) send a reference to the parent when you create the child:

Code:
    public TextDemo(String sFileName) {
        initComponents();
        sFileToAppend=sFileName;

becomes

Code:
    public TextDemo(My_DB_GUI mdb_gui,String sFileName) {
        initComponents();
        sFileToAppend=sFileName;
        mdb=mdb_gui;

(where mdb is declared as 'public My_DB_GUI mdb;')


2) Write a simple routine in that child/secondary window/class:

Code:
       public void CloseMe(){
           // Hide the frame
           mdb.ParentCloseMe();
        }

Don't worry about 'mdb.ParentCloseMe();' yet. We'll get there in a minute.

3) Simply call that routine from this child/secondary code where you want it to 'trip' the closure:

CloseMe();

4) Now we go back to the parent code:


Code:
   public void ParentCloseMe(){
        // Hide the frame
        frame.setVisible(false);
        // If the frame is no longer needed, call dispose
        frame.dispose();
    }


There, wasn't that easy and intuitive?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo