• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Automatic Layout cuts off labels - Modified headfirst beatbox

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this question is in the right forum. Apologies if it too easy
I am working through the headfirst java book. It is my first experience with programming since I made a role-playing game on my ti-82.

In order to learn more, I have tried to tweak and extend all the code examples in the book. Same goes for BeatBox. For some reason, though, the beatbox layout isn't coming out right. Fundamentally, the beatbox has three graphics regions:

The background is then dumped into the frame:

nameBox isn't displaying properly, though. The overall window size seems to be too small, and none of the instrument names shows:


If I manually resize the window at runtime, the instrument names appear as expected.

I tried increasing theFrame.setBounds to (50, 50, 1200, 1200) [I have a big monitor]
That made no difference - none - nothing changed.

What am I overlooking?



full code:





note: all the edits are because I originally typed it up with [java] [/java] tags like they have over at artima: We ought to get some of them there java tags over here, coz they sure do look purty.

[ December 08, 2005: Message edited by: Adam Price ]

[ December 08, 2005: Message edited by: Adam Price ]
[ December 08, 2005: Message edited by: Adam Price ]
 
Marshal
Posts: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instrument names showed when I tried it, and the GUI filled my screen (I only use 1280*1024). There seemed to be a two-dimensional array of checkboxes, which I don't think you intended however.

I am not sure I have been that helpful. have I?
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
The instrument names showed when I tried it, and the GUI filled my screen (I only use 1280*1024). There seemed to be a two-dimensional array of checkboxes, which I don't think you intended however.


There should be a two dimensional array. The columns represent beats, the rows are for instruments, so you can have a boom-chuck-a with a cymbal crash and a handclap occurring simultaneously with the chuck. . .or something like that. It is actually kind of fun to get a beat going and turn it up loud - home-brew techno.



I am not sure I have been that helpful. have I?

Unfortunately, no. You say the instrument names showed with the default window size? What platform are you on? (I am compiling out of eclipse running on Macox10.4)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This belongs in the Swing forum. The behavior is expected if the window is too small for all of it's contents. Off the top of my head I'm not aware if setBounds() will trigger layout validation. If it doesn't, you need to call validate() yourself to cause the frame to have it's components arranged after you change it's size through setBounds().
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:
This belongs in the Swing forum.



Can I move it myself? Or can a moderator do it for me?

Also posted by Ken Blair:

The behavior is expected if the window is too small for all of it's contents. Off the top of my head I'm not aware if setBounds() will trigger layout validation. If it doesn't, you need to call validate() yourself to cause the frame to have it's components arranged after you change it's size through setBounds().



Hmm..I added theFrame.validate. It didn't work. Rats.
I tried forcing nameBox to be visible (nameBox.setVisible(true) and I tried re-validating nameBox as well. Neither attempt worked.

I did notice, though, that it may not be a size issue. If I resize the window so it is smaller, that will also cause the instrument names to appear. In fact, if I minimize then maximize the window or dock the window and undock it, the names will appear. Moving the window does not work. How do I get this thread moved to swing?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adam, Welcome to JavaRanch!

I will move this to Swing/AWT for you; it's not something you can do yourself.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Price:


Hmm..I added theFrame.validate. It didn't work. Rats.
I tried forcing nameBox to be visible (nameBox.setVisible(true) and I tried re-validating nameBox as well. Neither attempt worked.

I did notice, though, that it may not be a size issue. If I resize the window so it is smaller, that will also cause the instrument names to appear. In fact, if I minimize then maximize the window or dock the window and undock it, the names will appear. Moving the window does not work. How do I get this thread moved to swing?



That's (probably) because all of those cause layout to occur. If you change the size of the frame but don't force the layout to be recalculated then nothing is going to change. All of the things you've mentioned doing at runtime as a user cause this to happen. Programmatically changing the size doesn't.

Try validatnig the content pane, not the frame. Or calling validateTree().
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


That's (probably) because all of those cause layout to occur. If you change the size of the frame but don't force the layout to be recalculated then nothing is going to change. All of the things you've mentioned doing at runtime as a user cause this to happen. Programmatically changing the size doesn't.

Try validating the content pane, not the frame. Or calling validateTree().




Arggh...I have tried every combination of
to no avail. I removed the explicit setBounds. I removed the border. Arrghhhh!

I tried and , but was told
in both cases. Arrrgghh!
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code works as-is on my machine, though
the window pops up way to huge. (It looks better
with theFrame.pack() over theFrame.setSize().)

It's hard to know where things are going wrong
for you if I can't reproduce it, but does it
help any if you use GridLayout for nameBox?

//Box nameBox = new Box(BoxLayout.Y_AXIS);
JPanel nameBox = new JPanel(new GridLayout(INSTRUMENT_COUNT, 0, 2, 1));

It shouldn't but you never know. Something
else you can try is replacing main() with:

public static void main(String[] args) {
final BeatBox bb = new BeatBox();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bb.buildGUI();
}
});
}
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:
Your code works as-is on my machine, though
the window pops up way to huge. (It looks better
with theFrame.pack() over theFrame.setSize().)

It's hard to know where things are going wrong
for you if I can't reproduce it, but does it
help any if you use GridLayout for nameBox?

//Box nameBox = new Box(BoxLayout.Y_AXIS);
JPanel nameBox = new JPanel(new GridLayout(INSTRUMENT_COUNT, 0, 2, 1));

It shouldn't but you never know. Something
else you can try is replacing main() with:

public static void main(String[] args) {
final BeatBox bb = new BeatBox();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bb.buildGUI();
}
});
}



Bizarre - neither of these has any effect. The window appears, then the namebox appears, then the grid and the buttons appear, then there is a second delay, then the namebox disappears again. I resize, then the namebox reappears and stays visible.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code doesn't work at all on my system, but it does compile properly.. hmmmm..

Anyway, have you tried the following?

Looking back through some of my old code, that was how I had to sometimes get guis to display properly.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Price:
Bizarre - neither of these has any effect.



Not too bizarre. The code seems to be working
for most of us, so we're just guessing out of
the blue what might help you.

That it works ok for you after any resize of
the frame is interesting. It almost means it
can't be anything too simple.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the unmodified headfirst beatbox work
ok for you? If so, what are the differences? I do
not have access to the headfirst book.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Price:

to no avail. I removed the explicit setBounds. I removed the border. Arrghhhh!

I tried and , but was told
in both cases. Arrrgghh!



That's because you shouldn't use validateTree(), it was a stupid thing for me to say. Really stupid in fact seeing as validate() calls validateTree() in the first place. This example works fine for me when I compile it. Have you tried just copying what you posted here over to a new file? Sometimes we get so caught up trying to solve a problem we inadvertently introduce a new one without realizing it that masks the first one being solved.

If it still happens I'd start to wonder about your OS, JDK and L&F.
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:
Does the unmodified headfirst beatbox work
ok for you? If so, what are the differences? I do
not have access to the headfirst book.



No - the differences were not related to display. I just introduced a few global variables so I can extend the program easier later and added the "clear checkboxes" and "reset beat to 120 buttons" I only mentioned it so that some other headfirster wouldn't find my code later, try to cut and paste to avoid typing, and get confused.

Originally posted by Ken Blair
[QB]That's because you shouldn't use validateTree(), it was a stupid thing for me to say. Really stupid in fact seeing as validate() calls validateTree() in the first place. This example works fine for me when I compile it. Have you tried just copying what you posted here over to a new file? Sometimes we get so caught up trying to solve a problem we inadvertently introduce a new one without realizing it that masks the first one being solved.

If it still happens I'd start to wonder about your OS, JDK and L&F.[qb]



OS: MacOSX 10.4.3
L&F: Aqua (default)
JDK: --funny...I told eclipse in the preferences to use 1.5, but when I scan the configuration dump, it appears to be using 1.4.2 - I am not sure about that though. When I try to run from the command line, though, I get noClassDef errors and it won't launch.


Thanks to everyone who is trying to help on this!
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well we should be able to determine if it is a problem with the LnF. Do you still have a problem if you use this main() method?

public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
new BeatBox().buildGUI();
}
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:
Well we should be able to determine if it is a problem with the LnF. Do you still have a problem if you use this main() method?

public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
new BeatBox().buildGUI();
}



Same as before, but with square boxes. I also tried every rearrangement I could think of for the background.add(...) calls and tried throwing in a theFrame.setSize(theFrame.getSize) at the end of buildGUI(). No dice.
[ December 12, 2005: Message edited by: Adam Price ]
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic