Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swing(gui) code - how to keep it organized?

 
Jon Park
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was wondering how you guys go about keeping swing code manageable.
I mean, my client-side gui contains long long lines of code for many components(menuBar, search fields, table, buttons, etc.) and most of the code is in the constructor. I have divided the code into different groups and commented them, but that's about it:
//menubar
code
//search panel
code
//table panel
code
..so on

Would this be neat enough? or should I put each group into different methods and call the methods one by one in the constructor(is this necessary, though?)?
How are you guys doing this?

Thanks

Jon

ps- How do you make it so that the exit button on top right-hand corner of the window behaves like a JButton(trigger actionEvents that can be caught by "actionPerformed()" method)?
[ May 08, 2008: Message edited by: Jon Park ]
 
Richard Roehl
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I pondered that myself and here are my thoughts...

I am using NetBeans 6.0 which allows you to build GUIs via a GUI and then it re-generates the code every time you change something. The generated code gets put in one big method called initComponents() and then that is called that from the constructor. So if you respect NetBeans' opinion then you should not put component builder code in the constructor but it's okay to have it all in a single long method.

Regarding documentation... Because NetBeans auomatically generates code you can't easily just jump in their and add comments. I'm probably going to argue that since it was generated automagically and that I used appropriatly named variables to reference the GUI parts, any additional documentation would violate their their mandate that states, "do not provide comments that do not add to the comprehensibility of the code."

So I would agree that you should keep it all in one method and not add anything more than the one line headers to logically break up the sections.

Richard
 
Richard Roehl
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the JFrame you want to ignore the close button, call:

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

I take it one step furthur and re-map the closing of the window to be equivalent to them selecting "Exit" from the menu bar with this code:

 
Jon Park
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Richard!

You cleared up everything for me.
I wonder why it is more preferable to have code in a separate method, not the constructor, though. Thanks again!
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,

I actually developed a framework, which consisted of a number of different classes for the different areas of the screen, I think it would be difficult to justify that your design would easily support future enhancement if it was contained within one large method, not to mention that it's not very OO.
Your user interface should be designed with the expectation of future functionality enhancements, and it should establish a framework that will support this with minimal disruption to the users when this occurs.

Richard - I also wouldn't recommend using Netbeans to generate your GUI code, I'm not sure about your assignment but mine had the following statement in the deliverables section
You are permitted to use any IDE tool you choose, but you must not submit any code that is not your own work.

This requirement is must therefore I think you could potentially fail the assignment if you include generated code in your submission.

Regards,
Jason
[ May 09, 2008: Message edited by: Jason Moors ]
 
Jon Park
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

What exactly is "framework" in this context? would a single class with methods to add components(buttons, textfields) to a panel be considered a framework? Thanks.
 
Robert James Liguori
Author
Ranch Hand
Posts: 553
5
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans IDE is cool. Today, when GUI builders can be used to expedite the development of user interfaces, they should be.

However, for this assignment, my specification specifically states, "You are permitted to use any IDE tool you choose, but you must not submit any code that is not your own work."

I see this requirement as saying that you must script your own code. What the NetBeans IDE does for GUI building, is it generates the code for you. So is this code truely, "your own work". My interpretation is that it is not.

My opinion is that you should hard code the GUI from scratch for this assignment as this approach will help you in the general understanding of the Swing API. Note: If you want to use the Netbeans IDE on the side for further understanding, why not... just don't directly edit those files for submission.

There is a way to determine if it is allowable though... call-out to Java Ranchers... has anyone submitted and passed the SCJD with the GUI built using the NetBeans IDE?
[ May 11, 2008: Message edited by: Robert Liguori ]
 
Richard Roehl
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a little discussion of "what's the difference between automated code and hand code?" here link.

Okay, I've worked on this over the last couple of weeks, finalized my GUI and then converted my code into what I feel comfortable submitting.

Here is what I did:

1) NetBeans creates external ".form" files along side your java code when you use it for GUI creation. That's obviously not "my own work" and it's not useful outside of NetBeans so I deleted them.
2) It puts comment tags into the source that tells the editor not to allow user edits on the auto-generated code. I got rid of all of those.
3) It likes to use fully qualified package references (e.g. new javax.swing.JPanel()) instead of imports. I stripped out the fully qualified references and used import statements.
4) It puts class scoped component references at the bottom of the source file and I like them at the top, so I moved them there.

At that point the only "evidence" that I used a code generator was in the initComponents() method. So I went through the classes one by one and did the following:


- I found that NetBeans likes to use the default contructor and then modifies the component one method at a time. For example instead of saying 'new JButton("my button"') it would generate 'b = new JButton(); b.setText("my button");' So I consolidated everywhere I could.
- For real small classes, like my online help that simply displays HTML, I got rid of the initComponents() method and put the GUI code directly in the class initializer.
- The order in which components are initialized and added appeared random, so I rearranged the code and commented it so that someone reading it could follow along in a left/right and top/down order initialization path.
- Where I used GridBag I noted in the comments the number of rows and number of columns and then ordered the initComponents code by rows and columns.

If your sitting there thinking, "He just re-ordered code that his IDE generated and it's not his own work" then so be it. I came up with the overal GUI design, I tweaked the screens so that they looked good, I put the logic in the methods that were called when the GUI was "clicked", I simplified the generated code where reasonable and I rearranged and documented the generated code so that it would "be readily understood by junior programmers". That's exactly what I would do in the real world.


So, I guess in the end I'm arguing that I truly don't believe you have to code this project entirely from scratch and you shouldn't be afraid to use NetBeans or any other IDE to help you achieve your goal.

Richard
 
Ray Tsia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just personal opinion.
You will learn more if you hard code everything by yourself. I knew how to create GUI from netbean but I don't know how to start from scratch all by myself. It's one of thing I need to learn before I take the exam.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will learn more if you hard code everything by yourself.


That's the spirit! What I did, I created all windows with a plugin for Eclipse, then developed everything else. I'm totally done with everything now (documentation, code, choices justification, etc), and now I am rewriting the code of each window by myself. I did this because I was too ancious to write the cool stuff but honestly, I don't want a certification that was not 100% done by myself. So, even if a plugin or tool can generate a code that can never be detected as been generated by a tool, I don't want it. I'll feel a whole lot more satisfied if I do everything.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic