Preventing components below panel to get hovered when panel is transparent - java

I have a JPanel, set to be transparent:
public SomePanel() {
setOpaque(false);
[...]
}
I have other JComponent instances under it (at the same location, but below it).
If I draw on the panel using paintComponent(g), putting my mouse on the panel still triggers mouseEntered and mouseExited events for other components below it.
How can I prevent components below the panel to fire mouse events if the non-opaque panel is visible? I am using setOpaque(false) because I need a transparent background, perhaps there is another way to achieve this?

One possible solution: give the covering JPanel its own MouseListener, one that is there to simply swallow the mouse events and prevent them from being transmitted. The code could be as simple as:
myPanel.addMouseListener(new MouseAdapter() {});

Related

How disable JFrame Background

I've made a JFrame with the next Properties:
setLayout(null)
setUndecorated(true)
setResizable(false)
within i've put a JLabel with one Icon(PNG image) in netbeans, and im looking for some soluction that disable the backgroud(full Transparent, and unactive) when the mouse moves THROUGH JFrame's Image(or someother component) BUT the mouse works different outside of the JLabel because JLabel Icon avoid any mouse action over the JLabel. But there's a default Gray background that doesnt exactly what i want.
We can see that mouse doesnt do anything on the JLabel(unless there were some component in the Frame)
Green = JFrame size.
And here the mouse change when moves through of the web page
im looking for some soluction that disable the backgroud(full Transparent, and unactive) when the mouse moves
Don't use full transparency.
If the pixels are not 100% transparent, then the MouseEvents will be handled by the frame.

What is the order of adding eventlistener to a component and adding that component to a panel or frame?

Can a mouselistener be added to a component say JLabel after the JLabel has been added to the frame or panel ? Or eventlistener has to be added before adding the component to the panel or frame.
The order shouldn't matter, since the user can't interact with the control until the current piece of code returns control to the system. Are you having trouble catching an event?

How to make an auto-hide the JToolBar?

I'd like to make an auto-hide the JToolBar and it appear only when the mouse goes near/over the JToolBar. I have added the JToolBar in JPanel. There is no mouseover listener in JToolBar. How to do this?
Add a MouseMotionListener to your JFrame or JDialog.
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
toolbar.setVisible(e.getY() < 10);
}
});
In that way, the tool bar will only be shown if the mouse is in the top 10 vertical pixels of the window.
There is no mouseover listener in JToolBar
You would use a MouseListener is handle the mouseEntered and mouseExited events.
But you will have a problem because the mouse events will only be passed to a visible component. So once you hide the toolbar is will not receive the mouseEntered event.
So I don't understand your design. Do you plan to have the other components shift up to fill the space by the toolbar? Or will you just leave the space empty? In the latter case you would then need to add the MouseMotionListener to the panel and handle the mouseMoved event to see the the mouse is at a location where the toolbar should be.

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.

updating view only after changing the size of JFrame

So I have a JFrame in which inside of it I have a JScrollPane and inside the JScrollPane I have a JPanel. I have a button click mouse listener that modifies the JPanel inside the JScrollPane. The code in the mouse listener is:
#Override
public void mouseClicked(MouseEvent arg0) {
searchResult.updateInfo();
}
The method updateInfo is adding a bunch of JPanel into the searchResult JPanel. After I click the button associated with this listener, nothing happens, but when I resize the JFrame.. it updates the view.. why is this?
I tried repaint the JFrame, but it did not solve my problem
After adding/removing components from a visible GUI you need to tell the panel to layout the components so the preferred size can be recalculated. You need to add:
searchResult.revalidate();
searchResult.repaint(); // sometimes needed
Then if the preferred size is greater than the size of the scrollpane scrollbars will appear.
make
searchResult.removeAll();
searchResult.updateInfo();
searchResult.validate();
if it doesn't work make like this
searchResult.removeAll();
searchResult.updateInfo();
searchResult.validate();
searchResult.repaint();

Categories

Resources