Java (Swing) - Drag two windows at once - java

How can I have one window move with another? i.e., I'd like a JDialog to follow a JFrame when the JFrame is being dragged. If the JFrame moves by (+5, +20), the JDialog needs to move the same.
I've tried using ComponentListeners, but I only receive drag events in chunks which causes the JDialog window to be jumpy while dragging the main JFrame. I've tried using MouseListeners, but I can't figure out how to detect events on the actual frame of the JFrame.

AFAIK here is no move multiple windows in AWT. To get the moves to be called at a similar time, I guess you want the JFrame decorations to be PL&F rendered. Put in a PL&F-specific hack to do the moves yourself, moving both windows at almost the same time. You may still have a problem with exposing bits of windows only to cover them up causing some performance degradation.

Try using the ComponentListener.componentMoved event instead of monitoring drag events on the JFrame.

The component listener method works perfectly.
I did something like this:
Point p = this.getLocation();
p.x += this.getWidth() + 10;
this.getOwner().setLocation(p);
Where the '10' is the space between the current window(a JDialog) and its owner which is to its right.

Related

How to restrict swing Jframe in desktop screen?

I am new in J2SE, I am using Java Swing to create J2SE application. The JFrame can pick and drag at any where on screen but I want to restrict frame to do not reach out from desktop screen.
How to enforce the drag restriction?
Note: The code must work for all type of O.S.(Window, Linux, Mac)
I found help from net but that code is not supportable for all type of O.S.
You will need to know size of the screen , and size of JFrame.
Toolkit.getDefaultToolkit().getScreenSize();
Use thread to scan for location of JFrame on screen. CONSTANTLY / Daemon
Find the location/Position of JFrame in the window
If user gets to corner of the screen, do some math.Set your JFrame ot latest valid location or to center of the screen.
How to set JFrame to appear centered, regardless of monitor resolution?
Bud for the love of sanity , dont do this. Just let user move it where
he wants it.If any software woud do this to me i woud consider it
borderline Malware sincei use multimonitor setup, and this is not
acceptable for me.

Recreate Windows Look-and-Feel border of JFrame or JDialog?

I need to recreate the translucent border of JFrame or JDialog that appears when setting the windows look and feel in swing.
I need it because Windows LaF does not let you access the title bar (on the border). In fact, I need to apply a MouseAdapter to the JDialog that gets notified when it is dragged/pressed/released. In windows laf, as you cannot get access to the bar component, you can only apply a ComponentListener which gives you notification only when moving (so you don't capture anything when the user has grabbed it but hasn't moved yet, or either when the user "releases" it).
Therefore, I decided to go with undecorated dialogs and apply the listeners to my custom bar. However I want the custom dialog looks exactly as in windows laf (it means I need to recreate the border).
I'm not very experienced in Graphics2D to override the paintBorder() method, so I'm asking for your help.
Has anyone ever faced this problem and has a tested solution?
As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, you can add translucent and shaped windows to your Swing applications.
This means that you can have your JFrame emulate a native window with the rounded corners and transparency.
In your case, your approach would be in the JFrame level instead of the border level because the border is painted on top of the JFrame (or JDialog, for that matter). Therefore, if the JFrame is not already rounded, for instance, the paintBorder() method will still be painting on top of a rectangular area of the screen.
Check this tutorial from Oracle covering shaped and translucent JFrame.

Java Swing - user alerts

I am trying to build a user alert mechanism by bringing up the window to the front and then flashing the icon on the screen for the user. I have two questions with regards to this approach:
How can you find the current window you are at in Java and then de-minimize it and bring to front?
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
Thanks a lot for any replies.
How can you find the current window you are at in Java and then de-minimize it and bring to front
Window[] allWindows = Window.getWindows();
returns arrays of all Top-Level Containers from current JVM e.g. J/Frame, J/Dialog(JOptionPane), J/Window,
you can to test for (example) if (allWindows[i] instanceof JFrame) {
then WindowState returned WindowEvent
by bringing up the window to the front and then flashing the icon on the screen for the user
use undecodated JDialog (works toFront, toBack) with
create only once time
setDefaultCloseOperations(HIDE_ON_CLOSE)
use Swing Timer for hide JDialog
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
have look at Java Translucent Window, put there Icon to the JLabel (or to the JButton)
use Swing Timer for flashing by hiding Icon or swithing bewtween two or more Icons (three or four is good)
I think the simplest way to get the window ancestor is :
SwingUtilities.getWindowAncestor(yourComponent);

JFrame attached on the side of another JFrame

I'm trying to attach a JFrame to another JFrame, it works by setting the coordinates of the 2nd jframe to the right edge of the first. But it doesn't feel like one whole because when I drag the first JFrame the second one lags behind it like a dog.
Is there any way to keep them from seperating when you drag it, so it looks more like one whole?
Because the host's heavyweight peer component owns the frame, this will be difficult to do in a cross-platorm manner. On Mac OS X, you might look at binding to Cocoa Drawers and Disclosure. The MacWidgets project may have useful examples. See also this Q&A.
Try a JSplitPane or a JInternalFrame, JInternalFrame provides many features of a native frame.

ToolTip flicker in Java if outside JFrame?

I am implementing ToolTip in Java as to make users having an easier time to use the product. Though tooltip that are at the borders of the JFrame and ends up outside the JFrame starts to "flicker". I've tried lots of things (like moving the tooltip so it should be inside the Jframe, controlling the painting so it ends up within the JFrame and so on) though it doesn't work.
Anyone got any expertise within the field that know how to avoid this problem?
Cheers,
Skarion
When a tooltip is displayed in a JFrame, Swing does not create a floating window, it simply paints the tooltip in the graphic context of the JFrame. This does not generate any flickering.
On the other hand, when a tooltip is outside the boundaries of the JFrame, it becomes heavyweight: a window is created to host the tooltip component. Flickering occurs when the tooltip window appears.
Maybe setting "-Dsun.awt.noerasebackground=true" would help because it prevents one step of background repainting of the hosting window.

Categories