Java Glass pane - java

I have a problem. I want to ask you how can I implement GlassPane to paint on it.
I mean, if I click mouse button, in mouseClicked event, my transparent glass pane should be created, because I want to see all my components behind glassPane and I can paint on it using mouseDragged event. When I release mouse button my glassPane disappears.
I have another question too. When I will paint on glass pane all my components behind them will be refreshing and repainting? Maybe somebody have nice example with glass pane which might help me.

How to Create Translucent and Shaped Windows

Related

Swing: how to paint an animation over every component, JPanel, JButton, etc?

I'm trying to paint over every component in my Swing application.
what I have:
jButton b = new JButton();
b.addActionListener(e -> fillEntireScreen())
f.add(b);
f.setVisible()...f.setSize()...f.setDefaultCloseOperation()...
where fillEntireScreen() simply is an animation that expands over the screen. I override Component's paintComponent(), painting over the frame, not a panel or anything.
fileEntireScreen() works without the button, but does not work when I add the button, as the animation is behind the button.
how can I paint over the button?
Thanks!
Use a glassPane instead - See How to Use Root Panes for more details and How can I paint in an specific JPanel when more than one in same frame- Java for an example.
Alternatively you could use a JLayer, but's more complicated and may not suit your needs. See How to Decorate Components with the JLayer Class

Some JPanel's backgrounds fade when I interact with page

As i wrote in the title, I've this problem with JPanel.
My page is structured as a mainJPanel, another JPanel inside the main (where I paint a background) and some component added to this background panel.
Everytime I interact with something (for example i click a button) the background of the components fade to a lighter version of themselves.
I really can't figure out why.
mainPanel have setOpaque(false) and setLayout(null);
backgroundPanel have setOpaque(false) and setLayout(null) (i painted the background when i create the class backgroundPanel that extends JPanel, with an override of the method paintComponent);
all the components that becomes lighter have setOpaque(true) and setLayout(null);
the background of this components are a custom grey (rgb(232,232,232)).
Thank you all
Using setOpaque(false) should not cause a problem (ie. JLabels are non opaque). It sounds like a painting problem when using transparent colors.
Check out Background With Transparency for an explanation of the problem and a couple of solutions.

Making custom UI in swing

I need to make a custom ui which looks like the following image :-
I know how to change the button to that arrow shape and everything else.But I can't understand how to move those close ,restore and minimize buttons to the center and give them round shape (on Windows).
On Googling ,I found how to make custom shape windows but it doesn't meet my requirements.
Can anyone please tell me how to do this or any link.??
You will need to create your own title bar as another panel with buttons and then remove the window decoration on the frame.
JFrame frame = new JFrame(...);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//This will close the frame and exit
To minimize and maximize, listen for button events and use:
frame.setState(Frame.ICONIFIED);//minimizes
frame.setState(Frame.NORMAL);//restores
See Frame.setState(int) for details.

Making JFrame transparent breaks scrolling on Mac

I have a transparent undecorated JFrame that I set using AWTUtilities.setWindowOpaque(this, false). On the JFrame, I have a scrollpane; it works perfectly on Windows. On the Mac, the whole JFrame is draggable; so when I try to scroll through the scrollpane by clicking and holding the mouse on the scrollbar, the entire frame moves instead of the scrollbar thumb. I also tried to use setBackground(new Color(0,0,0,0)) instead of setWindowOpaque(), but it has the same problem. Any ideas on how to fix this?
As suggested in this similar thread, try:
getRootPane().putClientProperty("apple.awt.draggableWindowBackground", Boolean.FALSE);
If you choose to use this, the scrollbar will be usable and the window won't drag. However, you may be stuck with an immovable window, unless you add a MouseMotionListener and move the window around in the mouseDragged() method using a call like frame.setLocation().
Instead, you might be able to force the user to click on the scrollbar's arrow buttons, rather than drag the scrollbar itself... But that's not the most user-friendly idea I've ever seen.

Adding components to a GLCanvas

I'm working with an application that draws on a GLCanvas. I'd like to add a "floating menu" on top of it (something I would do in Swing by adding a menu to the glass pane). Since GLCanvas doesn't extend Container, what would be the suggested way to do this?
GLCanvas inherits from java.awt.Component, so when you add a GLCanvas to your JFrame, you could use the glasspane on your containing JFrame.
Or, depending on the visual effect you want, you could, after your scene is done rendering on the GLCanvas, add a GL call to glOrtho, and then draw your menu on top of the scene using primitives in GL itself, (though then you'd be stuck rigging your own callback behaviors and such... I'm not sure from the question if you want to get into that).
Are you talking about a popup menu? You can add a MouseListener to your GLCanvas (since it extends from Component), in the MouseListener, check the mouseEvent.isPopupTrigger(), and if so, create your JPopupMenu - since you want to show it over a heavyweight component, call setLightweightPopupEnabled(false) before showing the JPopupMenu - then call show(glCanvas, x, y) on your JPopupMenu.

Categories