Solved

Separating code

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

776 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