Solved

JSplitPane

Posted on 1998-11-18
6
866 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

27 Experts available now in Live!

Get 1:1 Help Now