View Javadoc
1   package com.jsql.view.swing.util;
2   
3   import javax.swing.*;
4   import javax.swing.border.Border;
5   import javax.swing.plaf.basic.BasicSplitPaneDivider;
6   import javax.swing.plaf.basic.BasicSplitPaneUI;
7   import java.awt.*;
8   
9   public class JSplitPaneWithZeroSizeDivider extends JSplitPane {
10  
11      private static final int DIVIDER_DRAG_OFFSET = 4;
12  
13      public JSplitPaneWithZeroSizeDivider(int i) {
14          super(i);
15          this.setDividerSize(1);
16          this.setContinuousLayout(true);
17      }
18  
19      @Override
20      public void doLayout() {
21          super.doLayout();
22  
23          // increase divider width or height
24          BasicSplitPaneDivider divider = ((BasicSplitPaneUI) this.getUI()).getDivider();
25          Rectangle bounds = divider.getBounds();
26          int dividerDragSize = 9;
27          if (this.orientation == JSplitPane.HORIZONTAL_SPLIT) {
28              bounds.x -= JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET;
29              bounds.width = dividerDragSize;
30          } else {
31              bounds.y -= JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET;
32              bounds.height = dividerDragSize;
33          }
34          divider.setBounds(bounds);
35      }
36  
37      @Override
38      public void updateUI() {
39          this.setUI(new SplitPaneWithZeroSizeDividerUI());
40          this.revalidate();
41      }
42  
43      private static class SplitPaneWithZeroSizeDividerUI extends BasicSplitPaneUI {
44  
45          @Override
46          public BasicSplitPaneDivider createDefaultDivider() {
47              return new ZeroSizeDivider(this);
48          }
49  
50          @Override
51          protected void installDefaults() {
52              super.installDefaults();
53  
54              // make sure that divider is first component (and can overlap left/right components)
55              if (this.splitPane.getComponent(0) != this.divider) {
56                  this.splitPane.setLeftComponent(this.splitPane.getLeftComponent());
57                  this.splitPane.setRightComponent(this.splitPane.getRightComponent());
58              }
59          }
60      }
61  
62      private static class ZeroSizeDivider extends BasicSplitPaneDivider {
63  
64          public ZeroSizeDivider(BasicSplitPaneUI ui) {
65              super(ui);
66              super.setBorder(null);
67              this.setBackground(UIManager.getColor("controlShadow"));
68          }
69  
70          @Override
71          public void setBorder(Border border) {
72              // ignore
73          }
74  
75          @Override
76          public void paint(Graphics g) {
77              g.setColor(this.getBackground());
78              if (this.orientation == JSplitPane.HORIZONTAL_SPLIT) {
79                  g.drawLine(JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET, 0, JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET, this.getHeight() - 1);
80              } else {
81                  g.drawLine(0, JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET, this.getWidth() - 1, JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET);
82              }
83          }
84  
85          @Override
86          protected void dragDividerTo(int location) {
87              super.dragDividerTo(location + JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET);
88          }
89  
90          @Override
91          protected void finishDraggingTo(int location) {
92              super.finishDraggingTo(location + JSplitPaneWithZeroSizeDivider.DIVIDER_DRAG_OFFSET);
93          }
94      }
95  }