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.
Related
Can you tell if your jframe is behind an OS window, or minimized but requesting focus?
I am writing a multi frame java program and trying to keep track of what new info the user hasn't seen yet in.
You can use methods like:
frame.isActive() to determine if the frame currently has focus
frame.getExtendedState() to determine if the frame is minimized
Of course you would to continually poll the frame for this information, so I'm not sure how helpful it will be.
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.
I have been working on a screen recorder app in Java.
I need to know ho to create a JFrame fully transparent?
Like this.
You can make a JFrame partially transparent using the frame.setOpacity(float opacity) method.
For more information, see the Java Tutorial on the subject.
Caution: If you make if the frame 100% transparent, users won't be able to see it.
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);
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.