A button close in JDialog

S

shoa

Hello

I have a a JDialog (extends of JDialog class) object in that I have an
aPanel (extends of JPanel class). In this aPanel, I have button. When I
click this button, I want this dialog to close. Could you please tell me how
to do that.

I tried to use this function for the button:

this.getParent().dispose() //obtain the parent (JDialog) then dispose it

but this function does not work

Thank you
S.Hoa
 
A

Andrew Thompson

I have a a JDialog (extends of JDialog class) object in that I have an
aPanel (extends of JPanel class). In this aPanel, I have button. When I
click this button, I want this dialog to close.

setVisible(false)
 
S

shoa

If I use that function, only the panel inside the Dialog is hidden. The
Dialog is still visible.
 
V

Vova Reznik

shoa said:
Hello

I have a a JDialog (extends of JDialog class) object in that I have an
aPanel (extends of JPanel class). In this aPanel, I have button. When I
click this button, I want this dialog to close. Could you please tell me how
to do that.

I tried to use this function for the button:

this.getParent().dispose() //obtain the parent (JDialog) then dispose it

but this function does not work

Thank you
S.Hoa
What is *this*?
If this is button - parent is JPanel
If this is JPanel - parent is Contanier (JDialog content pane)
 
A

Andrew Thompson

On Thu, 30 Jun 2005 00:18:29 +1200, shoa wrote:

(Please refrain from top-posting - further comments in-line)
If I use that function, only the panel inside the Dialog is hidden. The
Dialog is still visible.

Well, duuh.. Try.

theDialog.setVisible(false);

or.. (invoked from the right place)

this.setVisible(false);

...which translates to

setVisible(false);
 
S

shoa

Hello
As the button is inside a panel, this panel is inside a theDialog, I do not
know how to get the theDialog.setVisible(false).

I also tried
this.getParent().getParent().dispose() //this is the button
but it doesn't work
 
A

Andrew Thompson

As the button is inside a panel, this panel is inside a theDialog, I do not
know how to get the theDialog.setVisible(false).

I could tell you, but you can probably figure it out
better by experimenting.

// where is my mummy?
Container c = (Container)this;
while( c.getParent()!=null ) {
c = c.getParent();
System.out.println( "Container: " + c );
}
I also tried
this.getParent().getParent().dispose() //this is the button
but it doesn't work

Is it just lazy, perhaps? ;-)
 
S

shoa

Thank you for your help

I have to use this to get it works. Too long !!!!

this.getParent().getParent().getParent().getParent().getParent().setVisible(
false)

I use your code and get

Container:
javax.swing.JPanel[,0,0,500x570,layout=java.awt.BorderLayout,alignmentX=null
,alignmentY=null,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
Container:
javax.swing.JPanel[null.contentPane,0,0,500x570,layout=javax.swing.JRootPane
$1,alignmentX=null,alignmentY=null,border=,flags=9,maximumSize=,minimumSize=
,preferredSize=]
Container:
javax.swing.JLayeredPane[null.layeredPane,0,0,500x570,alignmentX=null,alignm
entY=null,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,optimized
DrawingPossible=true]
Container:
javax.swing.JRootPane[,4,23,500x570,layout=javax.swing.JRootPane$RootLayout,
alignmentX=null,alignmentY=null,border=,flags=385,maximumSize=,minimumSize=,
preferredSize=]
Container:
super.ManagerWindow[dialog0,0,0,508x597,invalid,layout=java.awt.BorderLayout
,modeless,title=Manager Stocks
,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,4,23
,500x570,layout=javax.swing.JRootPane$RootLayout,alignmentX=null,alignmentY=
null,border=,flags=385,maximumSize=,minimumSize=,preferredSize=],rootPaneChe
ckingEnabled=true]
Container:
super.SuperApplication[frame0,0,0,350x150,invalid,layout=java.awt.BorderLayo
ut,title=,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JR
ootPane[,3,22,344x125,layout=javax.swing.JRootPane$RootLayout,alignmentX=nul
l,alignmentY=null,border=,flags=385,maximumSize=,minimumSize=,preferredSize=
],rootPaneCheckingEnabled=true]
 
A

Andrew Thompson

Thank you for your help

You're welcome.
I have to use this to get it works. Too long !!!!

this.getParent().getParent().getParent().getParent().getParent().setVisible(
false)

(chuckles) That was just to help you *find* it, but is
very fragile. There are probably better ways to get it,
like..

while ( c.getParent() ! instanceof TargetDialogClass ) {
c = c.getParent();
}

...or storing a reference to the parent when constructing, or..
 
T

Thomas Weidenfeller

Andrew said:
(chuckles) That was just to help you *find* it, but is
very fragile. There are probably better ways to get it,
like..

while ( c.getParent() ! instanceof TargetDialogClass ) {
c = c.getParent();
}
SwingUtilities.getWindowAncestor()

..or storing a reference to the parent when constructing, or..

Or have some central repository where all your top-level windows
register and de-register.

/Thomas
 
A

Andrew Thompson

SwingUtilities.getWindowAncestor()

...(checks subject line) Good point. I had
completely lost that this was a *Swing* GUI
matter and was thinking 'valid back to 1.1!'
( same old story ;)

As an aside, do you know if that method has
been available since the introduction of Swing?
The javadocs are a bit vague on some of the
'@since' tags..
 
T

Thomas Weidenfeller

Andrew said:
As an aside, do you know if that method has
been available since the introduction of Swing?

AFAIK it was added for 1.3
The javadocs are a bit vague on some of the
'@since' tags..

That's an understatement. Correct @since tags seem to be completely out
of the scope of Sun's QA.

/Thomas
 
V

Vova Reznik

shoa said:
this.getParent().getParent().dispose() //this is the button
but it doesn't work

It will not work, because parent of content pane is not JDialog.
You should not be able to compile.
 
V

Vova Reznik

shoa said:
Thank you
I now understand more about getParent() function
It is not about understanding *getParent()* method.
It is about understanding who is the PARENT. :)
 
V

Vova Reznik

Thomas said:
Or have some central repository where all your top-level windows
register and de-register.

/Thomas

SwingUtilities.getWindowAncestor(Component)
or
jComponent.getTopLevelAncestor()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top