Use of the swing control jsplitpane (common methods)

Source: Internet
Author: User

1. The Swing split window control jsplitpane is used to split the window into two parts.

2. Only one control can be placed in each split window. If you want to add multiple controls, you can create one jpane panel on the top so that multiple controls can be created.

3. jsplitpane myjsplitpane = new jsplitpane (INT neworientaion, Boolean new continuouslayout );

Neworientaion: the optional value is jsplitpane. horizontal_split.

Jsplitpane, vertucal_split

4. Common Methods

① Setdividersize (INT size): set the size of the split entry.

② Getdividersize () to get the size of the split.

③ Setdividerlocation (INT size) sets the split position according to the percentage.

④ Getorientation.

5. Use jsplitpane to listen for window zoom events

Jsplitpane splitpane = new javax. swing. jsplitpane (); splitpane. addpropertychangelistener (New Java. beans. propertychangelistener () {public void propertychange (Java. beans. propertychangeevent EVT) {If (EVT. getpropertyname (). equals (jsplitpane. divider_location_property) {// Action Code }}});

6. jsplitpane proportional Segmentation

Jsplitpane looks better than Delphi's spliter
It is difficult to use. However, swing can easily use Notepad text editors to directly write the layout-based interface. Code We should tolerate the inconvenience it brings. However
Jsplitpane is frequently asked on msdn but has no good answer. The question is how jsplitpane is divided proportionally.
(Http://www.my400800.cn) cut, setdividerlocation (double D) Why does not work.

To solve this problem, first see Java Doc. The introduction to setdividerlocation is as follows:
Setdividerlocation

Public void setdividerlocation (double proportionallocation)

Set the separator to a percentage of the size of jsplitpane.

According to setdividerlocation (INT) To implement this method. This method rapidly changes the size of the pane based on the current size of the separated pane. If the separation pane is not correctly implemented and is not displayed on the screen, this method will not have any impact (the new separation bar position will be 0 (the current size * proportionallocation )).

Parameters:
Proportionallocation-double-precision floating point value indicating percentage, from 0 (top/left) to 1.0 (bottom/right)
Throw:
Illegalargumentexception-if the specified location is <0 or> 1.0

There is no concept after reading it... I just thought it was not so straightforward. Maybe it was tricky. In particular, this method does not have any impact if the separation pane is not correctly implemented and is not displayed on the screen...

So let's look at the source code of jsplitpane. The 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 ));}}

Now I understand that the setdividerlocation (double) function will use getwidth () or
Getheight () and Java DesktopProgramIf layout is used before the main form setvisible is available, no validate () or paint ()

The width and height of each component are 0 by default. In other words, it is necessary to use setdividerlocation (double) after the main form setvisible (true)
.

The following is an example:

Package tl1_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. setdefaclocloseoperation (jframe. exit_on_close); // sets the process to exit automatically after the form is closed. setsize (800,600); // set the default size of the form. setextendedstate (jframe. maximized_both); // sets the form status to maximize the screen, That is, the full screen size. F. setvisible (true); // display the Form F. jsplitpane1.setdividerlocation (0.7); // set the ratio of left and right sides of the split panel (this takes effect now. It will not work until it is placed in setvisible (true .)} 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 ();}

it is a JBuilder interface code. Running jsplitpane, you can see that jsplitpane is split proportionally. The problem is that the ratio cannot be maintained after the interface is maximized after the split is dragged. To solve this problem, add a componentlistener. Example:

Package tl1_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 () {setdefaclocloseoperation (jframe. exit_on_close); // set After the form is closed, the process is automatically exited. setsize (800,600); // set the default size of the form. setvisible (true); // display the form jsplitpane1.setdividerlocation (0.7 ); // set the ratio of left and right sides of the split panel (this takes effect now. It will not work if it is placed before setvisible (true .) /***** Initialization 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); jsplitpane1.add (jpanel1, jsplitpane. left); jsplitpane1.add (jpanel2, jsplitpane. right);} jsplitpane jsplitpane1 = new jsplitpane (); jpanel jpanel1 = new jpanel (); jpanel jpanel2 = new jpanel ();}

Is there a myinit () method? This is my personal habit. The interface code area I wrote should be placed under myinit () in order to be generated. In general, some interface default values and interface events are initialized. The added componentlistener determines that jsplitpane will be split proportionally after any interface size is changed.

 

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.