Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 8. (The basics) Split Panes. Easy for reading, Click here!

Swing Chapter 8. (The basics) Split Panes. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/3 


Previous Page Previous Page (1/3) - Next Page (3/3) Next Page
Subpages: 1. Split Panes: JSplitPane 
2. 
Basic split pane example 
3. Gas model simulation using a split pane 

8.2    Basic split pane example

This basic introductory demo shows JSplitPane at work. We can manipulate four simple custom panels placed in three JSplitPanes:

Figure 8.1 Split Pane example displaying simple custom panels.

<<file figure8-1.gif>>

The Code: SplitSample.java

see \Chapter8\1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplitSample extends JFrame{
  protected JSplitPane m_sp;
  public SplitSample() {
    super("Simple SplitSample Example");
    setSize(400, 400);
    Component c11 = new SimplePanel();
    Component c12 = new SimplePanel();
    JSplitPane spLeft = new JSplitPane(
          JSplitPane.VERTICAL_SPLIT, c11, c12);
    spLeft.setDividerSize(8);
    spLeft.setContinuousLayout(true);
    Component c21 = new SimplePanel();
    Component c22 = new SimplePanel();
    JSplitPane spRight = new JSplitPane(
          JSplitPane.VERTICAL_SPLIT, c21, c22);
    spRight.setDividerSize(8);
    spRight.setContinuousLayout(true);
    m_sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
         spLeft, spRight);
    m_sp.setContinuousLayout(false);
    m_sp.setOneTouchExpandable(true);
    getContentPane().add(m_sp, BorderLayout.CENTER);
    WindowListener wndCloser = new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
            System.exit(0);
      }
    };

    addWindowListener(wndCloser);
    setVisible(true);
  }

  public static void main(String argv[]) {
    new SplitSample();
  }
}

class SimplePanel extends JPanel{
  public Dimension getPreferredSize() {
    return new Dimension(200, 200);
  }

  public Dimension getMinimumSize() {
    return new Dimension(40, 40);
  }

  public void paintComponent(Graphics g) {
    g.setColor(Color.black);
    Dimension sz = getSize();
    g.drawLine(0, 0, sz.width, sz.height);
    g.drawLine(sz.width, 0, 0, sz.height);
  }
}

Understanding the Code

Class SplitSample

Four instances of SimplePanel (see below) are used to fill a 2x2 structure. Two left components (c11 and c12) are placed in the vertically split spLeft panel. The two right components (c21 and c22) are placed in the vertically split spRight panel. The spLeft and spRight panels are placed in the horizontally split m_sp panel.

Several properties are assigned to demonstrate JSplitPane's behavior. The continuousLayout property is set to true for spLeft and spRight, and false for m_sp panel. So as the divider moves inside the left and right panels, child components are repainted continuously, producing immediate results. However, as the vertical divider is moved it is denoted by a black line until a new position is chosen (i.e. the mouse is released). Only then are its child components validated and repainted. The first kind of behavior is recommended for simple components that can be rendered quickly, while the second is recomended for components whose repainting can take a significant amount of time.

Also note that the oneTouchExpandable property is set to true for the vertical JSplitPane. This places small arrow widgets on the divider. By pressing these arrows with the mouse we can instantly move the divider to the leftmost or rightmost position. When in the left or right-most positions, pressing these arrows will then move the divider to its most recent location, maintained by the lastDividerLocation property.

Class SimplePanel

SimplePanel represents a simple Swing component to be used in this example. Method paintComponent() draws two diagonal lines across this component. Note that the overridden getMinimumSize() method defines the minimum space required for this component. JSplitPane will prohibit the user from moving the divider if the resulting child size will become less than its minimum size.

Note: The arrow widgets associated with the oneTouchExpandable property will move the divider to the extreme location without regard to minimum sizes of child components.

Running the Code

Note how child components can be resized with dividers. Also note the difference between resizing with continuous layout (side panes) and without it (center pane). Play with the "one touch expandable" widgets for quick expansion and collapse.



[ Return to Swing (Book) ]


Search Books
Custom Search
Latest articles
 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 2


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy