Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Clicking on a JSplitPane divider.

843806Aug 3 2009 — edited Feb 9 2010
Hi,

This may seem like a stupid question but, how can I detect clicks on the Divider of a JSplitPane? I can make it resize programmatically, and I simply want to resize it if it's double clicked on (according to the left component's preferred size).

Many thanks.

Comments

Alan.M
Hi,
since JSplitPane extends JComponent so it will be able to listen to mouse; simply add a mouse listener and invoke what method you want on the listener ( mouse click, moved etc..)
JSplitPane split = new JSplitPane();
		split.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {}
			  // call your method here 
		});
Regards,
Alan Mehio
London, UK
843806
Hi Alan,

Thanks for the swift reply. This is exactly what I thought and tried, however no events are being sent when I click on the Divider. Can you try and see if yours is giving you events for the divider clicks?

Regards,

Bob.
camickr
Try adding the MouseListener to the divider:
BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
ui.getDivider().addMouseListener(...);
843806
Thanks for that, it works great.

On a side issue is there any chance that the casting to BasicSplitPaneUI may fail?
843807
I do it this way:
// frame class fields
protected int splitPaneDefaultDividerLocation = -1;
protected JSplitPane splitPane;

// inside method for initialization of frame components,
// after splitPane is created and added to the frame content pane

    SplitPaneUI spui = splitPane.getUI();
    if (spui instanceof BasicSplitPaneUI) {
      // Setting a mouse listener directly on split pane does not work, because no events are being received.
      ((BasicSplitPaneUI) spui).getDivider().addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
          if (e.getClickCount() > 1) {
            if (splitPane.getDividerLocation() != splitPaneDefaultDividerLocation) {
              splitPane.setDividerLocation(splitPaneDefaultDividerLocation);
            } else {
              splitPane.setDividerLocation(0);
            }
          }
        }
      });
    }

// splitPane.getDividerLocation() returns last value passed to splitPane.setDividerLocation()
// so at this point it returns -1 and cannot be used

    pack(); // Layout the components.
    splitPaneDefaultDividerLocation = splitPane.getUI().getDividerLocation(splitPane);
PhHein
Welcome to the forum. Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

I'm locking this thread now.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 9 2010
Added on Aug 3 2009
6 comments
1,469 views