Solved

handling events in swing

Posted on 2004-04-26
6
363 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 75
difference between sorce folder and folder in eclipise 3 45
SequenceInputStream example 3 22
wild fly 8 startup error 2 43
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

730 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