File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes I'm sure I'm doing something very dumb.... why can't I see my JLabel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I Watch "I New topic
Author

I'm sure I'm doing something very dumb.... why can't I see my JLabel?

Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
I'm designing one of those slider games where you push the tiles around to complete a picture. My eventual goal is to be able to have teh user supply a picture which the program then makes a puzzle out of. However as an interim step I was just labeling the tiles with numbers to make sure the game worked. I can't get the JLabel which holds the tile number to dislpay! Sorry to dump mass amounts of code!!! I don't know where the error could be...



This is the panel in which everything goes down:

The challenged JLabel is at line 134.



This is the class for the panels that move.




This is the code to start everything up.





Thanks!!!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You may wish to try to simplify your problem a bit, and often a solution will show itself to you. If not, then you can post your simpler compilable code which would likely be much easier for others to figure out. Best of luck!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I've looked a bit more at your code. What is displayed currently when you run your code? It seems to me that you're holder panels use flow layout, and so the components they hold will try to be at their preferred size. Your MoveablePanels hold a JLabel with a number (currently all hold the same number, 1), and the MoveablePanels shrink to the size of the JLabel they hold which is very small. You try to fix this by calling setSize on the MoveablePanels, but most layouts, including the FlowLayout used by the Holder Panels, don't recognize a component's size property but rather the preferredSize property. You may wish to either set the preferredSize of your MoveablePanels or perhaps better, have your Holder panels use a BorderLayout and add the MoveablePanels to them BorderLayout.CENTER.
Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
Thanks a bunch everyone! I've gotten to to work through a combination of setting the preferredSize and using the validate() command (which I must admit I didn't know existed... how sad).
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Interesting problem that got me to thinking. Here's another solution.

MovePanelMain.java


MovePanelSliderPanel.java


MovePanel.java


MovePanelMouseListener.java
Nick Sharp
Greenhorn

Joined: Jul 14, 2009
Posts: 8
Wow, thanks a lot, that was really interesting. I would not have thought to do it using a multi-dimensional array (that's what it's called, right?) instead of an arraylist of holder panels. That definitely makes some things a lot simpler.

It also occurred to me that part of the reason my code was probably confusing was because I was using netBeans and I let netBeans generate the code for the boring parts of the GUI and I just made the panel that actually had my game. But it completely works now and I'm a happy student!



If you'd like to enlighten me a little more, I am working through one new issue:

I discovered that if you randomly lay down tiles on a game like this, 50% of the generated puzzles will be unsolvable. To get around this, I made the program generate a solved board, then scramble it as a human would, simply moving one tile at a time a couple hundred times (depending on the size of the board).

I thought it would be cool for the user if I could make it display this process happening. I imagine all the tiles shifting around faster than the eye can follow, and I feel it would be a very cool effect. However, no matter what I do, I can't get it to update the view of the board in between each shift. It always generates the entire board, then displays it. It's not a big deal, so I'm not going to worry about posting my code, but are there some rules that need to be followed when i want to update graphics with iterations that are happening extremely quickly? Or is it just some error in my code?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm sure I'm doing something very dumb.... why can't I see my JLabel?