A button close in JDialog

Discussion in 'Java' started by shoa, Jun 29, 2005.

  1. shoa

    shoa Guest

    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
    shoa, Jun 29, 2005
    #1
    1. Advertising

  2. Andrew Thompson, Jun 29, 2005
    #2
    1. Advertising

  3. shoa

    shoa Guest

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

    "Andrew Thompson" <> wrote in message
    news:1nauxpvkf9gdu.v2u7rmlexaia$...
    > On Wed, 29 Jun 2005 14:39:30 +1200, shoa wrote:
    >
    > > 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)
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
    shoa, Jun 29, 2005
    #3
  4. shoa

    Vova Reznik Guest

    shoa wrote:
    > 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)
    Vova Reznik, Jun 29, 2005
    #4
  5. On Thu, 30 Jun 2005 00:18:29 +1200, shoa wrote:

    (Please refrain from top-posting - further comments in-line)

    > "Andrew Thompson" <> wrote in message
    >> On Wed, 29 Jun 2005 14:39:30 +1200, shoa wrote:
    >>
    >>> ..When I
    >>> click this button, I want this dialog to close.

    >>
    >> setVisible(false)


    > 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);

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 29, 2005
    #5
  6. shoa

    shoa Guest

    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


    > Well, duuh.. Try.
    >
    > theDialog.setVisible(false);
    >
    > or.. (invoked from the right place)
    >
    > this.setVisible(false);
    >
    > ..which translates to
    >
    > setVisible(false);
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
    shoa, Jun 29, 2005
    #6
  7. On Thu, 30 Jun 2005 10:33:56 +1200, shoa wrote:

    > 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? ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 30, 2005
    #7
  8. shoa

    shoa Guest

    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]
    shoa, Jun 30, 2005
    #8
  9. On Thu, 30 Jun 2005 12:34:14 +1200, shoa wrote:

    > 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..

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 30, 2005
    #9
  10. shoa

    shoa Guest

    Thank you
    I now understand more about getParent() function

    "Andrew Thompson" <> wrote in message
    news:1owpm1cv94obc$.qxw3qw22o90e$...
    > On Thu, 30 Jun 2005 12:34:14 +1200, shoa wrote:
    >
    > > 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..
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
    shoa, Jun 30, 2005
    #10
  11. Andrew Thompson wrote:
    > (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


    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Jun 30, 2005
    #11
  12. On Thu, 30 Jun 2005 09:40:38 +0200, Thomas Weidenfeller wrote:

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

    >
    > 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..

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jun 30, 2005
    #12
  13. Thomas Weidenfeller, Jun 30, 2005
    #13
  14. shoa

    Vova Reznik Guest

    shoa wrote:

    > 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.
    Vova Reznik, Jun 30, 2005
    #14
  15. shoa

    Vova Reznik Guest

    shoa wrote:
    > Thank you
    > I now understand more about getParent() function
    >
    > "Andrew Thompson" <> wrote in message
    > news:1owpm1cv94obc$.qxw3qw22o90e$...
    >
    >>On Thu, 30 Jun 2005 12:34:14 +1200, shoa wrote:
    >>
    >>
    >>>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..
    >>
    >>--
    >>Andrew Thompson
    >>http://www.PhySci.org/codes/ Web & IT Help
    >>http://www.PhySci.org/ Open-source software suite
    >>http://www.1point1C.org/ Science & Technology
    >>http://www.LensEscapes.com/ Images that escape the mundane

    >
    >
    >

    It is not about understanding *getParent()* method.
    It is about understanding who is the PARENT. :)
    Vova Reznik, Jun 30, 2005
    #15
  16. shoa

    Vova Reznik Guest

    Thomas Weidenfeller wrote:
    > Andrew Thompson wrote:
    >
    >> (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
    >
    >


    SwingUtilities.getWindowAncestor(Component)
    or
    jComponent.getTopLevelAncestor()
    Vova Reznik, Jun 30, 2005
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denon
    Replies:
    1
    Views:
    571
    Saravana [MVP]
    Nov 14, 2003
  2. Mariano
    Replies:
    0
    Views:
    583
    Mariano
    Dec 2, 2006
  3. emekadavid

    JDialog from Icon close

    emekadavid, Jun 6, 2008, in forum: Java
    Replies:
    1
    Views:
    412
    RedGrittyBrick
    Jun 6, 2008
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    845
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Replies:
    5
    Views:
    309
Loading...

Share This Page