简体   繁体   中英

JSplitPane small border but big grab-hitbox

I have two JPanels combined in a JSplitPane. The user should be able to move the split-pane-divider very freely, but the divider shouldn't be too big (at best case only 1px).

Is there a way to keep the dividerSize at 1 but to increase the hitbox for clicking on the divider? So that maybe 20px to each side the divider is also grabbed when the mouse is clicked?

You can add MouseListener to the components an manually adjust the divider location.

Here is a basic example to get you started:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DividerListener extends MouseAdapter
{
    private JSplitPane splitPane;
    private Component component;
    private int pressedX;
    private int dividerLocation;

    public DividerListener(JSplitPane splitPane, Component component)
    {
        this.splitPane = splitPane;
        this.component = component;
        component.addMouseListener(this);
        component.addMouseMotionListener(this);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        pressedX = SwingUtilities.convertPoint(component, e.getPoint(), splitPane).x;
        dividerLocation = splitPane.getDividerLocation();
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {

        int draggedX = SwingUtilities.convertPoint(component, e.getPoint(), splitPane).x;
        int deltaX = draggedX - pressedX;
        splitPane.setDividerLocation(dividerLocation + deltaX);
    }

    private static void createAndShowGUI()
    {
        JSplitPane splitPane = new JSplitPane();

        JLabel left = new JLabel("LEFT");
        splitPane.setLeftComponent(left);
        new DividerListener(splitPane, left);

        JLabel right = new JLabel("RIGHT");
        splitPane.setRightComponent(right);
        new DividerListener(splitPane, right);

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( splitPane);
        frame.setLocationByPlatform( true );
        frame.setSize(200, 200);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

You will need to customize the logic to check if the mouse is pressed at the right edge of the left component or the left edge of the right component. So you will probably need to add another parameter so you know how to do the bounds checking.

Then you would need to set a flag so the mouseDragged code can either process the event or ignore it when you are not close enough to the edge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM