Solved

JSplitPane

Posted on 1998-11-18
6
869 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 39
ForLoop Example 3 49
difference of if loops 23 49
Unhandled exception type Exception 18 31
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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