aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Automatic Layout cuts off labels - Modified headfirst beatbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Automatic Layout cuts off labels - Modified headfirst beatbox" Watch "Automatic Layout cuts off labels - Modified headfirst beatbox" New topic
Author

Automatic Layout cuts off labels - Modified headfirst beatbox

Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Nov 11, 2005
Posts: 95
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)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Nov 11, 2005
Posts: 95
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Adam, Welcome to JavaRanch!

I will move this to Swing/AWT for you; it's not something you can do yourself.


[Jess in Action][AskingGoodQuestions]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Nov 11, 2005
Posts: 95
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!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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();
}
});
}


bitguru blog
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
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.
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
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
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Nov 11, 2005
Posts: 95
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
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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

Joined: Nov 11, 2005
Posts: 95
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automatic Layout cuts off labels - Modified headfirst beatbox