?
Solved

handling events in swing

Posted on 2004-04-26
6
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 225 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 16

Accepted Solution

by:
gnoon earned 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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