Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

how to manage a multi-source file project

Thread view: 
vijay - 08 Mar 2005 14:45 GMT
Hello,

I actually have a java source file that makes the GUI using Swing. The
GUI has 12 buttons each performing different functionalities. It even
got a couple of text areas.

I do not want to clog the GUI code by adding all the button's event
listeners. So I actually want to make different classes for each button
event listener, namely, Button1Listener, Button2Listener.

I tried the same for Button1 but the problem is that the
Button1Listener class has to use the text area in my GUI code and my
GUI code has a
button1.addActionListener(new Button1Listener()) which gives
compilation errors. So, I am in a deadlock.

My question is, is there any other way I can do it without clogging the
GUI code. If no, how to resolve this problem.

regards,
vijay.
Sergio - 08 Mar 2005 21:35 GMT
> Hello,
>
[quoted text clipped - 17 lines]
> regards,
> vijay.

Why don't you create a single ButtonListener object that manages all the
buttons events and also capable to access the content of the text area?.
To proccess the different actions, the Listener could detect which
button is sending the action using the getSource() method or maybe
better using getActionCommand().
HTH
Sergio
vijay - 09 Mar 2005 04:40 GMT
> > Hello,
> >
[quoted text clipped - 25 lines]
> HTH
> Sergio

In my GUI code, I have
button1.addActionListener(new Button1Listener());

and in my Button1Listener class,
I need to access the jTextArea1 which is declared protected in my GUI
code (both classes are in the same package).

The problem is that to compile Button1Listener, I need the class file
of my GUI code and vice-versa. I am stuck in a deadlock here. There
must be some proper way to go about developing java projects, whose
knowledge I surely am missing.

This is the first time I am working in a live project, that's why a bit
stuck.

regards,
vijay.
Sergio - 10 Mar 2005 01:28 GMT
>>>Hello,
>>>
[quoted text clipped - 64 lines]
> regards,
> vijay.

As I suggested before, inestead of creating a new Button1Listener for
every single button, you may do something like this pseudocode:
in your GUI class:

uniqueButtonListener = new YourSpecializedButtonListener();
uniqueButtonListener.setTheTextAreaToRead(jTextArea1);

button1.addActionListener(uniqueButtonListener);
button2.addActionListener(uniqueButtonListener);
.
.
button897678.addButtonListener(uniqueButtonListener);

etc..

and your button listener class will be something like:

class YourSpecializedButtonListener extends ActionListener {

protected JTextArea theTextAreaToUse;

public void setTheTextAreaToRead(JTextArea textArea) {
  theTextAreaToUse = textArea;
}

public void actionPerformed (ActionEvent event) {

String command = event.getActionCommand();
if(command.equals("whatever")) {
    doWhateverYouHaveToDoWithTheJTextArea(theTextAreaToUse);
.
.

Now compile YourSpecializedButtonListener class and then the GUI class.

Best,
Sergio


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.