In the examples of SwingX which JXCollapsiblePane is used with a button, but I want to transpose it with mouse events. In my example, the JXCollapsiblePane is closed at the beginning. Only when the user comes with the mouse on the button to open the JXCollapsiblePane. When the mouse leaves the area, which is supposed JXCollapsiblePane collapse again. My problem: The JXCollapsiblePane is not collapsed, when the mouse leaves the area via the button.
public class CollapsiblePaneDemo
{
/**
* @param args
*/
public static void main( String[] args )
{
final JXCollapsiblePane cp =
new JXCollapsiblePane( JXCollapsiblePane.Direction.RIGHT );
// JXCollapsiblePane can be used like any other container
cp.setLayout( new BorderLayout() );
// the Controls panel with a textfield to filter the tree
JPanel controls = new JPanel( new FlowLayout( FlowLayout.LEFT, 4, 0 ) );
controls.add( new JLabel( "Search:" ) );
controls.add( new JTextField( 10 ) );
controls.add( new JButton( "Refresh" ) );
controls.setBorder( new TitledBorder( "Filters" ) );
cp.add( "Center", controls );
JXFrame frame = new JXFrame();
frame.setLayout( new BorderLayout() );
// Then the tree - we assume the Controls would somehow filter the tree
JScrollPane scroll = new JScrollPane( new JTree() );
// Put the "Controls" first
frame.add( "Center", scroll );
// Show/hide the "Controls"
final JButton toggle = new JButton( cp.getActionMap()
.get( JXCollapsiblePane.TOGGLE_ACTION ) );
toggle.setText( "-" );
toggle.setPreferredSize( new Dimension( 20, toggle.getSize().height ) );
toggle.addMouseListener( new MouseAdapter()
{
@Override
public void mouseEntered( MouseEvent e )
{
if ( cp.getSize().width == 0 )
{
toggle.doClick();
}
}
} );
final JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.add( "Center", toggle );
panel.add( "East", cp );
panel.addMouseListener( new MouseAdapter()
{
@Override
public void mouseExited( MouseEvent e )
{
if ( !panel.contains( e.getPoint() ) )
{
toggle.doClick();
}
}
} );
frame.add( "East", panel );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
cp.setCollapsed( true );
frame.setVisible( true );
}
}
Thanks,
The mouseExited
event is triggered when the cursor leaves the JPanel
- either by leaving the bounds of the JPanel
or by entering one of the subcomponents. Because the button is right on the edge of the JPanel
, the cursor never enters the JPanel
again on the way left and thus can't exit it.
You could modify the mouseEntered
method in the button's MouseListener
to collapse the control panel if it's open and let your existing MouseListener
handle the case where the user leaves through the frame border. You need to keep track of the expanding/collapsing state of the control pane (SwingX API probably does this for you already) if you want to prevent the user from chasing the button and re-triggering it (I didn't bother in the code below).
My modified MouseListener:
toggle.addMouseListener( new MouseAdapter()
{
@Override
public void mouseEntered( MouseEvent e )
{
toggle.doClick();
}
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With