JDialog Question...

Discussion in 'Java' started by Ramon, Dec 1, 2009.

  1. Ramon

    Ramon Guest

    Hi,

    I'm trying to make a class that inherits from JDialog. The dialog has
    two buttons: Ok and Cancel. When the OK button is clicked, the main
    form is trying to read a result that is stored in the dialog's variable.

    Question: Is there a safe way how to read a dialog's instance variable
    (using a getter) after the dialog is set to invisible (i.e.
    this.setVisible(false)) ?

    PS: In general I am able to read the dialog's variables but, apparently,
    sometimes the dialog is destroyed by the garbage collector...

    Thanks.
    Ramon, Dec 1, 2009
    #1
    1. Advertising

  2. Ramon

    Daniel Pitts Guest

    Ramon wrote:
    > Hi,
    >
    > I'm trying to make a class that inherits from JDialog. The dialog has
    > two buttons: Ok and Cancel. When the OK button is clicked, the main
    > form is trying to read a result that is stored in the dialog's variable.
    >
    > Question: Is there a safe way how to read a dialog's instance variable
    > (using a getter) after the dialog is set to invisible (i.e.
    > this.setVisible(false)) ?
    >
    > PS: In general I am able to read the dialog's variables but, apparently,
    > sometimes the dialog is destroyed by the garbage collector...
    >
    > Thanks.

    If you have a reference to the JDialog, then it will not be garbage
    collected. Are having a particular problem? What are the symptoms?

    Provide an SSCCE if you want to get the most help from this newsgroup.
    See <http://sscce.org> if you don't know how or why to provide an SSCCE.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 1, 2009
    #2
    1. Advertising

  3. Ramon

    Ramon Guest

    The following is a short version of my Jdialog...

    public class InputDialog extends JDialog
    {
    private Boolean okButtonPressed_; // true = OK button pressed

    /* init here ... */


    /*
    * OK button even handler
    */
    private void okButtonActionPerformed(java.awt.event.ActionEvent
    evt)
    {
    this.setVisible(false);
    this.okButtonPressed_ = true;
    }
    }


    This is a method of the class which is calling my InputDialog:

    private void cmdUserSettings()
    {
    InputDialog inputDialog = new InputDialog(null, true);

    // if ok button was pressed
    if (inputDialog.getOkButtonPressed()) // #####
    { /* ... */ }
    else
    { /* ... */ }

    if (inputDialog != null)
    inputDialog.dispose();
    }

    The line marked with ##### is sometimes throwing a NullPointerException,
    thus indicating that somehow the garbage is destroying inputDialog...

    Any suggestions?




    Daniel Pitts wrote:
    > Ramon wrote:
    >> Hi,
    >>
    >> I'm trying to make a class that inherits from JDialog. The dialog has
    >> two buttons: Ok and Cancel. When the OK button is clicked, the main
    >> form is trying to read a result that is stored in the dialog's variable.
    >>
    >> Question: Is there a safe way how to read a dialog's instance
    >> variable (using a getter) after the dialog is set to invisible (i.e.
    >> this.setVisible(false)) ?
    >>
    >> PS: In general I am able to read the dialog's variables but,
    >> apparently, sometimes the dialog is destroyed by the garbage collector...
    >>
    >> Thanks.

    > If you have a reference to the JDialog, then it will not be garbage
    > collected. Are having a particular problem? What are the symptoms?
    >
    > Provide an SSCCE if you want to get the most help from this newsgroup.
    > See <http://sscce.org> if you don't know how or why to provide an SSCCE.
    >
    Ramon, Dec 1, 2009
    #3
  4. Ramon

    Daniel Pitts Guest

    Please don't top-post. Harder to read it makes the post.
    Ramon wrote:
    > Daniel Pitts wrote:
    >> Ramon wrote:
    >>> Hi,
    >>>
    >>> I'm trying to make a class that inherits from JDialog. The dialog
    >>> has two buttons: Ok and Cancel. When the OK button is clicked, the
    >>> main form is trying to read a result that is stored in the dialog's
    >>> variable.
    >>>
    >>> Question: Is there a safe way how to read a dialog's instance
    >>> variable (using a getter) after the dialog is set to invisible (i.e.
    >>> this.setVisible(false)) ?
    >>>
    >>> PS: In general I am able to read the dialog's variables but,
    >>> apparently, sometimes the dialog is destroyed by the garbage
    >>> collector...
    >>>
    >>> Thanks.

    >> If you have a reference to the JDialog, then it will not be garbage
    >> collected. Are having a particular problem? What are the symptoms?
    >>
    >> Provide an SSCCE if you want to get the most help from this newsgroup.
    >> See <http://sscce.org> if you don't know how or why to provide an SSCCE.
    >>

    > The following is a short version of my Jdialog...
    >
    > public class InputDialog extends JDialog
    > {
    > private Boolean okButtonPressed_; // true = OK button pressed
    >
    > /* init here ... */
    >
    >
    > /*
    > * OK button even handler
    > */
    > private void okButtonActionPerformed(java.awt.event.ActionEvent evt)
    > {
    > this.setVisible(false);
    > this.okButtonPressed_ = true;
    > }
    > }
    >
    >
    > This is a method of the class which is calling my InputDialog:
    >
    > private void cmdUserSettings()
    > {
    > InputDialog inputDialog = new InputDialog(null, true);
    >
    > // if ok button was pressed
    > if (inputDialog.getOkButtonPressed()) // #####
    > { /* ... */ }
    > else
    > { /* ... */ }
    >
    > if (inputDialog != null)
    > inputDialog.dispose();
    > }
    >


    > The line marked with ##### is sometimes throwing a NullPointerException,
    > thus indicating that somehow the garbage is destroying inputDialog...

    While this isn't an SSCCE, I do believe I'm able to tell you the solution.

    The garbage collector will not set a value to null, so the NPE is caused
    by something else. In this case, your okButtonPressed_ is null.

    If null doesn't make sense for your value, you should use the primitive
    version, in this case "boolean" with a lower-case 'b'.

    A couple of other comments. In Java, the convention is to avoid "_" in
    names, except constants, which are ALL_UPPERCASE_LETTERS. Thus, a
    better declaration for your case is:

    private boolean okPressed;

    public boolean isOkPressed() {
    return okPressed;
    }

    One other comment, for your use-case, you might be happier using
    JOptionPane. some quick googling should give you some hints on how to
    use it.
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 1, 2009
    #4
  5. Ramon

    Ramon Guest

    What a silly mistake :) Thank you Daniel for your useful tips.

    Have a good day/night.


    Daniel Pitts wrote:
    > Please don't top-post. Harder to read it makes the post.
    > Ramon wrote:
    >> Daniel Pitts wrote:
    >>> Ramon wrote:
    >>>> Hi,
    >>>>
    >>>> I'm trying to make a class that inherits from JDialog. The dialog
    >>>> has two buttons: Ok and Cancel. When the OK button is clicked, the
    >>>> main form is trying to read a result that is stored in the dialog's
    >>>> variable.
    >>>>
    >>>> Question: Is there a safe way how to read a dialog's instance
    >>>> variable (using a getter) after the dialog is set to invisible (i.e.
    >>>> this.setVisible(false)) ?
    >>>>
    >>>> PS: In general I am able to read the dialog's variables but,
    >>>> apparently, sometimes the dialog is destroyed by the garbage
    >>>> collector...
    >>>>
    >>>> Thanks.
    >>> If you have a reference to the JDialog, then it will not be garbage
    >>> collected. Are having a particular problem? What are the symptoms?
    >>>
    >>> Provide an SSCCE if you want to get the most help from this
    >>> newsgroup. See <http://sscce.org> if you don't know how or why to
    >>> provide an SSCCE.
    >>>

    >> The following is a short version of my Jdialog...
    >>
    >> public class InputDialog extends JDialog
    >> {
    >> private Boolean okButtonPressed_; // true = OK button pressed
    >>
    >> /* init here ... */
    >>
    >>
    >> /*
    >> * OK button even handler
    >> */
    >> private void okButtonActionPerformed(java.awt.event.ActionEvent evt)
    >> {
    >> this.setVisible(false);
    >> this.okButtonPressed_ = true;
    >> }
    >> }
    >>
    >>
    >> This is a method of the class which is calling my InputDialog:
    >>
    >> private void cmdUserSettings()
    >> {
    >> InputDialog inputDialog = new InputDialog(null, true);
    >>
    >> // if ok button was pressed
    >> if (inputDialog.getOkButtonPressed()) // #####
    >> { /* ... */ }
    >> else
    >> { /* ... */ }
    >>
    >> if (inputDialog != null)
    >> inputDialog.dispose();
    >> }
    >>

    >
    >> The line marked with ##### is sometimes throwing a
    >> NullPointerException, thus indicating that somehow the garbage is
    >> destroying inputDialog...

    > While this isn't an SSCCE, I do believe I'm able to tell you the solution.
    >
    > The garbage collector will not set a value to null, so the NPE is caused
    > by something else. In this case, your okButtonPressed_ is null.
    >
    > If null doesn't make sense for your value, you should use the primitive
    > version, in this case "boolean" with a lower-case 'b'.
    >
    > A couple of other comments. In Java, the convention is to avoid "_" in
    > names, except constants, which are ALL_UPPERCASE_LETTERS. Thus, a
    > better declaration for your case is:
    >
    > private boolean okPressed;
    >
    > public boolean isOkPressed() {
    > return okPressed;
    > }
    >
    > One other comment, for your use-case, you might be happier using
    > JOptionPane. some quick googling should give you some hints on how to
    > use it.
    Ramon, Dec 1, 2009
    #5
  6. Ramon

    Guest

    On Dec 1, 9:50 pm, Ramon <> wrote:
    > Hi,
    >
    > I'm trying to make a class that inherits from JDialog. The dialog has
    > two buttons: Ok and Cancel. When the OK button is clicked, the main
    > form is trying to read a result that is stored in the dialog's variable.


    Don't do it that way... It's really not a nice.

    Imagine the point of your dialog is to create some 'Nano' object
    (whatever, it's just an example).

    Usually the only thing that you care about in your app is to be
    handled
    that Nano object (or a failure, in some apps).

    So simply pass a callback when creating your JDialog and call it
    when the user clicks on OK.

    public final class MyDialog extends JDialog {

    public MyDialog(
    @NotNull final Handable<Nano> callback
    ) {
    ...
    btnOK.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    callback.handle( shinyNanoObject );
    dispose();
    }
    } );
    }

    }


    Keeping a reference to the JDialog, starting to read it's
    reference/primitives etc. is not nice OO.

    It's all objects and interaction between said objects.

    All that you care about is what kind of object your JDialog
    can handle...

    Don't go into low level details, focus on the bigger OO picture.

    If you like to alienate your users and want an application
    that seems to be hung when the user mistakingly hides a modal
    window (or, worse, moves it to another virtual desktop), then
    use the idiom:

    Color after = JColorChooser.showDialog(...)

    But it's really piss-poor practices. If forces you to give
    a default 'fallback' value (wtf, a user picking 'red' and a
    user cancelling, while 'red' was the default is the same?
    A big joke I tell ya) and it forces you to compare the return
    value with the fallback to see if something changed or not and
    it doesn't tell you if the user clicked on OK or if he cancelled.
    In some case it can amount to the same, but for a great many
    scenario you'll need something more complicated than this toyish
    procedural way of doing things.

    The antithesis of OO...


    Alex
    , Dec 2, 2009
    #6
  7. Ramon

    Roedy Green Guest

    On Tue, 01 Dec 2009 22:50:51 +0100, Ramon
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Question: Is there a safe way how to read a dialog's instance variable
    >(using a getter) after the dialog is set to invisible (i.e.
    >this.setVisible(false)) ?


    visibility has nothing whatsoever to do with the ability to read/write
    variables.

    for sample JDialog code see
    http://mindprod.com/jgloss/jdialog.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The future has already happened, it just isn’t evenly distributed.
    ~ William Gibson (born: 1948-03-17 age: 61)
    Roedy Green, Dec 9, 2009
    #7
    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. Alexandr Molochnikov

    Re: JDialog in taskbar under Linux

    Alexandr Molochnikov, Jul 31, 2003, in forum: Java
    Replies:
    0
    Views:
    1,808
    Alexandr Molochnikov
    Jul 31, 2003
  2. Larry Coon

    Controlling where JDialog opens

    Larry Coon, Oct 23, 2003, in forum: Java
    Replies:
    3
    Views:
    11,200
    Kleopatra
    Oct 24, 2003
  3. venky
    Replies:
    1
    Views:
    3,244
    Chandler
    Nov 13, 2003
  4. cccc
    Replies:
    0
    Views:
    1,822
  5. Inertia_sublimation
    Replies:
    5
    Views:
    2,105
    Inertia_sublimation
    Jan 3, 2004
Loading...

Share This Page