The problem of JSplitPane by proportional partitioning

Source: Internet
Author: User

JSplitPane seems to be much more difficult than Delphi's Spliter. But swing can easily use a notepad-type text editor to write directly to the layout-based interface code we still tolerate the inconvenience it brings. But the question of the more frequent but no good answers on MSDN when using JSplitPane is how JSplitPane is split proportionally and why Setdividerlocation (double D) doesn't work.

To solve this problem, look at the Java DOC first. The introduction to Setdividerlocation is this:

setdividerlocationpublic void Setdividerlocation (double proportionallocation) sets the position of the separator bar to a percentage of the jsplitpane size.

This method is implemented according to Setdividerlocation (int). This method quickly changes the size of the pane based on the current size of the Splitter pane. If the divider pane is not implemented correctly and does not appear on the screen, this method will have no effect (the new separator position will become 0 (the current size * proportionallocation)).


Parameters: Proportionallocation-Indicates the percentage of double-precision floating-point values, thrown from 0 (Top/left) to 1.0 (Bottom/right): illegalargumentexception-If the specified position is < 0 or > 1.0

After reading no concept ... I just think it's not so straightforward, maybe there's something fishy in it. In particular, "If the divider pane is not implemented correctly and does not appear on the screen, this method will not have any effect" this sentence, no big understanding ...

So go to see the source code of JSplitPane. About Setdividerlocation is roughly as follows:

public void Setdividerlocation (double proportionallocation) {
if (Proportionallocation < 0.0 | |
Proportionallocation > 1.0) {
throw new IllegalArgumentException ("Proportional location must" +
"Be between 0.0 and 1.0.");
}
if (getorientation () = = Vertical_split) {
Setdividerlocation ((int) ((double) (GetHeight ()-getdividersize ()) *
proportionallocation));
} else {
Setdividerlocation ((int) ((double) (GetWidth ()-getdividersize ()) *
proportionallocation));
}
}

This makes sense, setdividerlocation (double) This function uses functions such as getwidth () or getheight (), while the Java Desktop program uses layouts before the main form setvisible. The width and height of each component not yet validate () and paint () is 0 by default. This means that you must use Setdividerlocation (double) after the main form setvisible (true) to be valid.

An example is given below:

Package tlw.zbe.info.stratch;

Import Javax.swing.JFrame;
Import Javax.swing.JSplitPane;
Import Java.awt.BorderLayout;
Import Javax.swing.JPanel;

public class MainFrame extends JFrame {
public static void Main (string[] args) {
MainFrame f=new MainFrame ();
F.setdefaultcloseoperation (jframe.exit_on_close);//Set the form to automatically exit after a process is closed
F.setsize (800,600);//Set the default size of the form
F.setextendedstate (Jframe.maximized_both);//Set the form state to maximize the screen, which is the full screen size.
F.setvisible (TRUE);//Display Form
F.jsplitpane1.setdividerlocation (0.7);//Set the left and right proportions of the split panel (this time it takes effect, if it is placed in setvisible (true), it will not work. )
}
Public MainFrame () {
try {
Jbinit ();
} catch (Exception ex) {
Ex.printstacktrace ();
}
}

private void Jbinit () throws Exception {
This.getcontentpane (). Add (JSplitPane1, Java.awt.BorderLayout.CENTER);
Jsplitpane1.add (JPanel1, jsplitpane.left);
Jsplitpane1.add (JPanel2, jsplitpane.right);
}

JSplitPane jSplitPane1 = new JSplitPane ();
JPanel JPanel1 = new JPanel ();
JPanel JPanel2 = new JPanel ();
}

Very good to see is JBuilder interface code, run it will find JSplitPane already obediently in proportion split. The problem is that when you drag the split after the interface does a maximized scale cannot be maintained. To solve this problem is to add a componentlistener. For example the following:

Package tlw.zbe.info.stratch;

Import Javax.swing.JFrame;
Import Javax.swing.JSplitPane;
Import Java.awt.BorderLayout;
Import Javax.swing.JPanel;
Import java.awt.event.ComponentEvent;
Import Java.awt.event.ComponentAdapter;

public class MainFrame extends JFrame {
public static void Main (string[] args) {
MainFrame f=new MainFrame ();
}
private void Myinit () {
Setdefaultcloseoperation (jframe.exit_on_close);//Set the form to automatically exit after a process is closed
SetSize (800,600);//Set the default size of the form
SetVisible (TRUE);//Display Form
Jsplitpane1.setdividerlocation (0.7);//Set the left and right proportions of the split panel (this time it takes effect, if it is placed in setvisible (true), it will not work. )
/***** Initialize Event ***/
This.addcomponentlistener (New Componentadapter () {
public void componentresized (ComponentEvent e) {
Jsplitpane1.setdividerlocation (0.7);
}
});
}
Public MainFrame () {
try {
Jbinit ();
Myinit ();
} catch (Exception ex) {
Ex.printstacktrace ();
}
}

private void Jbinit () throws Exception {
This.getcontentpane (). Add (JSplitPane1, Java.awt.BorderLayout.CENTER);&NBSP;
        Jsplitpane1.add ( JPanel1, Jsplitpane.left); &NBSP;
        Jsplitpane1.add ( JPanel2, Jsplitpane.right); &NBSP;
   } 

    JSplitPane jSplitPane1 = new JSplitPane ();     JPanel jPanel1 = new JPanel ();     JPanel jPanel2 = new JPanel (); }&NBSP;


See a Myinit () method? This is my personal habit, I write the interface code area in order to divide the generated are placed in the Myinit () below. In general, some interface default values and interface events are initialized. The added Componentlistener determines that the JSplitPane will be split proportionally after changing the interface size arbitrarily. &NBSP;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.