473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

GUI Classes -- How Big? A Question of Style...

I think a lot of this is definately a question of personal programming
style, but I'm new to Java and would like to hear a few opinions.

I'm writing a control panel for an application that runs separately. The
control panel is basically (almost) fully self contained. It consists of a
tabbed pane with 5 different tabs. Each tab has a number of different
controls -- basically all the "commonly used" controls (buttons, lists,
comboboxes, text input...). For a number of components it is easiest for
me to use anonymous or other inside classes. I'm also using private
classes (each tab pane is created by a private class in the same file as
the main GUI class).

So for a project like this, do most people write the code for the entire
window as 1 class, or a separate class for each panel? Is there any kind
of guideline about when a class gets too big? It seems to me any class
creating a window and adding controls will be rather large, since so many
things are done for each component (specifying min, max & preferred size,
adding listeners, specifying list objects, etc), so I would think almost
any class that contains a window would be big anyway.

So how would other people determine what goes in one class and in another,
or if they use private classes in a case like this?

Thanks!

Hal
Jul 17 '05 #1
5 2950
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:Js87b.293235$Oz4.81794@rwcrnsc54...
I think a lot of this is definately a question of personal programming
style, but I'm new to Java and would like to hear a few opinions.

I'm writing a control panel for an application that runs separately. The
control panel is basically (almost) fully self contained. It consists of a tabbed pane with 5 different tabs. Each tab has a number of different
controls -- basically all the "commonly used" controls (buttons, lists,
comboboxes, text input...). For a number of components it is easiest for
me to use anonymous or other inside classes. I'm also using private
classes (each tab pane is created by a private class in the same file as
the main GUI class).

So for a project like this, do most people write the code for the entire
window as 1 class, or a separate class for each panel? Is there any kind
of guideline about when a class gets too big? It seems to me any class
creating a window and adding controls will be rather large, since so many
things are done for each component (specifying min, max & preferred size,
adding listeners, specifying list objects, etc), so I would think almost
any class that contains a window would be big anyway.

So how would other people determine what goes in one class and in another,
or if they use private classes in a case like this?

Thanks!

Hal


I would probably use a class for the "parent" window with the tabbed pane
and use a separate class for each of the 5 options. These will share
characteristics so I would probably use a common superclass for these 5 to
avoid retyping the same stuff 5 times. I don't know of any particular
guidelines, but I seldom write a class larger than 500 lines. I also tend to
move actionlisteners into a separate class to avoid mess. I would also
recommend using private classes sparingly. I find it much easier to deal
with them separately.

John
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003
Jul 17 '05 #2
I have done this sort of thing only once and I use
a separate class for each tab. I like to keep my files
relatively small too, but the guy I work with keeps
making huge files that go on forever.

"John Court" <-> wrote in message
news:3f***********************@lovejoy.zen.co.uk.. .
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:Js87b.293235$Oz4.81794@rwcrnsc54...
I think a lot of this is definately a question of personal programming
style, but I'm new to Java and would like to hear a few opinions.

I'm writing a control panel for an application that runs separately. The control panel is basically (almost) fully self contained. It consists of
a
tabbed pane with 5 different tabs. Each tab has a number of different
controls -- basically all the "commonly used" controls (buttons, lists,
comboboxes, text input...). For a number of components it is easiest
for me to use anonymous or other inside classes. I'm also using private
classes (each tab pane is created by a private class in the same file as
the main GUI class).

So for a project like this, do most people write the code for the entire
window as 1 class, or a separate class for each panel? Is there any kind of guideline about when a class gets too big? It seems to me any class
creating a window and adding controls will be rather large, since so many things are done for each component (specifying min, max & preferred size, adding listeners, specifying list objects, etc), so I would think almost
any class that contains a window would be big anyway.

So how would other people determine what goes in one class and in another, or if they use private classes in a case like this?

Thanks!

Hal


I would probably use a class for the "parent" window with the tabbed pane
and use a separate class for each of the 5 options. These will share
characteristics so I would probably use a common superclass for these 5 to
avoid retyping the same stuff 5 times. I don't know of any particular
guidelines, but I seldom write a class larger than 500 lines. I also tend

to move actionlisteners into a separate class to avoid mess. I would also
recommend using private classes sparingly. I find it much easier to deal
with them separately.

John
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003

Jul 17 '05 #3
"John Court" <-> wrote in message
I would probably use a class for the "parent" window with the tabbed pane
and use a separate class for each of the 5 options. These will share
characteristics so I would probably use a common superclass for these 5 to
avoid retyping the same stuff 5 times. I don't know of any particular
guidelines, but I seldom write a class larger than 500 lines. I also tend to
move actionlisteners into a separate class to avoid mess. I would also
recommend using private classes sparingly. I find it much easier to deal
with them separately.

John


That's an interesting approach. I wonder, if you have a separate
class for your action listener, and it needs to reference other
information on the UI, how do you pass along that information?

For a contrived example, you have a textbox for text, a textfield for
a filename, and a "save" button. So the actionListener for the button
is in a separate class. That's fine... but then you have to set
fields within the action listener class so that it can access the
textbox and textfield, or some other similar overhead (not that that's
a whole lot of overhead).

My approach is either that it's an independent operation, in which
case I might extend JButton with a class that implements
actionListener, or when everything is somehow related, I have my GUI
class implement the actionListener (especially in this case - where
there's only one thing listening anyway). Now, the GUI class, in this
case, might just be another compound component - one of the tab panes,
for example. I have some programs that do database work and I have a
reusable component to display the records in a JTable with options to
refresh the display or remove a record - I use this in several
programs. It's simple enough that (with only two buttons), that I
implemented the actionListener within that class, and just check to
see which button was pressed.

I guess what I'm saying is that I wouldn't always do it the same way,
I'd have to know more about the context of the program. I like to
think of things as either being related (in which case I might put the
actionListener within the GUI class) or as being independent (in which
case I might put it in a separate class). But if I were putting it in
a separate class, I'd just implement my own component - extending a
component and implementing an actionListener. Then that makes for
reusable components.

For this example, the five tab panes, I'd have to know more, but I'd
probably at least put each pane in a separate class, and if they are
simple enough I'd implmenet the actionListener within the class. Just
my $0.02.
Jul 17 '05 #4
Hal,

Large classes are very complicated, and hard to understand. Primarily
because there is a lot of code and it's doing lots of things.
Further, everything in your program has access to everything else.
This sort of design makes it hard to change things without have other
dependancies that must also change. Smaller classes do less things,
and are easier to understand and maintain. If there's not a lot of
code there then it can't be doing to much.

Your question is a very good question to ask. If you look at all the
Swing books out there, they're more concerned with telling you the API
than best practices or how to make the API easy to use. Very few
people seemed to know how to structure Swing apps so that it's easy to
change and modify them.

I would recomend you seperate your classes in logical units.
Everything dealing with X in one "place", and everything dealing with
Y in another. In your program X and Y might be subclasses of panels
or classes that contain the components in 1 of the 5 tabs. When I say
place I mean a class or a set of related classes. Now what glues X
and Y together, so that it creates a complete program, should be the
responsiblity of another class. Generally, this is called the
controller in MVC terms. He should be in charge of creating instances
of X, Y, and maybe some high level actions. In your program he'd
probably create the tabs and the JFrame. He should also control the
model for your program. Then he can hand the right parts of the model
to each view component or control access to it. Should he be adding
listeners on the sub panels or controls within X and Y? Probably not.
Let the X and Y be responsible for responding to changes in their UI,
and have them call a method on the controller in order to accomplish
the business logic and manipulate the model. Seperating these
concerns from each other makes it esaier to modify the internals of X
or Y without impacting any other part of your program.

It doesn't sound like your program is too complicated, but it's good
that you're thinking about these sorts of issues. I have been doing
Swing for 4 years now, and I learned all of this the hard way.

good luck
charlie

Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<Js87b.293235$Oz4.81794@rwcrnsc54>...
I think a lot of this is definately a question of personal programming
style, but I'm new to Java and would like to hear a few opinions.

I'm writing a control panel for an application that runs separately. The
control panel is basically (almost) fully self contained. It consists of a
tabbed pane with 5 different tabs. Each tab has a number of different
controls -- basically all the "commonly used" controls (buttons, lists,
comboboxes, text input...). For a number of components it is easiest for
me to use anonymous or other inside classes. I'm also using private
classes (each tab pane is created by a private class in the same file as
the main GUI class).

So for a project like this, do most people write the code for the entire
window as 1 class, or a separate class for each panel? Is there any kind
of guideline about when a class gets too big? It seems to me any class
creating a window and adding controls will be rather large, since so many
things are done for each component (specifying min, max & preferred size,
adding listeners, specifying list objects, etc), so I would think almost
any class that contains a window would be big anyway.

So how would other people determine what goes in one class and in another,
or if they use private classes in a case like this?

Thanks!

Hal

Jul 17 '05 #5
Thanks to all those who responded (I can't respond to the original now, it's
off my server -- I've been off line and on Gilligan's Island -- no light,
no power, no 'lectricity, just like Robinson Crusoe -- thanks to Isabel and
9 days without power!). There are some really helpful points and answers,
not just on this response, but in the other branch of this thread.

I am almost completely self-taught (I took a few courses back in the early
80s, but that was mainly FORTRAN and Assembler), so I've been picking up
things like structured programming on my own and it is really helpful to
hear other people talk about how they do things.

Thanks for the comments!

Hal
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
12
by: Vaibhav | last post by:
I recently heard about 'new-style classes'. I am very sorry if this sounds like a newbie question, but what are they? I checked the Python Manual but did not find anything conclusive. Could someone...
3
by: Kalle Anke | last post by:
I'm confused by the concepts of old-style vs new-style classes, I've read most of the documents I found about this but it doesn't "click". Probably because I wasn't around before 2.2. Anyway,...
5
by: venk | last post by:
Hi, can some one properly explain the differences between class types and classic classes? ... Still face problems in identifying what is what.
1
by: ankit | last post by:
Hello, Please put some light on, What are new style classes and classic style classes in python. The basic differences in them. And How can I decide to choose one.
4
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r)...
7
by: wolfing1 | last post by:
Is there a way to see what CSS classes in a page are not defined? (like if I want to find mispelled class names, etc)
11
by: allendowney | last post by:
Hi All, I am working on a revised edition of How To Think Like a Computer Scientist, which is going to be called Think Python. It will be published by Cambridge University Press, but there...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.