Have a window track a component's location on screen - java

In Java Swing, I want a Window to show up right next to a component--so it will float on top of the GUI. But I need it to always stay right next to that specific component wherever it moves, whether the user moves the window, scrolls the scroll pane that the component is in, resizes, etc. Is there a straightforward way to do this?
I already know how to get it to show up in the right place to begin with. So, for example, if there was some event that fires anytime a component has changed location on the screen, that would work.

Take a look at Componet#getLocationOnScreen
You're going to have to take into account the size of the component and the possibility that the window could be opened outside of the current screen bounds, but lets start with small steps

I think I found it. The java.awt.Component.addHierarchyBoundsListener fires events when scrollbars move, windows move or resize. I think that will do what I need.

Related

Java registering events through window

What I would like in Java is to create a transparent screen over the entire screen that does not respond to events such as mouse or keyboard. Essentially, the user won't even know about the screen. How would I make the window "transparent" to events?
Read the section from the Swing tutorial on Creating a Translucent Window.
You would use an opacity of 0.0f.
Edit:
With an opacity of 0.0f the events go through the frame to the component below, therefore the window you click on will come to the front.
With a non-zero opacity (ie 0.05f) the events are intercepted by the frame.
You may also need to use:
frame.setAlwaysOnTop( true );
to prevent the window from coming to the front.
Play with the opacity and always on top property to get your desired effect. If these two properties don't work then I have no ideas to do what you are asking, nor do I understand the requirement.

Set correct dialog location without knowing parent component

I am trying to figure out how to get rid of parents passed in application just for the reason to center dialogs over window or frame.
Application I am responsible for is quite large and not written nicely. Refactoring is in process however it's not easy to decouple some things. Part I am trying to change now is window with tabbed pane. Tabs are same class and consists of several panels which are responsible for different things, so they are also in different packages and classes. Good thing that each class is responsible for single thing only now, bad one is that there are lots of dialog (error, info, question...) which require parent to be centered correctly over window.
I would like not to use these parents all the way down, however at this moment it seems impossible to move all dialogs up in hierarchy. Just setting parent null also doesn't solve problem as they appears somewhere else then window (may be problem on dual screen).
I was thinking about static method in main window which returns current location (or null if doesn't exist). Then set all references to parent as null and set modality type to application. Finaly setLocation on point given by static method. I feel it's not the best solution, but I think it's much better then passing whole window as parameter cca 10 levels down.
What do you think about it? Is it acceptable or do you have some better idea how to handle parent in large GUI application?
EDIT: To specify my intention more deeply - I need dialog boxes to be centered over it's parent. In other words, it doesn't need to be centered on the screen, it should be located directly over the window (not necessarily in the center of window bot just over it). If I set parent to null or location to center of the screen, it can happen that dialog box will display somewhere else than parent window.
EDIT2: So I did it for now as I suggest above. I have created static field JFrame parent = null. Next static method was created public static Point getCenterPosition(Dimension dimension). If something calls this method and parent doesn't exist yet, topleft of the screen is returned (however it can't happen).
As parameter is passed size of dialog I want to center, which is used for inner calculation. As a result method return point, so I can use dialog.setLocation(ParentFrame.getCenterPosition(dialog.getSize()));
Now I have dialog nicely centered and I don't need passed parent at all. It's not very nice solution, but it works.
While I am not entirely sure what specific effect you are trying to create without some form of illustration I believe you are trying to get a dialog or pane centered in the screen.
Common practice for the creation of GUI is to base your sizes on the screen resolution that is being used. (this helps your application scale to different resolution monitors)
If you know the screen resolution it is also relatively easy to determine the center point of the screen where you want to display your dialog or pane.
An out of the box solution for determining screen size can be found in the Toolkit (Java7)
I propose you use getScreenSize() or getScreenResolution() (depending on your preferred approach) and then take half of each dimension to find the center point of the screen.
subtract half of your dialog or pane size and you will know the origin point for your pane.
You could put inside of the dialog which has to be centered something like
Window ancestor = SwingUtilities.getWindowAncestor(this);
setLocationRelativeTo(ancestor);
I use it all the time and it avoids having to pass the parent explicitly.
Of course the dialog has to be called by the parent you want to center to.

Remove close (X) button from the top right corner and keep min/max on JFrame

I want to create my UI with a JFrame and a minimize and maximize look-alike button with the same functionality as normal minimize and maximize buttons but I am not sure how. I want to take this approach so there will not be an X button in the top corner.
You cannot remove it and keep the other two, however, you can disable it using setDefaultCloseOperation:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
Note that this will not make the button unclickable but will disable the functionality.
For an undecorated frame you'd have to use JWindow instead of JFrame, then render your own title bar.
Of course, whatever you render will only look right on one particular platform in one particular look and feel (unless you write your own logic to handle different platform conventions by hand). For example, Mac users will expect the close, min and max buttons to be traffic light coloured circles at the left hand end of the title bar, not square buttons at the right.

Howto paint a popup with image beside a component being positioned in a relative layout manager?

A have a list with small thumbnails and want to show a fancy tooltip with a larger version of the image of the thumbnail left to the thumbnail if one hovers with the mouse over the thumbnail. If one drags the mouse down to the next list element the old tooltip should disappear and a new tooltip next to the new cell should appear.
Unlike the existing text tooltip javax.swing.JComponent.setToolTipText(String) my fancy tooltip should always be displayed left of the current column. The problem is, that this is a very complex relative layout I cannot change for this. The fancy tooltip should appear above all other components like the real tooltip does too, i.e. I cannot reserve some free space for it.
I took a look in the implemenation of the real tooltip and think about something like getting the absolute coordinates of the mouse mouseEvent.getX() and subtracting the position inside the thumbnail to get the right corner of my fancy tooltip. Is it possible to get the coordinates of the mouse event inside the component where the listener is registered? Any better ideas for my needs?
I thought java.awt.event.MouseEvent.getX() is the absolute point, but thats java.awt.event.MouseEvent.getXOnScreen(), i.e. this is already the answer...

How do I control the display of a JComponent's Tooltip?

I have a JComponent that's painting various shapes on itself. I'm detecting whenever the mouse enters one of these shapes and changing the tooltip accordingly.
The problems I'm having are:
The tooltip doesn't follow the mouse as the user tracks the mouse across the shape. It
stays where it was first set and then only jumps whenever another shape changes the tooltip.
It takes about a second for the tooltip to appear, but I'd like it to appear immediately.
Can someone suggest a way of getting these behaviours without writing a custom tooltip mechanism?
Take a look at the ToolTipManager.
You can register your component with that manager and then adjust a number of settings. Its pretty straight forward to use.
That at least can solve your initialdelay problem.
For your first problem you can overide the createTooltip command from your component to get a hold of the JTooltip instance. and then its easy make the position change whenever you move your mouse(aka follow your mouse) as its a subclass of the JComponent class.
To solve your first issue of where the tooltip doesn't follow the mouse, if you override the getToolTipLocation(MouseEvent e) in JComponent, you can return the point for where you want to the display the tooltip. The MouseEvent will allow you to retrieve the x and y.

Categories

Resources