0

I'll be cranking out my first serious GUI in Java here over the next few weeks / months. This will be my first project in Java where I'll hold myself to a very high standard for coding practices and results, so I expect the learning process will take some time.

I'd say I'm not quite a beginner with Java, but more of an intermediate user - getting used to the API, still awkward on a few keywords. I'm comfortable with OOP, mainly through PHP development.

Are there any tips people can think of to help accelerate the learning curve, i.e. "don't do THAT" or "I wish someone had told me this" type of things.

The first one I came across in a few other questions was a null layout manager - apparently, that's shoddy programming and leads to trouble down the line. So, that helps me right away, probably saved a week and a few rewrites right there.

Another was use NetBeans to move around in Swing...but I can't elaborate on that one yet, needs some research.

Are there any other strong recommendations out there?

cHao
  • 84,970
  • 20
  • 145
  • 172
Ben
  • 54,723
  • 49
  • 178
  • 224

7 Answers7

4

Are there any other strong recommendations out there?

Also on the general subject of layouts, don't spend time trying to to find the 'right' layout, when a combination of nested layouts will achieve the effect. See the Nested Layout Example for sample code.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The good thing about specialised layouts for different panels is that they have really better support in GUI designers. Something like MigLayout in a GUI designer just lets you edit the constraints as a string because it is too generic (and will tell you to edit the source file if you enter something invalid!). Not very user friendly. – Chris Dennett Jul 20 '11 at 11:04
3

If you're using Eclipse, you can try WindowBuilder Pro, which is a GUI designer now open source and part of the Eclipse project. The code it generates is extremely readable. Honestly, with this tool, I wouldn't do UI development by hand any longer. It does Swing and SWT, amongst other things. Also has support for many powerful layout managers (including MigLayout!).

enter image description here

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
3

You might want to check out the following:

JGoodies has the famous FormLayout and other stuff: http://www.jgoodies.com/

SAF JSR-296 implementation fork: http://kenai.com/projects/bsaf/pages/Home

Apache Commons, especially lang, if you don't use these already, you're on the wrong track: http://commons.apache.org/

Not sure about SwingX right now, but it seems to be worth looking at as well: Does anyone use the "swingx" extensions to Swing?

Java practices, general good stuff: http://www.javapractices.com

You should definitely look into using existing frameworks and tools. It helps to read a lot up front to get a picture instead of delving into writing much yourself. There's almost anything that exists except a library for common translations in desktop apps, e.g. for common actions (menu items).

Edit: Ah and don't forget the SwingWorker...

Community
  • 1
  • 1
Kawu
  • 13,647
  • 34
  • 123
  • 195
  • Excellent @Kawu, love it: "if you don't use these already, you're on the wrong track" lol. Thanks, will probably accept but I'll give it a day or so and see what else pans out. – Ben Jul 20 '11 at 00:35
  • Oh and don't expect too much from Swing's layout managers, they're known not to provide what you need for complex layouts. Hence you should look for custom layout managers, which I think are present in SwingX. You also might want to prefix GUI elements with their class, pn for JPanel, tf for JTextField etc. If you don't want to do that you will end up postfixing everything, e.g. loginPanel instead of pnLogin, which isn't any better. Note, this is not Hungarian notation, just a simple prefixing scheme which allows changing GUI element classes quickly without having to come up with new var names – Kawu Jul 20 '11 at 01:21
  • I've seen that prefixing (occasionally postfixing) pop up here and there, and that's a great explanation why it's useful. – Ben Jul 20 '11 at 02:00
2

Another was use NetBeans to move around in Swing...

I'm not a fan of using an IDE to generate because well, now you are tied to the IDE and you can't move from IDE to IDE.

You are forced to follow the structure of the code they generate. I like to be in full control of the code.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm right with you - I much prefer to generate it by myself. However, I seem to recall that piece of advice mentioning that the person learned a lot faster than by using Swing from the ground up... – Ben Jul 20 '11 at 03:39
  • Note that does not apply if you don't use the built-in 'GUI designer' (and watch a few other things). (Perhaps that was obvious to some from your comment, I just felt it was worth clarifying.) I worked with a team on a rich client that used Ant to build the project. We were free to use whatever IDE we liked (and would not cost the company). About half the developers used Eclipse, while the other half used Netbeans. No problems encountered. – Andrew Thompson Jul 20 '11 at 11:20
1

Not specific to UI programming, but I strongly recommend you read Effective Java.

hoipolloi
  • 7,984
  • 2
  • 27
  • 28
1

Swing is just a GUI API (and anything related to GUI programming e.g. MVC will be applicable and that topic is well covered in CS literature).

Simply note the following caveat (which actually applies to most GUI systems out there as it hugely simplifies the task of writing such libraries):

It is an 'active sub-system' that has its own dedicated thread. Delve into the threading issues of swing, how to use SwingWorker, etc.

As far as tools, last I checked, NetBeans was the king of the hill in Swing land (but that was pre-ora). If you dislike the tool, do yourself a favor and at least create an example app and observe how the IDE organizes your app for you. There is a boat load of collective knowledge right there.

alphazero
  • 27,094
  • 3
  • 30
  • 26
  • Great advice, thanks! I heard about the downfalls of single-thread swing but I have yet to look into that subject also. I'll get delving! – Ben Jul 20 '11 at 01:57
1

I like GridBagLayout a lot. Flow and Box Layout are also good to know, and group layout if you have Java 6. The NetBeans IDE has a really intuitive GUI manager, but the way it codes things is a bit strange. The null layout of course has the most flexibility but is not good practice like you said, and besides with these layouts, I doubt you would need any more functionality except in rare cases.

joseph
  • 767
  • 3
  • 8
  • 16