Solved

Separating code

Posted on 2001-06-26
3
201 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 150 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

717 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