Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSplitPane

Posted on 1998-11-18
6
Medium Priority
?
876 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 120 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

705 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