wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes GUI classes, implementing ActionListener. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI classes, implementing ActionListener." Watch "GUI classes, implementing ActionListener." New topic
Author

GUI classes, implementing ActionListener.

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I feel like i know the little bits but not the overall scheme of things.

I've separated my GUI class to a separate class called gui. I have a class for my objects and a main class to run everything.

How do i get the ActionPerformed method in the GUI class to call the Main Class for action methods?, I've tried setting them public

Rob.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Rob - can you post some code
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
It's reasonable to understand a pure Model-View-Controller (MVC) architecture approach as you experiment with what program design works well for you, but many here will say pure MVC is not the best Java Swing architecture. Even so, I suggest you read through this tutorial to get a basic answer to your question. The tutorial should give you a clear understanding of how to separate the parts of your Java program into the important elements and then program them to work together.

Then, as you "evolve" to the next step, read about the Modified Delegate architecture. Others here can provide simple examples of the Modified Delegate approach (especially if you show them code written to an MVC model).

Good luck.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
That tutorial uses what I think is poor design for Listeners, using ActionCommands. We discuss that regularly on our GUIs forum, where I am moving this discussion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
I have found an example of how I write an ActionListener, with a link to a previous discussion, here.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Thanks for the replies.

I'm using NetBeansIDE which adds the following code when i attach an ActionListener to a component of the frame. My quesiton is how do i invoke methods on the core class which does, say the addition of a sum or some kind of inner function.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
If you are a beginner, you are probably better off not using an IDE like NetBeans, but writing code by hand. What NetBeans has done is create a method with an awkward name, but it is easy enough to understand if you look at it carefully. What you should have to accompany it is something like this:What that means is that when you click on the 2nd menu item, it calls the jMenuItem2ActionPerformed method. So you put inside that method whatever you want to happen whenever anybody clicks the 2nd menu item (or pushes keys like enter while that 2nd menu item has the focus).
It would be similar for your addButton. Replace jMenuItem2 with addButton throughout, and change the name of the method to add. Then you write an add method, which might read like thisSomething like that. Note you do need the additional pair of () around the second + sign.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
... and you can pass the ActionEvent object to the add() method, or not pass it, as appropriate.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Ok i understand that. Now on a newbie non Swing /GUI thread.

I have my main class and my GUI class. How do i call functions on the main class from the GUI class? Or on objects created in main?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Probably not at all.

You want classes which represent the model of your application. Those are best designed without a GUI. Then you call their method from the buttons (Control), and get the results and show them in the GUI (View).
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
That's what i want to do.

Say i declare an object in the main class, how do i then invoke a method on the object from the GUI class?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Give the GUI class a reference to the object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Sorry i don't understand. Say Main creates the class GUI, and a second class "compute" for data manipulation. GUI calls methods in the "compute" class.
By which name does GUI Call compute without creating the instance itself?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
was thinking it might be that.

Many thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI classes, implementing ActionListener.
 
Similar Threads
NetBeans and GUI linking
reference problem within inner class
Gui and Threading
Loading the Client
JTabbedPane Focus problem: