Solved

Separating code

Posted on 2001-06-26
3
196 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
Comment Utility
try the inner class concept
0
 

Accepted Solution

by:
kainhart earned 150 total points
Comment Utility
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
Comment Utility
Thnx kainhart!
Just what I was looking for.
Thnx again. :p
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now