简体   繁体   中英

Creating custom classes for each java swing component in my application

I'm working on a Java swing application. No big graphics. Just Frames and dialogs with jcombos, jtexts, jlists etc.. I have seen customised jcomboboxes or jtextfields with some custom functionality specific for the application. This each customised class for each type of jcomponent will be used throughout the application. But what i thought is - make a separate class for each component being displayed in application, and handle the functionlity associated with them in their own class... Instead of using one customised class for all the Jcombos in the application, I am thinking of using a seaparate class for separate jcombos... may be i require thousands of classes to display as many number of elements(JComponents).

for example: if in a jdialog i have 5Jcombos, 3 Jtextfields, 2JCheckboxes - I am planning to write 5 custom classes extending JComboBox, 3 custom classes extending JTextFields, 2 custom classes extending JCheckBoxes.

Is this a good thing to do? I dont know about design patterns and stuff. Is this feasible? Have this approach been followed already?

No, it's not a good thing to do.

I have worked on a project (for years) that tried to do exactly what you are suggesting. Thousands of classes will significantly reduce the speed of your application, and will make your code extremely difficult to understand and maintain.

In general, you should never subclass any Swing component, unless you are changing its behavior. If you can do what you want to do using the component's public methods, you definitely should not subclass it. A class (Swing or otherwise) should not be extended just because one wants to make use of it.

I think it is not a good idea to write a class for every use case, but probably would be better to do something generic. I used to develop a Swing application where my tables had some specific styles. So, at that time I decided to use the Decorator pattern (where I added some extra styles, adjustements to the JTable) and everywhere where I needed to use the tables I was using this class. If you don't know the design patterns, probablby would be a good idea to go through them from time to time so you'll get used with them... Also, I would recommend using the basic principles: Low coupling and high cohesion - in a few words: try to make the class not to have many dependencies to other resources which will make your code more maintenable and try to do a specific job in your class: don't mix different scopes in the same class...

This is just my subjective opinion. Wish you good luck!

Yes, like others have already said, do use composition over inheritance whenever possible. However, since you state you are unfamiliar with design patterns, I wonder if you are asking a slightly different question here.

Are you by a chance asking where to put your event listeners? Ie whether to have one monstrous event listener for all JComboBoxes in your dialog, possibly with a switch statement, or have one concrete event listener class per combo box? If this is what you are asking, then the modern pattern is the latter one. And yes, you probably want to make an individual listener class (or a number of listener classes if you are listening to different events) per component - only these are inner classes, best placed in the main component you are designing.

I highly recommend Window Builder that comes with Eclipse, it's WYSIWYG and generate decent OO code, teaches you good design patterns while being customizable to yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM