Smart implementation of Event-Handling (MVC) ??!!

T

Tobi Krausl

Hi there!
In my Java-program there are among others two classes:

class Java_GUI: This class "builds" the GUI (JFrame) and contains all
necessary components (Lists, Buttons…).

class MyEventHandler: This class is intended to handle all events
occuring in "Java_GUI". Additionally it will be necessary to update
some controls in "Java_GUI" depending on the events. It could be for
example necessary to change the items of a list declared in
"Java_GUI".

My plan is as follwos:
In the constructor of Java_GUI I reference a MyEventHandler-Object. So
far it's fine.
But how can I directly access the public attributes (components) in
"Java_GUI" from inside a MyEventHandler-class? I don't want to write
special public-methods in "Java_GUI" which provide the functionality
to update private attributes (components).

How could I solve my problem in a smart way? Corresponding to MVC I
would have to create something like a "Controller", right? Would
implementing "Observer" help in some way and if yes, how?

Thank you, Tobi
 
J

Jesper Nordenberg

Hi there!
In my Java-program there are among others two classes:

class Java_GUI: This class "builds" the GUI (JFrame) and contains all
necessary components (Lists, Buttons?).

class MyEventHandler: This class is intended to handle all events
occuring in "Java_GUI". Additionally it will be necessary to update
some controls in "Java_GUI" depending on the events. It could be for
example necessary to change the items of a list declared in
"Java_GUI".

My plan is as follwos:
In the constructor of Java_GUI I reference a MyEventHandler-Object. So
far it's fine.
But how can I directly access the public attributes (components) in
"Java_GUI" from inside a MyEventHandler-class? I don't want to write
special public-methods in "Java_GUI" which provide the functionality
to update private attributes (components).

How could I solve my problem in a smart way? Corresponding to MVC I
would have to create something like a "Controller", right? Would
implementing "Observer" help in some way and if yes, how?

Given the small amount of information about your class structure, I
think you misunderstand the MVC pattern.

- The "view" layer in this case is Swing and any Swing components
you've created yourself.

- The "model" layer contains the data you want to visualize in the
"view" layer. It shouldn't depend on any classes in the "controller"
and "view" layers. It should have some listener interfaces that is
triggered when the data is changed.

- The "controller" layer should use classes in both the "model" and
"view" layer and connect the two. It's here you should implement the
interfaces from the "model" and "view" layers (for example
MouseListener etc). It's here you should put your Swing event code.

/Jesper Nordenberg
 
T

Tobi Krausl

Ok, maybe I should have been more precise. My application has in
general this strucutre:

class Java_GUI extends JFrame {
 
J

Jesper Nordenberg

Ok, maybe I should have been more precise. My application has in
general this strucutre:

class Java_GUI extends JFrame {
.
.
.
DefaultListModel lModel=new DefaultListModel();
JList myList=new JList(lModel);
.
.
//Add myList to a panel...
.
.
} //Java_GUI


class File_Access {
// The class has methods which read periodically data from a file

public String getNextNewEntry() // returns the next new entry found
in the file..
.
.
}


Ok so far?
What I want to do now is to update myList everytime getNextNewEntry()
retruns a value.

You should create a FileAccessListener interface:

public interface FileAccessListener {
void newEntryAdded(String entry);
}

Create new methods in File_Access (bad name, see Java coding
standards) that allows you to add and remove listeners. Create a
thread in File_Access that checks the file and notifies the
FileAccessListener's when a new entry is added in the file.

The Java_GUI (bad name again) implements the FileAccessListener
interface, adds itself as listener to a File_Access object and updates
the GUI when newEntryAdded() is called. Remember that you must update
the Swing GUI in the AWT event thread (see
SwingUtilities.invokeLater()).

Hope this helps!

/Jesper Nordenberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

mvc design doubts 9
spring mvc question 0
Error Handling 27
ASP.NET MVC 3
JSF event-handling question 0
Listeners and MVC 3
responsibility of the C in MVC 2
Cookie Handling 1

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top