Results 1 to 6 of 6
  1. #1
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Help! Is it possible to fix the relative position of the divider ?

    Hi everyone,

    Is it possible, no matter what size of component I put into Panel p2, I will always get the ratio of p1 and p2 2:1?

    Java Code:
            JFrame frame=new JFrame();
        	frame.setSize(500,500);
        	JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        	
        	JPanel p1=new JPanel();
        	p1.setBackground(Color.GRAY);
        	JPanel p2=new JPanel();
        	p2.setBackground(Color.BLUE);
        	
        	JButton b2=new JButton();
        	b2.setPreferredSize(new Dimension(300,300));//this causes my problem
        	p2.add(b2);
        	
        	
        	splitPane.setLeftComponent(p1);
        	splitPane.setRightComponent(p2);
        	
        	splitPane.setDividerSize(2);
        	splitPane.setDividerLocation(0.66);
        	splitPane.setResizeWeight(0.66);
        	
        	frame.setContentPane(splitPane);
        	frame.setVisible(true);
    Thank you very much!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,422
    Rep Power
    5

    Default Re: Help! Is it possible to fix the relative position of the divider ?

    Remove the size declarations from the panels and let the layout manager handle them. But first, setPreferredSize of the JFrame. Then just before you set the frame visible, call frame.pack(). Then the panels should be allocated properly no matter what the size of the frame. As you resize the frame with the mouse, the panels should fill up the frame properly.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Help! Is it possible to fix the relative position of the divider ?

    Hi jim,
    thank you very much for your answer. That helps somehow but do not really solve the problem.

    Because by doing what you have coached, the frame can pack the content in the panel(this case, that big button) properly. But if the button is really big(change size to new dimension(800,1200)), the ratio of width between left and right panel can still not be fixed to 2:1.It will take over all the place when the frame is not large enough.

    What I want is no matter what happens the ratio of width between left and right panel is 2:1 :)

  4. #4
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Help! Is it possible to fix the relative position of the divider ?

    Hi jim,
    thank you very much for your answer. That helps somehow but do not really solve the problem.

    Because by doing what you have coached, the frame can pack the content in the panel(this case, that big button) properly. But if the button is really big(change size to new dimension(800,1200)), the ratio of width between left and right panel can still not be fixed to 2:1.It will take over all the place when the frame is not large enough.

    What I want is no matter what happens the ratio of width between left and right panel is 2:1 :)

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,422
    Rep Power
    5

    Default Re: Help! Is it possible to fix the relative position of the divider ?

    Take your originally posted code and set the dividerLocation after you make the frame visible.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Help! Is it possible to fix the relative position of the divider ?

    Quote Originally Posted by jim829 View Post
    Take your originally posted code and set the dividerLocation after you make the frame visible.

    Regards,
    Jim
    thanks jim :)

Similar Threads

  1. Problem with changing the color of divider
    By tianchen in forum AWT / Swing
    Replies: 0
    Last Post: 02-03-2014, 04:30 PM
  2. jSplitPane and divider Location after jFrame resize?
    By michalss in forum AWT / Swing
    Replies: 6
    Last Post: 04-21-2011, 12:33 PM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •