NetBeans Forums

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

Newbie question: user input from a dialog box

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



Joined: 20 Nov 2010
Posts: 3
Location: CT

PostPosted: Sat Nov 20, 2010 2:35 pm    Post subject: Newbie question: user input from a dialog box Reply with quote

Ok, so I'm using netbeans 6.9.1 to make a netbeans platform application. And all I'm trying to do here is store the user input from a text field in a dialog box somewhere after the dialog box closes. So my program will have some server hostname or ip address stored in a variable somewhere.

So, this is the code for the 'Server Select' toolbar button:
Code:

public final class ServerSelect implements ActionListener {

    private String server;

    public void setServer(String newServer) {
        server = newServer;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ServerSelectDialog dialog = new ServerSelectDialog(null, true);
        dialog.setVisible(true);
    }

}


This is the class for the dialog box (there's more code in here really but i'm just showing the OK button handler code):
Code:

public class ServerSelectDialog extends javax.swing.JDialog {

    /* OK button */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {                                         
        System.out.println(jTextField1.getText());
        setVisible(false);
        dispose();
    }   


    /* ... */

}


So it prints out what is in 'jTextField1' to the console when I click OK... great. But I need to save it to a variable somewhere. In the OK button handler I've tried ServerSelect.setServer(jTextField1.getText()) but clearly that's not right. I would think it would be something like instanceOfServerSelect.setServer(jTextField1.getText()) but i have no idea where ServerSelect even gets instantiated. And is the instance of ServerSelect even where I should store this?

I normally code cgi apps in C or Perl... I haven't done java in a while and this is my first time with netbeans but it seems like I could do a lot with it if I could just get the basics down...

Thanks
Back to top
areeda



Joined: 28 Aug 2008
Posts: 469
Location: Los Angeles

PostPosted: Sat Nov 20, 2010 3:35 pm    Post subject: Reply with quote

There a a few ways to do this.

My usual approach is to store it in the dialog object with a getter method. On OK set a status variable and call setVisible(false) instead of dispose. This keeps the dialog object after closing the window.

Then in the object that inovkes this dialog, check the status and if OK get all the variables from the dialog.

Another not so clean but more C-like approach is to pass a pointer to the calling object to the Dialog object. Then on OK call a setter method in the caller.

If either of those appeal to you but is not clear I can go into more detail.

joe
Back to top
wildbill



Joined: 20 Nov 2010
Posts: 3
Location: CT

PostPosted: Sat Nov 20, 2010 5:10 pm    Post subject: Newbie question: user input from a dialog box Reply with quote

Cool I think I got it thanks to your tip, thanks! Not sure if this is the standard way of doing this sort of thing though but it seems to make sense this way...

So now I have:
Code:

public final class ServerSelect implements ActionListener {

    private ServerSelectDialog dialog;

    public ServerSelect()
    {
        dialog = new ServerSelectDialog(null, true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        dialog.setVisible(true);
    }

}


And...
Code:

public class ServerSelectDialog extends javax.swing.JDialog {

    private String server;

    /* ... */

    /* Cancel button */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {                                         
        setVisible(false);
        jTextField1.setText(server);
    }                                       

    /* OK button */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {                                         
        server = jTextField1.getText();
        setVisible(false);
    }     

    public String getServer()
    {
        return server;
    }
}


Smile

If anyone thinks this is the wrong way to do it though please let me know.
Back to top
areeda



Joined: 28 Aug 2008
Posts: 469
Location: Los Angeles

PostPosted: Sat Nov 20, 2010 6:55 pm    Post subject: Reply with quote

That's pretty close to the way I do it.

In your serverSelect, ActionPerformed method:

SetVisible(true) returns only after the dialog calls SetVisible(false)

So that's a good place to call getServer() and perhaps dispose of the dialog.
Back to top
wildbill



Joined: 20 Nov 2010
Posts: 3
Location: CT

PostPosted: Sat Nov 20, 2010 7:27 pm    Post subject: Reply with quote

Ok yeah it did seem a little strange to leave that dialog box hanging around even though it was not visible... Thanks again!

Code:

public final class ServerSelect implements ActionListener {

    private String server;

    @Override
    public void actionPerformed(ActionEvent e) {
        ServerSelectDialog dialog = new ServerSelectDialog(null, true);
       
        dialog.setServer(server);
        dialog.setVisible(true);

        server = dialog.getServer();
        dialog.dispose();
    }

}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans 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