?
Solved

Separating code

Posted on 2001-06-26
3
Medium Priority
?
202 Views
Last Modified: 2010-03-31
Hello
I want to separate my code handling my JFrame+components from the code handling all the events for the application+"underlying" code. So if a button in the mainwindow is pressed all the event handling is processed in another .class file, in order to make the code more easy to overlook.
I.e., can I create an ActionListener in a 2nd class-file and then add that listener to the button i the 1st main class-file?...calling the listener from a component in another class?

Thanks in advance
/ gweek



0
Comment
Question by:GWeeK
[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
3 Comments
 

Expert Comment

by:umargul
ID: 6227863
try the inner class concept
0
 

Accepted Solution

by:
kainhart earned 600 total points
ID: 6228160
Sure you can do pretty much what your talking about. I'm doing the same sort of concept but I'm only using a KeyListener. What I did was I made a new class which has a sole purpose of handling keyboard input, in your case you would be handling other input related to the ActionListener interface.


public class ActionHandler implements ActionListener
{
    // fill this class with the methods that must
    // overided in order to handle the action events
}


Now in a separate class somewhere mostlikely in the constructor you can do the following.

...
ActionHandler myActionHandler = new ActionHandler();

JButton myButton = new JButton();
myButton.addActionListener(myActionHandler);
...

Then add the JButton myButton to the contentPane of whatever container object your using (JFrame, JApplet, JDialog,...).

Good luck, I hope this works for you.
0
 

Author Comment

by:GWeeK
ID: 6228815
Thnx kainhart!
Just what I was looking for.
Thnx again. :p
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

764 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