Results 1 to 4 of 4
  1. #1
    broli is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Unhappy cant understand objects life in this particular code

    well, the title says half of it.
    after reading a introductory java book, decided to expand on one of the examples. this example had some info hardcoded and i wanted to ask the user for it (GUI app)

    I googled and found in the oracle docs, that they recommend using a "jdialog". using intellij idea "new dialog" option, wrote a few lines of code that I *supposed* it should work, but its not working

    Intelliji Idea code works like this. The class constructor adds the controls , buttons, ect. Adding a few Event listeners for the user cliking on OK or cancel and leaves a function called onOK to put my code.

    the problem is that at some point between after the onOK method is finished, but before returning to the "go" method, all the Objects are lost somewhere. nothing I create or modify is kept. and im pretty sure its because im doing something really dumb somewhere

    Java Code:
    Import javax.swing.*;
    import java.awt.event.*;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    public class Login extends JDialog {
        private JPanel contentPane;
        private JButton buttonOK;
        private JButton buttonCancel;
        private JPanel logInData;
        private JTextField nickNameTextField;
        private String nickName;
        private JTextField serverTextField;
        private String server;
        private JTextField serverPortTextField;
        private String serverPort;
        private UserConnection user;
    
        public Login() {
            setContentPane(contentPane);
            setModal(true);
            getRootPane().setDefaultButton(buttonOK);
    
            buttonOK.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onOK();
                }
            });
    
            buttonCancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCancel();
                }
            });
    
    // call onCancel() when cross is clicked
            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    onCancel();
                }
            });
    
    // call onCancel() on ESCAPE
            contentPane.registerKeyboardAction(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCancel();
                }
            }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    
        }
    
    
        private void onOK() {
    
            nickName = nickNameTextField.getText();
            server = serverTextField.getText();
            serverPort = serverPortTextField.getText();
            dispose();
        }
    
        private void onCancel() {
    // add your code here if necessary
    
            dispose();
        }
    
        public void setUser(UserConnection user) {
            this.user = user;
        }
    
        public UserConnection go() {
            
            Login dialog = new Login();
            
            dialog.pack();
            
            dialog.setVisible(true);
            
            assert  (nickName != null) : "nickname is null";
    
            user = new UserConnection(nickName,server,serverPort);
    
            assert (this.user != null) : "Login was about to return a null UserConnection";
    
    
            return this.user;
        }
    }
    The UserConnection class is a simple class to hold the 3 values.

    I tried creating the UserConnection object inside the onOK and the same happens

    the go method, for some reason executes until the point where "dialog.setVisible(true);" its called, and waits there untill the GUI is "disposed()" inside the "onOK()" method
    but when the execution resumes in the method "go()" and all the user input is lost. tried storing it in new variables, using something like
    Java Code:
    String sometext = new String(someJTextField.gettext());
    and its not working either. im completely lost

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: cant understand objects life in this particular code

    Post something compilable(i.e, code with a main method, and all the needed classes). We can start from there.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: cant understand objects life in this particular code

    Quote Originally Posted by broli View Post
    the go method, for some reason executes until the point where "dialog.setVisible(true);" its called, and waits there untill the GUI is "disposed()" inside the "onOK()" method
    That's the expected behavior when showing a modal dialog -- not just in Java/Swing but in just about every GUI platform I've ever used, excepting Java FX 2.x.

    You need to query the values after the dialog is dispose()d or hidden; and never use a String constructor where a simple assignment statement suffices.
    Java Code:
    String sometext = /*new String(*/someJTextField.gettext()/*)*/;
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    broli is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: cant understand objects life in this particular code

    ok, sorry. here is a simplified and compilable example of what is happening

    "main" app, starts and fires a modal dialog to ask the user some input

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class MainApp {
        JTextField someTextField;
        String userString;
    
        public static void main (String[] args){
            new MainApp().go();
        }
    
        private void go() {
            JFrame frame = new JFrame("Main app");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel mainPanel = new JPanel () ;
    
            someTextField = new JTextField(20);
            someTextField.setEditable(false);
    
            mainPanel.add(someTextField);
    
            frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
            frame.setSize(400,500);
            frame.setVisible(true);
    
            SomeDialog userinput = new SomeDialog();
    
            userString = userinput.getUserInput();
    
            someTextField.setText(userString);
        }
    }
    this is the dialog class. it creates de dialog gui . and sits there waiting for the user to put some text and click ok
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    public class SomeDialog extends JDialog {
    
        private JButton buttonOK;
        private JButton buttonCancel;
        private JLabel someLabel;
        private JTextField someTextInput;
        private String someString;
    
    
        public SomeDialog() {
    
            setModal(true);
            JPanel panel = new JPanel();
    
            buttonOK = new JButton("OK");
            buttonCancel = new JButton("cancel");
            someLabel = new JLabel("enter some input");
            someTextInput = new JTextField(30);
    
    
            panel.add(someLabel);
            panel.add(someTextInput);
            panel.add(buttonOK);
            panel.add(buttonCancel);
    
            getContentPane().add(panel);
    
            getRootPane().setDefaultButton(buttonOK);
    
            buttonOK.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onOK();
                }
            });
    
            buttonCancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCancel();
                }
            });
    
    // call onCancel() when cross is clicked
            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    onCancel();
                }
            });
    
        }
    
    
        private void onOK() {
            someString = someTextInput.getText();
            System.out.println("1: "+someString);
            dispose();
            System.out.println("2: "+someString);
        }
    
        private void onCancel() {
    // add your code here if necessary
    
            dispose();
        }
    
    
        public String getUserInput() {
    
            SomeDialog dialog = new SomeDialog();
    
            dialog.pack();
    
            //the dialog is shown, the user enters text, and clocks ok.
            dialog.setVisible(true);
            //after the user clicks ok, i cant access the String
    
            System.out.println("3: "+someString);
    
            return someString;
        }
    }
    doing a test run and writing "asd" in the dialog, i get this in the console

    1: asd
    2: asd
    3: null

    why, when the method called "getUserInput" resumes after the modal dialog is disposed(), the string is null?

    its not just a string, any object i create or modify inside the "onOK()" method, its lost after i ends

    thanks for the replys and sorry for the english, im not a native speaker

Similar Threads

  1. I don't understand the code help!
    By gomdohri in forum New To Java
    Replies: 3
    Last Post: 10-15-2011, 02:58 PM
  2. Replies: 1
    Last Post: 05-24-2011, 10:25 PM
  3. need to understand code
    By Masken2 in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:21 PM
  4. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  5. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •