Solved

handling events in swing

Posted on 2004-04-26
6
364 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 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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