• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing Problem with Head First Java example

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'ld like to get some feedback on my cross-platform solution.

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

Kaydell
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved as requested.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic