Solved

JSplitPane

Posted on 1998-11-18
6
862 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now