Solved

Separating code

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 52
Java Restore security prompts not working 10 67
Html Table Looping (part 2) 5 27
What is the use of Forwarding Class in java 1 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 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