Reputation: 1235
I have used MouseAdapter
in order to detect if the mouse enters or exits a JPanel and it works, somewhat, because it doesn't detect if the mouse is hovering over the JPanel.
public class SearchResultPanel extends JPanel{
private class mousePanelListener extends MouseAdapter{
@Override
public void mouseEntered(MouseEvent e){
setHighlightBorder(); //"highlight" the panel
}
@Override
public void mouseExited(MouseEvent e){
setDefaultBorder(); //"unhighlight" the panel
}
}
private final Border highlightBorder;
private final Border defaultBorder;
//ctor
SearchResultPanel(String data){
super();
setLayout(new GridLayout(1,1));
JTextArea textArea = new JTextArea(data);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setOpaque(false);
add(textArea);
addMouseListener(new mouseMotionPanelListener());
setFocusable(true);
highlightBorder = BorderFactory.createLineBorder(Color.RED, 5);
defaultBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
}
private void setHighlightBorder(){
setBorder(highlightBorder);
}
private void setDefaultBorder(){
setBorder(defaultBorder);
}
}
The SearchResultPanel
s highlight only when the mouse is at the edge(outline) of the panel. They go back to the default border if they are inside the panel or outside.
How can I detect if the mouse is over the JPanel so the JPanel sets its highlighted border?
Upvotes: 0
Views: 1668