Solved

JSplitPane

Posted on 1998-11-18
6
873 Views
Last Modified: 2013-11-23
The following is a compilable program. WHat the program to to is to show a frame with a split window. Both the left and right one should have the same width and hight. On the left side there is a split window again. The upper and the lower window should also have the same width and hight. But as I start the program, one can see the width of the left and right one are not the same. The worse is, one can't see the upper or lower half on the left split panel. Why?

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
public class Split2 extends JSplitPane
{
  static  JFrame          frame;

  public Split2()
  {
    super(HORIZONTAL_SPLIT);
    JPanel leftPanel=new JPanel(false);
    JSplitPane leftSplit=new JSplitPane(VERTICAL_SPLIT);
   
    leftSplit.setTopComponent(new JTable());
    leftSplit.setBottomComponent(new JTable());
    leftSplit.setDividerLocation(0.5);
    leftSplit.setDividerSize(5);
    leftPanel.add(leftSplit);

    // Add the left-side and right-side panels to split pane
       
        this.setLeftComponent(leftPanel);
    this.setRightComponent(new JTable());

    // Set the initial location and size of the divider
        this.setDividerLocation(0.5);        this.setDividerSize(10);
  }

  public static void main(String s[])
  {
     WindowListener l = new WindowAdapter()
     {
        public void windowClosing(WindowEvent e) {System.exit(0);}
     };
     frame = new JFrame("Split (client)");
     frame.addWindowListener(l);               frame.getContentPane().add("Center", new Split2());         frame.setSize(700,400);
     frame.pack();
    frame.setVisible(true);
  }
};

0
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 30 total points
ID: 1227688
there are two problems in your code
1.) using NULL layout in leftPanel
2.) adding leftSplit in leftPanel (don't know why this is a problem - haven't worked with JSplitPanes at all)
 (also empty JTables are strange components ... are you sure you can see them)

Ok this is the working code :

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    import com.sun.java.swing.*;
    import com.sun.java.swing.event.*;
    public class Split2 extends JSplitPane
    {
      static  JFrame          frame;

      public Split2()
      {
        super(HORIZONTAL_SPLIT);
        JPanel leftPanel=new JPanel();
      leftPanel.setLayout(new FlowLayout());

        JSplitPane leftSplit=new JSplitPane(VERTICAL_SPLIT);
        leftSplit.setTopComponent(new JLabel("Top Left"));
        leftSplit.setBottomComponent(new JLabel("Top Left"));
        leftSplit.setDividerLocation(0.5);
        leftSplit.setDividerSize(5);
//        leftPanel.add(leftSplit);

        // Add the left-side and right-side panels to split pane
             
//        this.setLeftComponent(leftPanel);
        this.setLeftComponent(leftSplit);
        this.setRightComponent(new JLabel("right"));

           this.setDividerLocation(0.5);      
        this.setDividerSize(10);
      }

      public static void main(String s[])
      {
         WindowListener l = new WindowAdapter()
         {
            public void windowClosing(WindowEvent e) {System.exit(0);}
         };
         frame = new JFrame("Split (client)");
         frame.addWindowListener(l);               frame.getContentPane().add("Center", new
    Split2());         frame.setSize(700,400);
         frame.pack();
        frame.setVisible(true);
      }
    };

Any other problems ?
  heyhey
0
 

Author Comment

by:chenwei
ID: 1227689
To heyhey:

thanks for your answer. I do want to put a table in the split window, not a button. So I've changed the buttons to table again. But as I start the program, one gets a big window. How can I set the frame to pass the table?

   import java.awt.*;
        import java.awt.event.*;
        import java.util.*;

        import com.sun.java.swing.*;
        import com.sun.java.swing.event.*;
        public class Split2 extends JSplitPane
        {
          static  JFrame          frame;

          public Split2()
          {
            super(HORIZONTAL_SPLIT);

            JSplitPane leftSplit=new JSplitPane(VERTICAL_SPLIT);
            leftSplit.setTopComponent(new JScrollPane(new JTable(5,3)));
            leftSplit.setBottomComponent(new JScrollPane(new JTable(5,3)));
            leftSplit.setDividerLocation(0.5);
            leftSplit.setDividerSize(5);

            // Add the left-side and right-side panels to split pane
                 
            this.setLeftComponent(leftSplit);
            this.setRightComponent(new JScrollPane(new JTable(5,3)));

               this.setDividerLocation(0.5);      
      this.setDividerSize(10);
          }

          public static void main(String s[])
          {
             WindowListener l = new WindowAdapter()
             {
                public void windowClosing(WindowEvent e) {System.exit(0);}
             };
             frame = new JFrame("Split (client)");
             frame.addWindowListener(l);               frame.getContentPane().add("Center", new
        Split2());         frame.setSize(700,400);
             frame.pack();
            frame.setVisible(true);
          }
        };
0
 

Author Comment

by:chenwei
ID: 1227690
Besides, I've set the frame size (700,400). But the frame shown is much larger. Why?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 16

Expert Comment

by:heyhey_
ID: 1227691
I will test your code at home ... (don't have Swing at the moment) ... but it seems Ok - probably JTables are just too Big ???
 
>> Besides, I've set the frame size (700,400). But the frame shown is much larger. Why?

because you use LayoutManager.  frame.pack(); tells the layout manager that he must repack the components so the layout manager asks every component what size it is ... and so you receive much bigger frame. you can throw frame.pack() away and see the result ...

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227692
from JSplitPane docs
'When the user is resizing the Components the minimum size of the Components is used to determine the maximum/minimum position the Components can be set to. So that if the minimum size of the two components is greater than the size of the splitpane the divider will not allow you to resize it. Refer to JComponent.setMinimumSize for a way to alter the minimum size of a JComponent. '

you have to adjust the minimum / preferrred size of all your components ...
so try this code :



              import java.awt.*;
              import java.awt.event.*;
              import java.util.*;

              import com.sun.java.swing.*;
              import com.sun.java.swing.event.*;
              public class Split2 extends JSplitPane
              {
                static  JFrame          frame;

                public Split2()
                {
                  super(HORIZONTAL_SPLIT);

                  JSplitPane leftSplit=new JSplitPane(VERTICAL_SPLIT);
            JScrollPane pane1 = new JScrollPane(new JTable(5,3));
            JScrollPane pane2 = new JScrollPane(new JTable(5,3));
            pane1.setMinimumSize(new Dimension(50,50));
            pane1.setPreferredSize(new Dimension(50,50));
            pane2.setMinimumSize(new Dimension(50,50));
            pane2.setPreferredSize(new Dimension(50,50));
                leftSplit.setTopComponent(pane1);
                leftSplit.setBottomComponent(pane2);
                  leftSplit.setDividerLocation(0.5);
                        leftSplit.setDividerSize(5);

                  // Add the left-side and right-side panels to split pane
                         
                  this.setLeftComponent(leftSplit);
            JScrollPane pane3 = new JScrollPane(new JTable(5,3));
            pane3.setMinimumSize(new Dimension(50,50));
            pane3.setPreferredSize(new Dimension(50,50));
                  this.setRightComponent(pane3);

                     this.setDividerLocation(0.5);        
            this.setDividerSize(10);
                }

                public static void main(String s[])
                {
                   WindowListener l = new WindowAdapter()
                   {
                      public void windowClosing(WindowEvent e) {System.exit(0);}
                   };
                   frame = new JFrame("Split (client)");
                   frame.addWindowListener(l);               frame.getContentPane().add("Center", new
              Split2());
//        frame.setSize(700,400);
                   frame.pack();
                  frame.setVisible(true);
                }
              };
0
 

Author Comment

by:chenwei
ID: 1227693
thanks a lot
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question