-2

For some design purposes, I need to have a Java UI component over 2 panels. However, it seems that any JComponent has to have a parent, and only one. Thus my question is the following : do you have any trick to make a component go over 2 panels at the same time ?

I attach to this post the representation of what I would like to have.

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
YohjiNakamoto
  • 373
  • 2
  • 12
  • 2
    *"For some design purposes, I need to have a Java UI component over 2 panels."* Please be specific about the exact 'design purpose' that has this unusual requirement. Provide ASCII art or a simple drawing of the *intended* (**complete** - ignore all the containers but show all components) layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jan 30 '17 at 09:13
  • 3
    Choose an approach from among those suggested in [*How to put a component on top of others?*](http://stackoverflow.com/q/14675914/230513) – trashgod Jan 30 '17 at 10:27

2 Answers2

2

As you correctly state, each component has to have exactly one parent. Therefore, you have to add JPanel 1, JPanel 2 and JComponent to a common container.

What I would suggest is adding JPanel 1 and JPanel 2 to another JPanel with, say, a BorderLayout or GridLayout, then add that JPanel and your overlaying JComponent to yet another JPanel and use a custom layout manager.

Look into the interface LayoutManager - it arranges the bounds and positions of components in a container. It's actually not difficult to implement, assuming you know what you want. In this particular case, the JComponent could be laid out relative to the two JPanel components which could be passed explicitly to the LayoutManager implementation.

This would be a very specific solution, but you could also generalize it by using LayoutManager2 instead of LayoutManager. LayoutManager2 allows to use constraint objects for the components to be laid out. Each constraint is a plain Java object which describes the layout of a component. It is specific to the LayoutManager2 implementation used.

Markus Fischer
  • 1,326
  • 8
  • 13
-1

It is possible if you overwrite the repaint method for the panels and you don't add the component to any panel, but this solution is too complex.

A component can belong only to one panel.

You can use a third panel that contains all three elements with these two options for layout:

1. Without a Layout Manager
https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

and

2. OverlayLayout layout https://docs.oracle.com/javase/8/docs/api/javax/swing/OverlayLayout.html

alex.pulver
  • 2,107
  • 2
  • 31
  • 31
  • I thought about this solution. If so : how to make one of the element (the "JComponent") get on top of the other ones ? There's no "Z index" or something like that... – YohjiNakamoto Jan 30 '17 at 09:19
  • The order (z-index as you say) is given by the order in that the elements are added to the container. – alex.pulver Jan 30 '17 at 10:45
  • *Without a Layout Manager* NOOOOOOOOOO!!!!!!!!!!!!! Please stop recommending the use of `null-layout`. See [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) to see why it's a bad practice, so, please, stop recommending it – Frakcool Jan 30 '17 at 13:58