Question about using JSplitPane

My program looks as follow. But I have a problem: if the program started, the JSplitPane will not resized as I want till I click the dividiton-bar. How can I solve such a problem?

public class MyTest extends JFrame {

  private javax.swing.JPanel jContentPane = null;

  private javax.swing.JSplitPane jSplitPane = null;
  public static void main(String[] args) {
      new MyTest().show();
  }

  public MyTest() {
    super();
    initialize();
  }

  private void initialize() {
    this.setContentPane(getJContentPane());
    this.setSize(500, 399);
    this.setContentPane(getJContentPane());
    this.setTitle("Test");
    this.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowOpened(java.awt.event.WindowEvent e) {    
       updateSize(); // TODO Auto-generated Event stub windowOpened()
    }
  });
}

  protected void updateSize() {
  // TODO Automatisch erstellter Methoden-Stub
  jSplitPane.setSize(this.getWidth(), this.getHeight());
  }

  private javax.swing.JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new javax.swing.JPanel();
      jContentPane.setLayout(null);
      jContentPane.add(getJSplitPane(),null);
    }
    return jContentPane;
  }

  private javax.swing.JSplitPane getJSplitPane() {
    if(jSplitPane == null) {
    jSplitPane = new javax.swing.JSplitPane();
    jSplitPane.setBounds(6, 26, 410, 182);
    }
    return jSplitPane;
  }
}
chenweiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
These JSplitPane functions will help you:
- void setResizeWeight(double value): Specifies how to distribute extra space when the size of the split pane changes.
-  void setDividerLocation(double proportionalLocation): Sets the divider location as a percentage of the JSplitPane's size.
 
0
zzynxSoftware engineerCommented:
>> the JSplitPane will not resized as I want till I click the dividiton-bar
How do you want it to behave?
0
chenweiAuthor Commented:
what i want is: if i start the program, the jsplitpane will has the full size as the jframe.

you can run my program and u can see what happens.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

girionisCommented:
> jSplitPane.setSize(this.getWidth(), this.getHeight());

I am not sure if I understand your problem correctly but why are you doing this? Why do you resize the split pane every time there is action in the window? You probably do not need to do this.

0
girionisCommented:
> what i want is: if i start the program, the jsplitpane will has the
> full size as the jframe.

> you can run my program and u can see what happens.

Try to get rid of the line above (jSplitPane.setSize(this.getWidth(), this.getHeight());) and tell us if this si what you are looking for.
0
zzynxSoftware engineerCommented:
Replace

>>jContentPane.setLayout(null);
>>jContentPane.add(getJSplitPane(),null);

by

      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
0
zzynxSoftware engineerCommented:
So:

/*
 * MyTest.java
 *
 */

import javax.swing.*;
import java.awt.*;

public class MyTest extends JFrame {

  private javax.swing.JPanel jContentPane = null;

  private javax.swing.JSplitPane jSplitPane = null;
  public static void main(String[] args) {
     MyTest test = new MyTest();
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     test.show();
  }

  public MyTest() {
    super();
    initialize();
  }

  private void initialize() {
    this.setContentPane(getJContentPane());
    this.setSize(500, 399);
    this.setContentPane(getJContentPane());
    this.setTitle("Test");
}

  private javax.swing.JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new javax.swing.JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  private javax.swing.JSplitPane getJSplitPane() {
    if(jSplitPane == null) {
    jSplitPane = new javax.swing.JSplitPane();
    jSplitPane.setBounds(6, 26, 410, 182);
    }
    return jSplitPane;
  }
}
0
chenweiAuthor Commented:
you are right,if i use the boderlayout, it will be ok. but if i don't use the borderlayout, how can i deal with such problem?

logically if i call the function updateSize(), the size must be rsized. but now it's not the case.

0
zzynxSoftware engineerCommented:
>> but if i don't use the borderlayout, how can i deal with such problem?
Why would you do it yourself?
That's just what layout managers like the BorderLayout are made for.
0
zzynxSoftware engineerCommented:
But to answer your question:
use a ComponentListener and take action in its function void componentResized(ComponentEvent e)
0
chenweiAuthor Commented:
>>Why would you do it yourself?
this is another question. my question is why my function-call doesn't work.
0
zzynxSoftware engineerCommented:
>> this is another question.
This was your initial Q:
>> How can I solve such a problem?
;°))
0
chenweiAuthor Commented:
thanks to all. i solve my problem now. i use the updateUI() of JSplitPane. :-)


0
zzynxSoftware engineerCommented:
So, no longer interested in another solution?
0
chenweiAuthor Commented:
hi zzynx, if you have another better solution, i will give u the points. :-)
0
zzynxSoftware engineerCommented:
/*
 * MyTest.java
 *
 */

import javax.swing.*;
import java.awt.*;

public class MyTest extends JFrame {

  private javax.swing.JPanel jContentPane = null;

  private javax.swing.JSplitPane jSplitPane = null;
  public static void main(String[] args) {
     MyTest test = new MyTest();
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     test.show();
  }

  public MyTest() {
    super();
    initialize();
  }

  private void initialize() {
    this.setContentPane(getJContentPane());
    this.setSize(500, 399);
    this.setContentPane(getJContentPane());
    this.setTitle("Test");
    this.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(java.awt.event.ComponentEvent e) {
           updateSize();
        }
    });
    this.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(java.awt.event.WindowEvent e) {    
           updateSize();
        }
    });
  }

  protected void updateSize() {
        Dimension d = new Dimension(this.getWidth(), this.getHeight());
        jSplitPane.setPreferredSize(d);
        getJContentPane().revalidate();
        getJContentPane().repaint();
  }

  private javax.swing.JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new javax.swing.JPanel();
      jContentPane.setLayout(new FlowLayout());
      jContentPane.add(getJSplitPane());
    }
    return jContentPane;
  }

  private javax.swing.JSplitPane getJSplitPane() {
    if(jSplitPane == null) {
        jSplitPane = new javax.swing.JSplitPane();
    }
    return jSplitPane;
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
I mean this one
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.