Mouse Within Component
I've been asked to code something that I thought would be simple, but I can't figure out how to make it work. Here's the situation. I start with a JSplitPane containing two JPanels, each JPanel having multiple child components. The JSplitPane is oneTouchExpandable. Initially, the lefthand panel is collapsed, so that only the divider and the righthand panel show. Now, when the user moves the mouse over the JScrollPane's divider, the left panel should expand. That part was easy and is working fine. The place where I'm having trought is the following.
As long as the mouse pointer remains within the left panel's rectangular area, the left panel should continue to show and allow the user to interact with the components inside it. But as soon as the mouse pointer leaves the left Panel's rectangular area, the JScrollPane should again collapse, hiding the left Panel. I cannot figure out how to determine when the mouse pointer really leaves the left Panel. I've added a mouseListener to the left Panel, but its mouseExited method rarely gets called, because the mouse is over one of the children components--or worse, when mouseExited is indeed called, it's because the mouse entered one of the child components instead of really leaving the left Panel. Then subsequently, the mouseExited never gets called again, even if the mouse leaves the child component and goes somewhere else on the screen.
How can I tell when the mouse pointer truly leaves the left Panel?
Thanks.
Bill Dennis
Here's the code. I've tried to trim down the class so that I'm posting only the relevant parts of the class.
int dividerLocation;
public MyProblemJFrameClass()
{
//
// JFrame has other components, like toolbars, not shown here
// since they have no effect on the basic problem.
//
JPanel movablePanel = new JPanel(new BorderLayout());
JTabbedPane westPanel = new JTabbedPane(SwingConstants.BOTTOM);
westPanel.add("Root", someScrollPaneWithTree);
westPanel.add("Tree", someOtherScrollPaneWithComponents);
movablePanel.add(westPanel, BorderLayout.CENTER);
movablePanel.add(someLabelWithImage, BorderLayout.NORTH);
contentSplitPane = new JSplitPane(SwingConstants.VERTICAL, movablePanel, someOtherJPanel);
contentSplitPane.setOneTouchExpandable(true);
contentSplitPane.setDividerSize(12);
contentSplitPane.addPropertyChangeListener(this);
getContentPane().add(contentSplitPane, BorderLayout.CENTER);
dividerLocation = westPanel.getPreferredSize().width + 4;
hideNavTree();
(((BasicSplitPaneUI)contentSplitPane.getUI()).getDivider()).addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent me)
{
showNavTree();
}
});
movablePanel.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent me)
{
System.out.println("Exited: " + me.getX() + "," + me.getY());
hideNavTree();
//-I want to call hideNavTree() whenever the mouse leaves the confines of moveablePanel
}
});
}
public void propertyChange(PropertyChangeEvent event)
{
boolean treeHidden = false;
String prop = event.getPropertyName();
if (prop.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY))
{
if (contentSplitPane.getDividerLocation() == contentSplitPane.getMinimumDividerLocation())
{
treeHidden = true;
}
if (!treeHidden)
{
dividerLocation = contentSplitPane.getDividerLocation();
}
}
}
void hideNavTree()
{
int saveDividerLocation = dividerLocation;
contentSplitPane.setDividerLocation(contentSplitPane.getMinimumDividerLocation());
contentSplitPane.setLastDividerLocation(saveDividerLocation);
}
void showNavTree()
{
contentSplitPane.setDividerLocation(dividerLocation);
}
Swing related questions should be posted in the Swing forum.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.