问题
I have a JSplitPane which when shown should split the pane by 50%.
Now on giving an argument of 0.5 (as suggested) to setDividerLocation, Java seems to treat it as a normal number instead of a percentage. As in, the divider, instead of going to the middle of the pane, is almost at the start of the left pane (the pane is vertically split). Any work arounds?
回答1:
Am I missing something? There seem to be a lot of rather convoluted answers to this question... but I think a simple setResizeWeight(0.5) would solve the issue ... it's described in the SplitPane Tutorial
回答2:
The setDividerLocation( double ) method only works on a "realized" frame, which means after you've packed or made the frame visible.
The setDividerLocation( int ) method can be used at any time.
回答3:
You can only set the split pane divider's location as a percentage when the split pane is visible. You can tap into the split pane owner's events to determine when its OK to set the divider's location. For example:
public class MyFrame extends JFrame {
public MyFrame() {
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
// ... set up the split pane, and add to the frame ...
// Listen for the frame's "shown" event.
addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent componentEvent) {
// Set the divider location to 20%.
// This works because we know the pane is visible.
splitPane.setDividerLocation(.2);
// Stop listening for "shown" events.
removeComponentListener(this);
}
});
pack();
}
}
回答4:
this fixes it:
public class JSplitPane3 extends JSplitPane {
private boolean hasProportionalLocation = false;
private double proportionalLocation = 0.5;
private boolean isPainted = false;
public void setDividerLocation(double proportionalLocation) {
if (!isPainted) {
hasProportionalLocation = true;
this.proportionalLocation = proportionalLocation;
} else {
super.setDividerLocation(proportionalLocation);
}
}
public void paint(Graphics g) {
super.paint(g);
if (!isPainted) {
if (hasProportionalLocation) {
super.setDividerLocation(proportionalLocation);
}
isPainted = true;
}
}
}
回答5:
If you're happy for the divider to move to the middle every time you resize the pane, you could add a ComponentListener and have its componentResized method call setDividerLocation(0.5).
回答6:
This works for me, based on Dave Rays link.
/**
* Set JSplitPane proportional divider location
*
* @param jsplitpane JSplitPane to set
* @param proportionalLocation double <0.0; 1.0>
*/
public static void setJSplitPaneDividerLocation(final JSplitPane jsplitpane, final double proportionalLocation)
{
if (jsplitpane.isShowing())
{
if (jsplitpane.getWidth() > 0 && jsplitpane.getHeight() > 0)
{
jsplitpane.setDividerLocation(proportionalLocation);
}
else
{
jsplitpane.addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent ce)
{
jsplitpane.removeComponentListener(this);
setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
}
});
}
}
else
{
jsplitpane.addHierarchyListener(new HierarchyListener()
{
@Override
public void hierarchyChanged(HierarchyEvent e)
{
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && jsplitpane.isShowing())
{
jsplitpane.removeHierarchyListener(this);
setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
}
}
});
}
}
回答7:
use setResizeWeight(double);
回答8:
I found the combination of setResizeWeight
and setDividerLocation
invoked later provides the behavior one would expect:
_splitPane.setResizeWeight(0.5);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_splitPane.setDividerLocation(0.5);
}
});
回答9:
There's a solution here that is a simple function that doesn't require sub-classing or any other changes to your splitpane.
回答10:
The following in the Component
with the JSplitPane
does it too:
@Override
public void setVisible(final boolean b) {
super.setVisible(b);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AppFrame.this.jSplitPane.setDividerLocation(0.9d);
}
});
}
来源:https://stackoverflow.com/questions/1879091/jsplitpane-setdividerlocation-problem