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.
Related
I've started adding a few buttons to my new Java Swing project (first time using Swing) and I tried adding a mouse listener event to them with the intention of having the background of the (transparent) button to change to a darker color when the mouse is hovering over it.
I did this using JPanels behind the the buttons, and I tried changing their backgrounds to a 50% opaque color.
Code:
for(int i=0;i<5;i++){
JPanel p = new JPanel();
p.setOpaque(false);
b[i].setContentAreaFilled(false); //b is an array consisting of 5 buttons
b[i].setForeground(Color.RED); //
b[i].addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
p.setOpaque(true);
p.setBackground(new Color(0,0,0,50));
}
public void mouseExited(MouseEvent e){
p.setOpaque(false);
p.setBackground(new Color(0,0,0,0));
}
p.add(b[i]);
}
I have multiple unexpected things happen with this code:
Every time I click the button (I have no ActionListener on them) the button (and only the button, not the panel behind it) gets increasingly darker until it reaches black.
If I hover over a (white) button in another part of the program first, then over any of these buttons the panels behind them become white instead of a darker version of the original color.
If I hover over any of these buttons, and then over another one of them afterwards, the second button seems to display the text of BOTH of the buttons.
Screenshots:
1: https://cdn.discordapp.com/attachments/871490774301294642/957684879712219168/unknown.png
2: https://cdn.discordapp.com/attachments/871490774301294642/957685306562338826/unknown.png
3: https://cdn.discordapp.com/attachments/871490774301294642/957685596900438086/unknown.png
Maybe I'm doing something blatantly wrong but I'm quite new to Swing so sorry if that is the case. Any help appreciated.
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() {});
I have a JLayeredPane, with a JButton and JPanel. Both are the same size and at the same location. The JPanel is transparent with a LineBorder and the button also, so that there is another border in front of the Jbutton, but when I move the mouse over the panel the Button appears in front of it. I want the button to move and JPanel to stay put, but why does the JButton move in front of the JPanel when moving mouse over it?
, but why does the JButton move in front of the JPanel when moving mouse over it?
Mouse events are generated as you move your mouse around the frame.
In this case a "mouse entered" event is being generated as the mouse moves over the button. By default a button has a rollover effect and the button needs to be painted, so it is painted on top of the panel.
Not sure, buy you might be able to turn off this effect by using:
button.setRolloverEnabled( false );
However, when you click the button you will have the same problem.
I have a problem with mouse events in my program. I'm trying to code a drawing program with a canvas.
The user should draw if he left-clicks and moves the mouse. So I defined a class Drawer with a boolean allow_draw in it, and I added a method draw.
draw is called with a mousemoved event in the canvas and allow_draw is set true and false with mousepressed and released.
However, mousemoved isn't firing while I press the mouse button...
My question is: how can I listen to mouse movements while a mouse button is pressed.
Hope you know what I'm looking for :)
Can you please post your source code? Please try adding a MouseMotionListener. Here is an example from a project I am working on.
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});`
You should consider,
using a combination of MouseListener and MouseMotionListener, which is conveniently combined in the MouseAdapter class.
Turn drawing on when mousePressed occurs.
Turn drawing off when mouseReleased occurs
Draw within mouseDragged if drawing is on (use an if block).
Add your MouseAdapter object twice to the component, with the addMouseListener(...) method and the addMouseMotionListener(...) method.
A mouse move event with a pressed button would be a drag event. Simply listen to 'MouseListener#mouseDragged', it is what you're looking for.
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();