javaswingjsplitpane

SplitPane vertical don't working


I have been working with jsplit pane in java swing and i am trying to set horizontal and vertical split pane in one main frame,first to divide main frame on left and right panel,and then that right panel to divide into bottom and top panel.I succeeded to divide main frame into left and right panel,but after that it doesn't divide well into bottom and top panel,it splits that right panel,and on it's right half it splits into bottom and top..Can someone help me to solve this?Thanks in advance
Code for split panes:

split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll, panel);
add(split, BorderLayout.CENTER);
split.setDividerLocation(250);
panel.setBackground(Color.RED);


JPanel gPanel=new JPanel();
gPanel.setMinimumSize(new Dimension(30,30));
JPanel dPanel=new JPanel();
dPanel.setMinimumSize(new Dimension(30,30));
split2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,gPanel,dPanel);
panel.add(split2,BorderLayout.CENTER);
split2.setDividerLocation(250);
gPanel.setBackground(Color.BLUE.brighter());

Solution

  • If I understand correctly, then you need to nest split panes so the code would be something like:

    JPanel top = new JPanel();
    JPanel bottom = new JPanel();
    JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top, bottom);
    JPanel left = new JPanel();
    JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
    frame.add(horizontal, BorderLayout.CENTER);