Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting data back from a child form

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Getting data back from a child form

    Hello everyone

    I am frantically trying to learn Java and have now found myself wondering if I am passing variables back from a temporary child dialog back to the caller correctly. If somebody would be so good, I would appreciate it if you could look at my example classes and tell me if I am doing it correctly. If not, why not!

    For this example, in C++ I would probably pass in a pointer and have the caller updated that way but of course Java doesn't support primitive type pointers.

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
     
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Example extends JFrame {
     
        //Instantiate the container object
          Container c = new Container();
     
        public static void main(String[] args) {
     
            Example w = new Example();
            w.initGUI();
     
        }
     
        private void initGUI(){
     
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            // SETUP BASIC GUI COMPONENTS
            JPanel p = new JPanel();
            JButton b = new JButton("CLICK ME");
            final JTextField tf = new JTextField();
     
            // SETUP ACTION LISTENER
            b.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent evt){
     
                    DialogHarvester dialog = new DialogHarvester(c);
                    dialog.setModal(true);
                    dialog.setVisible(true);
     
                    tf.setText(c.userParameter);
     
                    return;             
                }
            });
     
            // SIZE THEM AND ADD TO THE FORM
            tf.setPreferredSize(new Dimension(100,25));
            tf.setEditable(false);
            p.add(b);
            p.add(tf);
     
            this.add(p);
            this.setSize(200, 100);
            this.setVisible(true);
     
            return;
        }
     
    }

    AND

    import java.awt.Dimension;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
    public class DialogHarvester extends JDialog{
     
        Container c;
        JTextField t;
     
        DialogHarvester(Container con){
     
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            c = con;
            this.initGUI();
     
        }
     
        private void initGUI(){
     
            // SETUP BASIC GUI COMPONENTS
            JPanel p = new JPanel();
            JButton b = new JButton("OK");
     
            t = new JTextField();
     
     
            // SETUP ACTION LISTENER
            b.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent evt){
                    exitForm();
                }
            });
     
            // SIZE THEM AND ADD TO THE FORM
            t.setPreferredSize(new Dimension(100,25));
            p.add(t);
            p.add(b);
     
            this.add(p);
            this.setSize(100,100);
     
        }
     
        private void exitForm(){
     
            c.userParameter = t.getText();
            this.dispose();
        }
     
    }

    AND

    public class Container {
     
        Container(){}
     
        public String userParameter;
     
    }

    Many thanks for reading and I thank people in advance for helping me yet again.

    Regards
    Nikki


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting data back from a child form

    Well, does it work how you expect? Does the data get passed how you want it to?

    You might also want to look into this: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Getting data back from a child form

    Hi, thank you.

    Yes, it does, works perfectly! I was really thinking about best practice.

    Thank you

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting data back from a child form

    Quote Originally Posted by ishtar View Post
    Hi, thank you.

    Yes, it does, works perfectly! I was really thinking about best practice.

    Thank you
    I really wouldn't worry about best practices at this point. If it works, and if you understand why it works, that's probably good enough for now. No matter what code you write, in six months you're going to look back and wonder what you were thinking.

    That being said, I think returning something in the way outlined in the tutorial I gave you is the best practice. At the very least, you might want to call a set function instead of accessing the variable directly.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ishtar (October 5th, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Getting data back from a child form

    Thank you very much Kevin.

    I'm off to do some reading....

Similar Threads

  1. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 02:21 PM
  2. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum Object Oriented Programming
    Replies: 3
    Last Post: August 12th, 2011, 04:42 AM
  3. saving the data of the child jsp into parent jsp
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 15th, 2011, 11:05 AM
  4. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 10:43 AM
  5. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 09:51 AM