How to make JPanel stay onTop (on z-axis), using setLayout(null) - java

I have a JPanel named Scene which includes the following JPanels:
Frame, Projector, Tools.
I use null Layout on Scene because I want to use specific co-ordinates so no other Layout fits for me. I use the following code to add the 3 JPanels into the main JPanel Scene.
setLayout(null);
add(Tools);
add(Frame);
add(Projector);
I use this order because I want Tools to be always on top (on z-axis).
Now, if I do something inside the Projector that causes a repaint, then Projector comes on top of the other layers, hiding Tools. I can use a Thread to repaint the whole Scene JPanel but I see this "flash" when Projector comes onTop and goes onBottom and I dont want to do this.
Any ideas?

If you want to place components above each other in relation to their z-axis, then consider using a JLayeredPane as the main container, and then adding components using the add(...) method that takes an Integer as its second parameter. The bottom-most layer should be added using JLayeredPane.DEFAULT_LAYER, which I believe is 0. This will allow non-oqaque top components to show lower components through them.

Related

Make children of JLayeredPane fill available space of container

I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.

How to hide other panels or panes during coding

Using Eclipse Juno for Java & WindowBuilder
I have three panels of the same size that lay on top of each other - they have different widgets. During coding, they all display and clutter up what I'm doing.
I can show and hide them in runtime as needed but, I want to display only the one I'm working on while doing drag and drop of widgets. I've tried using different panels and pane types (tabbed, layered...) and selecting opaque but, nothing hides them.
How do I hide the other (panes, panels...etc) during coding?
I have the same issue. Among other reasons, my solution was to create separate classes per view. So my frame would be its own class, it would maybe have a TabbedPanel (or whatever it's called), and then I would have a new class for each tab on that tabbed panel. Each class would extend JPanel so I could plop it right in there. That way not only is your gui design not cluttered up, but your code logic is separated into separate files, where it might belong anyway.
[SOLVED] Answering my own question.
It may not be perfect or the best/correct way, but it works!
WindowBuilder wants to Surround other panels/widgets that are within it's bounds so, you have to trick it by using opaque, Order>forward/backward then setting the desired bounds (all panel sizes and bounds can be equal and will overlay nicely both during widget drag&drop and runtime).
Here's how to do it with a 3-Panel example (NOTE: WindowBuilder is buggy/in-consistent and often I needed to select the items from the gui, not in the Components tree).
Create your first panel. Add your widgets and border to it.
Create your second panel (the one you want to overlay on top of the first one). This second panel MUST not be completely inside the first panel - it MUST extend beyond the edges of the first panel (parts of it can be inside the first panel). This takes a bit of trial because of the 'surround', mentioned above. Use the shift-key to stop the snapping.
Select the top panel in the gui, NOT from the Components tree, and toggle the Opaque property. The top panel (first or second) in the tree is the one you set to opaque and work on.
Add another panel and repeat the process.
Once you get your widgets/etc as you want them, use the property Bounds to set them all the same or as desired. After that, as long as you don't move a panel by dragging it, it will remain un-surrounded by the other panels. If you move by dragging, it may get set to surround...
I've done this a dozen times now and it works consistently.
Below is a shot of 3 panels overlayed, un-surrounded and not opaque, thus showing widget clutter
Below is a shot after the bounds are set (and not surounded). Opaque and order not yet set:
Below is a shot with bounds set and panel 3 moved forward and opaque set:
Below is a shot with bounds set and panel 2 moved forward and opaque set:
... etc, etc... Now you can work on a panel that's ordered to the front and, naturally, use the setVisible in your code...

java JLayeredPane how to add JPanel in front of another panel generated by Window builder

I am writing in java some kind of application and i have to place something in front of another objects( in Z order)
I know that that i should use JLayeredPane but in fact i am not very familiar with it.
My idea is to make two JPanel's with different Z-order factors while inserting them to JLayeredPane.
i pasted my code http://www.wklejto.pl/130038
i would be very grateful if you tell me what is wrong because i am doing it for a long of time with no effect.
I don't see anything wrong with this code. Maybe you're trying to paint a transparent (not opaque) JPanel (e.g. with message) on top of the underlying base JPanel.
In that case you should invoke setOpaque(false) on your front JPanel.
JPanel second = new JPanel();
second.setOpaque(false);
second.add(new JLabel("message"));
jlp.add(second, new Integer(300));
JPanels are opaque by default - on the other hand JLabels aren't.
And take a look into tutorial.

How to add multiple components to a JFrame?

I have a JFrame.
I also have a Box class which extends Component.
This box class has a paint method which makes a filled rectangle.
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.
I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.
(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).
Thanks in advance!
You have 2 choices.
You can change the layout of your frame:
JFrame frame;
frame.setLayout(new FlowLayout());
Now, if you add more than one box, it will show up on the frame.
The other option is to do what you said you tried. (Adding a panel to the frame)
JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')
Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.
You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.
And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.
You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.
EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.
Do your layout on paper first, then read up on Swing layout managers.
Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.
You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.
Thank you for all your answers.
Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.
I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.
If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.

How to set JPanel size?

I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.
-Brett

Categories

Resources