Solved

handling events in swing

Posted on 2004-04-26
6
359 Views
Last Modified: 2013-11-23
what is the best way to handle events across several frames in an application. eg. how do i pass back values from textfields, passwordfields or textarea in a jdialog back to the parent frame when user clicks a submit button. are there any tutorials for this kind of event handling across classes without using inner classes. are there any design patterns for this.
0
Comment
Question by:vijaywm
  • 4
6 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 75 total points
ID: 10922237
Well, it's not because you dispose/close the dialog that it isn't there anymore.
So, you can still call all it's getter functions.

MyDialog myDialog = new MyDialog();
...
// 1) Show it

// 2) Here it is closed by pressing <Submit> or whatever
// but that doesn't prevent you to:

String field1 = myDialog.getField1();
String field2 = myDialog.getField2();
...
0
 

Author Comment

by:vijaywm
ID: 10922304
great. thanks.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10922386
Is your Q completely answered?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Accepted Solution

by:
gnoon earned 50 total points
ID: 10925080
Create your own event and listener class to handle event it's easy concept and implementation.

There're 3 classes and 1 interface in the session.

1 - Event generator class (generate some events)
2 - Event class (contains data of an event)
3 - Listener interface (contains event methods and will executed when some events occur)
4 - Event listening class (contains a reference of event generator)

For example, you have

  SigninListener interface : (Listener interface)
        interface SigninListener {
            public void whenUserClickSignin(SigninEvent e);
        }

  SigninEvent class : (Event class)
        public class SigninEvent {
            String username;
            String password;
            public SigninEvent(String username,String password) {
                this.username = username;
                this.password = password;
            }
            /* and other get/set method */
        }

  SigninDialog class : (Event generator class)
        public class SigninDialog extends JDialog implements ActionListener {
            JTextField jt_User,jt_Passwd;
            JButton jb_Signin;
            SigninListener signinlistener;

            public SigninDialog() {
                /* build ui here */
                signinlistener = new SigninListener() {
                    public void whenUserClickSignin(SigninEvent e) {/* default implementation */}
                };
                jb_Signin.addActionListener(this);
            }

            public void addSigninListener(SigninListener l) {
                signinlistener = l;
            }

            public void actionPerformed(ActionEvent e) {
                /* whenever user click sign-in button, (s)he is generating sign-in event.
                 */
                SigninEvent se = new SigninEvent(jt_User.getText(), jt_Passwd.getText());
                signinlistener.whenUserClickSignin(se);
            }
        }

  MainFrame class : (Event listening class)
        public MainFrame extends JFrame implements SigninListener {
            SigninDialog jd_Signin;
            public MainFrame() {
                /* build ui here */
                jd_Signin.addSigninListner(this);
            }
            public void whenUserClickSignin(SigninEvent e) {
                jd_Signin.dispose();
                /* user authentication here */
            }
        }

I hope you'll see the concept from the classes above.
G noon
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10925973
>> great. thanks.

If you feel like your question is answered,
then please kindly close this question by accepting one or more of the comments that were useful to you.
If you feel like your question is not yet answered, keep asking more explanation.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10927210
Thanks for accepting.
That keeps us answering your future questions too.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now