Kapil Sharma
Kapil Sharma

Reputation: 1432

Restrict JSplitPane divider movement

I am looking for a way to restrict divider movement beyond a certain width on Left Hand side. For example, I can use setDividerLocation(400) and then add another property with width = 500, so that user of the Swing GUI can move the divider till only width = 500.

I was looking at setLastDividerLocation, however, that doesn't seem to work. Can someone please help me with the correct property I need to set for this?

Thanks!

Upvotes: 1

Views: 559

Answers (1)

camickr
camickr

Reputation: 324118

You can add a PropertyChangeListener to the JSplitPane and reset the divider location when it exceeds the limit:

splitPane.addPropertyChangeListener("dividerLocation", new PropertyChangeListener()
{
    @Override
    public void propertyChange(PropertyChangeEvent e)
    {
        int location = ((Integer)e.getNewValue()).intValue();
        System.out.println(location);

        if (location > 400)
        {
            JSplitPane splitPane = (JSplitPane)e.getSource();
            splitPane.setDividerLocation( 400 );
        }
    }
});

Upvotes: 2

Related Questions