Java, get panel landed on mouse event - java

I have a draggable panel (not using the java drag and drop) that has to land on another panel. The dragged panel generates the event but I want to obtain the panel it lands on. Do I have to have an array of all the "landable" panels somewhere and cycle through it to get the one I want by comparing positions or is there a faster, better way?
Thanks in advance.

The Container class has a findComponentAt(...) method.

Related

How to enable/disable a magnetic grid in Java

Here's what I need to do:
Make a grid where I can drag and drop elements. (already done)
My problem is I need to give the option to enable magnetic grid, such as the Windows desktop when you allign icons.
What type of item should I use with Swing ? (Jpanel, GridLayout)
Is there an easy way to make a magnetic grid without coding everything?
*I need to be able to place items wherever I want when the magnetic option is not checked.
Thank you guys
Consider using a JLayeredPane, and adding as one layer a JPanel holding a grid of JPanels. When you want the grid functioning, then when a component is released over the GUI, the JPanel cell that contains your mouse pointer can accept the component and can perhaps center it using a GridBagLayout. When you want to place items in a free-hand way, then add them to a JPanel layer of the JLayeredPane that uses a null layout.

How to add a swing component to a Java 3D Applet?

I have made a Java 3D applet which basically displays a 3D graph with nodes(the nodes are spherical). Now, I want a side panel which contains a list of these nodes,so when I click on a node, the corresponding entry in the side panel should get highlighted/checked(if its a check box) Can anyone please help me as to how to get this done?
Try limiting the size of the Canvus for your graph and place it in your JFrame. Then put your JPanel into your JFram. Should work thought i have problems with JPanels being in there correct spot.

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...

How do I make a Swing JComponent a bigger mouse target?

I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.

Is it possible to have "movable"/"draggable" components like JButtons, JTextFields in a JFrame?

Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.

Categories

Resources