2

I'm building a Java swing application that uses JSplitPanes. Sometimes when I'm testing my application, the JSplitPanes jump to their default position after certain actions. I can move the divider to any location, but the certain action always causes the divider to jump to the same position it was when the program first opened.

My code is too large to show samples of, so I would like to know in general what could cause this.

One thing I have tried is taking out a call to revalidate(), and that did stop the JSplitPanes from jumping. However, it caused another system (one that makes our menus dynamic) to stop working altogether.

Is there a way to stop the JSplitPanes from jumping without breaking the dynamic menu system?

shieldgenerator7
  • 1,507
  • 17
  • 22
  • 3
    `My code is too large to show samples of,` - which is why you need to create a [SSCCE](http://sscce.org/) that demonstrates the problem. Chances are while creating the SSCCE you will find the problem. `However, it caused the dynamic menus to stop working altogether` - we have no idea what this is which is why you need to post the SSCCE to demonstrate the concept. The point of a SSCCE is to NOT post your application but for you to think of a simple way to demonstrate your concept that is causing the problem. – camickr Nov 12 '15 at 16:06
  • @camickr thanks for the advice, I'll try this out – shieldgenerator7 Nov 12 '15 at 17:09
  • Multiple things might effect this, but the probably the first thing I'd be investigating is the minimum/maximum sizes of your components – MadProgrammer Nov 12 '15 at 22:46
  • @MadProgrammer Yes, if you look at my answer, setting the preferred size is an acceptable workaround – shieldgenerator7 Nov 12 '15 at 23:45
  • @shieldgenerator7 I'd argue the use of preferredSize without further context to the original question – MadProgrammer Nov 12 '15 at 23:46

2 Answers2

1

So I was unable to find the source of the problem, but I did find a work around that works quite well:

splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, 
            new java.beans.PropertyChangeListener() {
               @Override
               public void propertyChange(java.beans.PropertyChangeEvent pce) {
                  SwingUtilities.invokeLater(
                     new Runnable() {
                        public void run()   {
                           Component left = splitPane.getLeftComponent();
                           Component right = splitPane.getRightComponent();
                           left.setPreferredSize(left.getSize());
                           right.setPreferredSize(right.getSize());
                        }
                     });
               }
            });

Basically, when the JSplitPane gets resized, it sets the preferred sizes of its two inner components, which it then uses to determine the divider location. The invoke later call was also necessary, as it didn't work without it.

I was going to do a SSCCE, but I came up with this idea while thinking about how to go about it, and tested it and it worked.

shieldgenerator7
  • 1,507
  • 17
  • 22
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). In this case you're likely just masking the underlying problem with a band aid fix, but I have no context to your original problem to be sure – MadProgrammer Nov 12 '15 at 23:47
  • @MadProgrammer Yes, I believe this "fix" is just a band aid. But it gets rid of the symptoms, so it's good enough for now. I think that at this point, my application will cause this error until we refactor or take out the dynamic menu system, which requires the use of revalidate(), which causes the split panes to jump. – shieldgenerator7 Nov 13 '15 at 00:19
0

If that is the width and height of your cell causing the jump, then you might be interested in to put your left and right components inside a jscrollpane (set scrolls to hidden).

Normally that happens when there is no enough room for one side, and it re-sizes the parent. I use jscrollpane with hidden scrolls to have more control over my objects. That is my idea. If someone has a better solutions, it may help me as well next time.

Soley
  • 1,716
  • 1
  • 19
  • 33