This question will only make sense to those who are familiar with the book "Head First Java" by Sierra and Bates. I'm quite new to Java (though not to C++ and some older languages), and I have found that working my way through the book seems to be a good way to learn it. I've been running the code examples on Mac OS X 10.4.10 on a G4 Powerbook using Java 1.5.0_07. Everything went well until I got to the BeatBox example in Chapter 13 (Code Kitchen).
The BeatBox is supposed to display a GUI window (frame) consisting of three sections, west is a box with a vertically-stacked list of musical instrument names, center is a 16 by 16 array of checkboxes, and east is a box of four vertically-stacked buttons. Problem is that when I compile and run it, the center panel overwrites the west panel section so that there is blank space where the column of names should be. This seems to be a problem with the Mac implementation of Swing as I ported the *.java file to a PC (Windows XP Home, Java 6) and compiled and ran it there with no problem. BTW, I had this problem with the downloaded BeatBox source in the Code Kitchens jar; so this problem isn't a typing error on my part.
My questions are: Has anyone else encountered this problem and is there a simple fix? Is it really (as I suspect) a problem with Swing on Mac? If so, has a fix to Swing on Mac been put out yet?
Originally posted by Richard Sandmeyer: ...Has anyone else encountered this problem...
Welcome to JavaRanch!
I'm also running Java 1.5.0_07 on OS X 10.4.10 (on a G5 iMac). Unfortunately, the WEST Labels do appear to be "covered" by the CENTER JPanel. I've found that you can (usually) get these to appear by resizing the JFrame (dragging the lower right corner). But they disappear again if you maximize the frame.
I've also noticed that if the (awt) Labels are replaced by (swing) JLabels (line 58), then they appear consistently -- although they're not aligned with the checkboxes in the center.
I don't know enough about Swing to explain why this is happening or to suggest a fix. But since JLabels seem better about repainting (or whatever is the issue), I would probably start by looking for a way to align JLabels with the checkboxes so that these can be used in place of awt Labels.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
I just tried two of your suggestions: 1. enlarging the window by dragging the lower left corner further to the left and 2. making the labels visible (but misaligned) by changing to JLabel in the code. Both did as you mentioned, of course. Guess the simplest solution may be to use JLabel and experiment with ways to get the spacing of the instrument names to line up with the checkboxes; perhaps something as simple as picking a larger font for the instrument names would space them more neatly, or perhaps I'll have to find some other trick.
At least now I know that it isn't something wrong with my particular machine or copy of Java. It looks like "write once, run anywhere" may need to be qualified by adding "but don't expect quite the same results everywhere" at least when it comes to Swing.
I think that the following code is an improvement to the original BeatBox program.
The main point is to use a single LayoutManager and a single Container, the content pane, to simplify things.
Also, to set the size of the window, the original version of BeatBox called setBounds() and pack() on the jFrame. The setBounds() was a no-op since it was immediately followed by pack() which sets the window size according to the layout managers and the containers.
One thing I don't understand is that BeatBox seems to handle multiple threads correctly. I've read that you're supposed to call SwingUtilities.invokeLater() to start your windows. BeatBox calls setVisible() directly yet seems to handle a second thread that produces the sound. Can somebody explain this?
[ July 10, 2007: Message edited by: Kaydell Leavitt ]