Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

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;
  }
}
0
chenwei
Asked:
chenwei
  • 10
  • 5
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
zzynxSoftware engineerCommented:
I mean this one
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now