|
|
Listing 13.2 The BoxLayoutTest Application
package com.foley.test;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import com.foley.utility.ApplicationFrame;
/**
* An application that displays a set of three labels in
* a panel on the left side of the window. Three sliders
* are shown on the right. Each sliders controls one of
* the alignment properties of the corresponding label.
* If the layout is X or Y oriented determines the property
* to control.
* <p>
* As the sliders are moved, the label panel is revalidated
* to show the new layout.
*
* @author Mike Foley
**/
public class BoxLayoutTest extends JPanel
implements ChangeListener {
/**
* If true, the layout is horizontal, if false, vertical.
**/
private boolean horizontal;
/**
* The sliders that control the alignment property.
**/
protected JSlider slider1;
protected JSlider slider2;
protected JSlider slider3;
/**
* The labels whos alignment property is being set.
**/
protected JLabel label1;
protected JLabel label2;
protected JLabel label3;
protected JPanel labelPanel;
/**
* Create the component is the display.
* Use a BoxLayout to arrange the nested panels.
* This class acts as a mediator between the sliders
* and the labels.
**/
public BoxLayoutTest( boolean horizontal ) {
this.horizontal = horizontal;
BoxLayout boxLayout = new BoxLayout( this, BoxLayout.X_AXIS );
setLayout( boxLayout );
labelPanel = createLabelPanel();
labelPanel.setBorder( BorderFactory.createLoweredBevelBorder() );
add( new JScrollPane( labelPanel ) );
add( Box.createGlue() );
JPanel sliders = createSliderPanel();
add( sliders );
}
/**
* Create the labels used in this demo.
**/
protected JPanel createLabelPanel() {
JPanel panel = new JPanel();
BoxLayout boxLayout;
if( isHorizontal() )
boxLayout = new BoxLayout( panel, BoxLayout.X_AXIS );
else
boxLayout = new BoxLayout( panel, BoxLayout.Y_AXIS );
panel.setLayout( boxLayout );
label1 = new JLabel( Label 1 );
label1.setOpaque( true );
label1.setBackground( Color.yellow );
panel.add( label1 );
label2 = new JLabel( Label 2 );
label2.setOpaque( true );
label2.setBackground( Color.blue );
panel.add( label2 );
label3 = new JLabel( Label 3 );
label3.setOpaque( true );
label3.setBackground( Color.green );
panel.add( label3 );
return( panel );
}
/**
* @return True if the alignment is horizontal.
**/
public boolean isHorizontal() {
return( horizontal );
}
/**
* Create the sliders.
* Each slider contains the range from 0-100 that
* will be mapped to 0 - 1.0 for the label alignment.
* <p>
* This method assumes the labels are already created.
* Set the slider background to match the labels background
* that the slider adjust.
**/
protected JPanel createSliderPanel() {
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout( panel, BoxLayout.Y_AXIS );
panel.setLayout( boxLayout );
slider1 = new JSlider();
slider1.setPaintLabels( true );
slider1.setPaintTicks( true );
slider1.setMajorTickSpacing( 20 );
slider1.setMinorTickSpacing( 5 );
slider1.setBackground( Color.yellow );
if( isHorizontal() )
slider1.setValue( ( int )( label1.getAlignmentY() * 100 ) );
else
slider1.setValue( ( int )( label1.getAlignmentX() * 100 ) );
panel.add( slider1 );
slider1.addChangeListener( this );
slider2 = new JSlider();
slider2.setPaintLabels( true );
slider2.setPaintTicks( true );
slider2.setMajorTickSpacing( 20 );
slider2.setMinorTickSpacing( 5 );
slider2.setBackground( Color.blue );
slider2.setForeground( Color.white );
if( isHorizontal() )
slider2.setValue( ( int )( label1.getAlignmentY() * 100 ) );
else
slider2.setValue( ( int )( label1.getAlignmentX() * 100 ) );
panel.add( slider2 );
slider2.addChangeListener( this );
slider3 = new JSlider();
slider3.setPaintLabels( true );
slider3.setPaintTicks( true );
slider3.setMajorTickSpacing( 20 );
slider3.setMinorTickSpacing( 5 );
slider3.setBackground( Color.green );
if( isHorizontal() )
slider3.setValue( ( int )( label1.getAlignmentY() * 100 ) );
else
slider3.setValue( ( int )( label1.getAlignmentX() * 100 ) );
panel.add( slider3 );
slider3.addChangeListener( this );
return( panel );
}
/**
* One of the sliders has changed. See which
* one and update the alignment property of
* the corresponding label.
**/
public void stateChanged( ChangeEvent e ) {
Object source = e.getSource();
if( source == slider1 ) {
int value = slider1.getValue();
if( isHorizontal() )
label1.setAlignmentY( ( float )value / ( float )100.0 );
else
label1.setAlignmentX( ( float )value / ( float )100.0 );
} else if( source == slider2 ) {
int value = slider2.getValue();
if( isHorizontal() )
label2.setAlignmentY( ( float )value / ( float )100.0 );
else
label2.setAlignmentX( ( float )value / ( float )100.0 );
} else if( source == slider3 ) {
int value = slider3.getValue();
if( isHorizontal() )
label3.setAlignmentY( ( float )value / ( float )100.0 );
else
label3.setAlignmentX( ( float )value / ( float )100.0 );
}
//
// Force the label panel to layout the labels.
//
labelPanel.revalidate();
}
/**
* Application entry point.
* Create the panels and display them.
* <p>
* The application takes a single parameter,
* horizontal. If this is not specified, or no
* parameter is specified, vertical alignment
* is used. If more parameters are specified, they
* are ignored.
* <p>
* @param args vertical for vertical alignment.
**/
public static void main( String args[] ) {
boolean horizontal = false;
//
// Parse the single possible parameter.
//
if( 0 < args.length ) {
String param = args[0].toUpperCase();
if( param.equals( HORIZONTAL ) )
horizontal = true;
}
JFrame frame = new ApplicationFrame( BoxLayoutTest );
JPanel panel = new BoxLayoutTest( horizontal );
panel.setBorder( BorderFactory.createLoweredBevelBorder() );
frame.getContentPane().add( panel, BorderLayout.CENTER );
frame.pack();
frame.setVisible( true );
} // main
} // BoxLayoutTest
|