aspose file tools*
The moose likes Mac OS and the fly likes Swing Problem with Head First Java example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "Swing Problem with Head First Java example" Watch "Swing Problem with Head First Java example" New topic
Author

Swing Problem with Head First Java example

Richard Sandmeyer
Greenhorn

Joined: Jul 07, 2007
Posts: 4
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?

TIA for any help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If anyone else is curious about this, the BeatBox.java example is below...

Source: JAR of code from WickedlySmart.com - Head First Java.
Richard Sandmeyer
Greenhorn

Joined: Jul 07, 2007
Posts: 4
Thanks for the replies.

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.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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 ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I'ld like to get some feedback on my cross-platform solution.

Bear, can you move this thread to the Swing forum?

Kaydell
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Moved as this transends Mac-specific issues.
[ July 10, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing Problem with Head First Java example