File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classes" Watch "Classes" New topic
Author

Classes

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I am producing a very large program and am concerned about the size of my classes. In particular my Program involves user-interfaces and this after certain buttons are pressed a new user interface is created providing the user with more options. Should these different user interfaces be kept in different classes and then invoked within each other when certain things happen? At the moment they are all within one class which is becomming very big and I am concerned that this may also increase memoey usage and make the program slower??? If the user interfaces could be separated into different classes could you please provide an example of this happening, ie, how would you invoke one class from another?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Should these different user interfaces be kept in different classes and then invoked within each other when certain things happen?
That might be a good idea, as it's often much easier to understand and maintain a few interworking smaller, well designed components with clear and limited responsibilities, than one big messy component that is trying to do everything.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Okay, But could you possibly provide an example of how i would invoke a method for example from another class?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Originally posted by Naf Rash:
Okay, But could you possibly provide an example of how i would invoke a method for example from another class?

This is a very simple example

To call a method in another class, you first create an instance of that class, then you can call it's methods.
Search on google for infomation on the MVC pattern. I personally think this pattern should always be used in GUI design.
HTH
Nigel
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
I have a class file which constructs a page with several buttons on it. The page will redirect to a new page depending on which button is selected.
My Button choices are "Save, Done, Cancel or Preview".
Here is the post method from this class file:

If the Preview button is hit, the following method is called from the post method:

The redirect causes another page to be drawn, the preview page, which has it's own separate class file.
Not sure if this is the kind of info you were looking for, hope it helps.
[ February 24, 2004: Message edited by: Kim Kantola ]
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
I should have mentioned that my post refers to servlet code,
Nigel's looks a bit more easy to follow if you are not working on servlet code.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Naf Rash - you might scroll down to the UML and OO forum where we talk about design a lot. Look for existing messages about MVC or Model View Controller. MVC ideas apply well to Swing programs and have been adapted to web applications, too. They may give you some good ideas about how to structure a large app with many windows or pages. See ya there!
(I will repeat tho, look for existing messages first. We've talked about MVC quite a few times so there should be something good there.)


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I am not well versed with MVC, but I thought I'd add my two cents. When I design a GUI in Java, I generally have several classes, even for the simplest program.
First, I extend Frame or JFrame, depending on whether I'm using AWT or Swing.

Then I also extend Panel or JPanel:

Typically the constructor of MyFrame creates an instance of MyPanel for its use and the MyPanel constructor creates the buttons and other controls necessary for display:

I have found that this scheme works well for me. For one thing, it is simple to reuse MyPanel in an applet by creating another class that extends Applet then creates an instance of MyPanel. This makes it easy to port a desktop application to an applet on the Web.
Also, it doesn't take much effort to add more complex elements to the program. You could create two more classes that extend JFrame and JPanel respectively. This new frame can add an instance of the new panel class. Then MyFrame can create an instance of this second frame and open it.
I don't know if this will work well for the project you are working on, but I thought I'd at least suggest it. Perhaps some of it will help give you ideas about what can work for you.
Keep Coding! (TM)
Layne


Java API Documentation
The Java Tutorial
 
Don't get me started about those stupid light bulbs.
 
subject: Classes