aspose file tools*
The moose likes Beginning Java and the fly likes GUI and events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GUI and events" Watch "GUI and events" New topic
Author

GUI and events

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I am having trouble with a programme that I am writting which is designed to desplay a GUI and then perform tasks depending on the user selecting buttons displayed in the GUI. I have managed to get everything to display but the problem I have is that I can not catch the events ie nothing happens when I select any of the buttons.

The programme is divided into seperate classes so I think this could be due to a problem with the classes not communicating as i am not really sure if the variables that I have are in the correct place. They are supposed to act as handles enabling one class to access information in another.

The alternative is that I have a problem with the way I have setup the actionListeners. I have tried to put a very simple test to see if any buttons are pressed and if so then print a message out to the Crimson Editor screen rather than the GUI but this does not work.

If anyone has any advice on how I can test this further or can spot my errors I would be most grateful.

The classes I have at present are

1. QuitableJFrame is a file I have been supplied with and handles the closing of the GUI

2. TranslationTool sets up the GUI frame

3. Translator class handles the events from the buttons and then "does task". This class does most of the real work

Then there are 3 Panel classes:-

4. TranslationPanel sets up JTextArea's for input output of text

5. InformationPanel sets up Jtext fields and Jlabel fields. One of which is the status of the programme

6. ControlPanel class sets up buttons for the user of the GUI to select

Full code is shown below.

Best wishes
Dianne












marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It appears that your entry point is TranslationTool. Take a look at the main method there and walk through the constructor calls to make sure that everything is getting created the way you intend.

I think one thing you will find is that no instance of Translator (where everything is supposed to get "connected") is ever created.

Also, the way it's written now, the Translator constructor needs a reference to the ControlPanel, but the ControlPanel constructor needs a reference to the Translator. (The ControlPanel constructor is currently getting a null reference.)

For some guidance on how to connect everything, you might want to do some searching on the Model View Controller (MVC) pattern.
[ February 08, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks I will take a look at Model View Controller

Best wishes
Dianne
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I have spent sometime reading about implementing the Model View Controller and have attempted to hook my classes up in a similar fashion.

The TranslationTool is designed as the parent class and as such needs access to all the classes. I have therefore given it instance variables to all the classes and set them within the constructor of the TranslationTool using




The Translator class which listens for the events and does most of the work is passed an instance of the TranslationTool as a parameter of its constructor. This is then copied into a variable inside the constructor.


I think that this class will be able to access all other classes if it uses �parent�



The ControlPanel class public is passed an instance of the Translator class as a parameter of its constructor. This is then copied into a variable inside the constructor.




Can anyone please explain the difference of using � this.� And just simple copying it into a local variable within the constructor???


The TranslationPanel and InformationPanel do not have any link except via the TranslationTool, since it has an instance variable to each class.

I have some get methods which are designed to get at each panel and the use the result to get at the text within the panel.

My main problem is that everything looks beautiful but nothing is functioning. I have setup tests to catch when buttons are pressed and are supposed to print out if a button is hit but nothing is happening.

I would be very grateful for any advice particularly on how to check if events are happening or if there are glaring mistakes in my logic.

I will paste the full code below.

Best wishes
Dianne

TranslationTool sets up the GUI frame

Translator class handles the events from the buttons and then "does task". This class does most of the real work

3 Panel classes:-

TranslationPanel
InformationPanel
ControlPanel

QuitableJFrame is a file I have been supplied with and handles the closing of the GUI



















Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I have moved on to a second problem with my programme while I wait for advice on my original post about events. I though it best to keep to this thread but please advise if it is preferable to start a new thread.

I need to set up FileChoosers to allow me to make use of my menu options in my GUI ie Open, Save and SaveAs.

I am a like confused as to the order and where code should be placed. I have tried to put some very basic code to initiate and use a fileChooser within my Translator class but I am begining to thing that it should be in a seperate class. However if I put it in a seperate class I get back to my origninal problem, ie I do not think that the classes are communicating and at present can not figure out any way of testing to determine what is wrong.

I have pasted the code I am tring to use below. It is in the actionPerformed method because I need to catch the event of selecting a menu option.







However I get a compilation error which is

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" Translator.java
Translator.java:68: showOpenDialog(java.awt.Component) in javax.swing.JFileChooser cannot be applied to (Translator)
returnVal = fileChooser.showOpenDialog(this);
^
This seems to say that the JFileChooser can not be used within the Translator class.

Can someone please let me know if this is correct and if so suggest how I can capture the initial event of selecting the menu item outside of this class. I think it is again a case of using "parent." but since I can not get this to work properly I am not so sure.

Hope someone can help me understand what I am doing wrong.

Best wishes
Dianne Gerrelli

PS Anyone know why I am a Ranch Hand know instead of a Green Horn ?
It is quite flattering but I am afraid I am definitely still learning but would really like to learn and use Java.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42623
    
  65
A few things in no particular order:

The following is code to use a JFileChooser. The javadocs for javax.swing.JFileChooser also have an example of how to use it.


Going back to what Marc said, make sure (e.g. by System.out.println) that the references you pass around as parameters to the constructors are not null.

Regarding "this": It refers to the current object instance. If you write "this.parent", then you're referring to a instance field called "parent", not to a field called "parent" in the current method. In the following code, the left side of the assignment refers to "Object parent", while the right side refers to "TranslationTool parent":


You're now a ranch hand because you posted more than 30 times - it is an automatic promotion, with no checking on your Java progress. Wear it proudly, even if you feel you haven't earned it yet - next time, there will be an exam
[ February 11, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks for the reply I will see if I can make further progress. I will post later today with either more questions or hopefully the functioning GUI.

Best wishes
Dianne
 
Don't get me started about those stupid light bulbs.
 
subject: GUI and events