Java - Is it possible to put a JLayeredPane inside JScrollPane? - java

when I try putting my JLayeredPane inside a JScrollPane I get a blank window (with white background) instead of the content I am trying to render (it could be an image, a button, a canvas). Does anyone know of a problem with layout managers that might cause it? Is it possible?
EDIT:
thanks to camickr help, I can now put a JLayeredPane inside a JScrollPane, though now I am facing a different problem:
I am using a very large image and I am trying to put it inside my JLayeredPane that's inside the JScrollPane. From some reason when I use this large image (I am not receiving a heap overflow exception) I get this blank (white screen). Has anyone experienced something like this?

Well this is pretty much a guess because you haven't provided much information, but it sounds to me like the JLayeredPane's preferred size is (0,0), and the "white" you're seeing is the background of the JScrollPane's JViewport child. Try setting a preferred size on the JLayeredPane as a start.

Read the Swing tutorial on How to Use Layered Panes for a working example.
Change the following line:
// add(layeredPane);
add(new JScrollPane(layeredPane));

Related

Howto get size of a cell in JGoodies FormLayout

I want to resize an image inside a JGoodies form layout according to the size the cell has, i.e. so that the image fits perfectly in the cell. If I understand correctly, there is no real component for the cell, the size is known only implicitly by the layout manager, right? Any ideas?
As suggested by kleopatra I found this solution: The image is put inside a scrollPane and I get the viewable size of the cell by
javax.swing.JComponent.getVisibleRect().getWidth();
where the JPanel is a JComponent... I can't paste more code directly, as we use other frameworks that wrap Swing and I didn't test that directly, sorry. If somebody else has this problem and uses this solution please add low level Swing code, thanks.
Thanks for your suggestions!

Java Swing stack components vertically without spreading out

I've been trying for ages to get a list of JPanels to stack vertically without spreading out to all available vertical space.
The container is a JPanel with a BoxLayout
Here is what is happening and what I want:
http://i.imgur.com/K1uUn.png
I've tried using Box.Filler, Box.createGlue(), adjusting alignment properties (they seem to be pretty much useless). Each item calls setAlignmentY(Component.TOP_ALIGNMENT);
Still doesn't work.
Anyone got a solution?
Put the BoxLayout at the PAGE_START of a BorderLayout.

Netbeans 7.0.1 acts weird, couldn't even edit the GUI in design mode

I'm experiencing a very weird problem in Netbeans 7.0.1 while designing a GUI.
At first it was all good, I could design and adjust all the swing components easily and normally. But today, for any reason, it turned to be faulty in which the whole JFrame is extended vertically way way way down (~37,000 in height!), and all the components mixed up messily. However, I couldn't do anything about this, since this happened it doesn't allow me to drag the frame's bottom side upward to minimize the height.
The worst thing is that I couldn't select (click on) some of the components, but some others can be. I don't know how to thoroughly explain this weird thing, just have a look at the Screen shot.
Tell me if you need any further info.
Cheers.
Select the JFrame component from the Navigator panel, and change the height of the JFrame from the properties panel to something like 200. Once you get the size under control, you can use mouse for resizing it to desired size.
This condition may happen if something went wrong while closing the file or if the IDE was terminated abruptly and if the IDE does not get a chance to write all the metadata about the UI properly.
First Note that You can't resize the JFrame if one of the components is the same length as th JFrame. Try resizing that tabbed pane first, then also the list on the left of the tabbed pane (Ongoing Projects).
Next use the inspector to select any component you couldn't select. It might be that another transparent component is covering it, or that it is in a panel and the panel is getting selected instead. Or even the other way round when you can't select a panel because it contains another component. So use the inspector to select components instead.
Finally, if the scroll bar is too long for you to resize easily, you can use the properties window, but note like I said, resize the components in the JFrame first before resizing the JFrame.
Hope this helps
Try to change the size (Height and width) of frame in preferredSize property,and use Panels. Without panels you cannot achieve the arrangement of different objects on a Frame.
After a lot of frustration over multiple failed attempts at fixing this issue using all the tips given by the users above [thanks everyone, but it didn't work :( ], I restarted the NetBeans application in despair. It worked. Hopefully this'll help someone else with the same problem!

Help with transformation of Drawed Layout to MigLayout

I’m making a sample application that uses MigLayout in a very cool way. Unfortunately after reading through the quickstart and the whitepaper I still have questions and can’t do my desired layout. The sample application lets you add/remove games which are basically an Info Panel + JLabel. The layout should have two rows, one for the info panel and the other one to the JLabel.
Layout:
Row 1 (Info Panel) : [grow][grow][grow][grow]
Row 2 (JLabels) : [grow][grow][grow][grow][/list]
Here is an image so you guys can see clearly:
So when I add a Game the layout should shrink the other to fit, like on this image:
And when I delete the layout should grow the remaining one:
But it’s not working with the given layout info, can you guys give me a hand? Also the shrinking JLabel should be handed by me, since it can’t resize automatically???
Sounds like a simple GridLayout will do the trick.
Yes, you will need to do custom paint to resize the image as the space available to it changes. This means you will probably need to use a JPanel and draw the image manually so you can scale the image on the fly.

JScrollPane doesn't show if BorderLayout constraint is CENTER?

Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.

Categories

Resources