Lecture 10 - September 21, 2010

From MCS Wiki
Jump to: navigation, search

Contents

Chapter 14 continued

Event Handlers Continued

Involves 3 Steps:

  • create the class that contains the event handler (nested class)
  • Implement an appropriate interface, known as an event-listener interface, in the class you created
  • Indicate that an object of the class from the two previous steps should be notified when an event occurs. This is known as registering the event handler.

Fig 14.12 on page 576 gives examples of some common event-listener interfaces of package java.awt.event
Fig 14.13 on page 577 shows the event registration for JTextField textField1

JButton Examples

// Fig. 14.15: ButtonFrame.java
// Creating JButtons.
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ButtonFrame extends JFrame { private JButton plainJButton; // button with just text private JButton fancyJButton; // button with icons
// ButtonFrame adds JButtons to JFrame public ButtonFrame() { super( "Testing Buttons" ); setLayout( new FlowLayout() ); // set frame layout
plainJButton = new JButton( "Plain Button" ); // button with text add( plainJButton ); // add plainJButton to JFrame
Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) ); Icon bug2 = new ImageIcon( getClass().getResource( "bug2.gif" ) ); fancyJButton = new JButton( "Fancy Button", bug1 ); // set image fancyJButton.setRolloverIcon( bug2 ); // set rollover image add( fancyJButton ); // add fancyJButton to JFrame</ br> // create new ButtonHandler for button event handling ButtonHandler handler = new ButtonHandler(); fancyJButton.addActionListener( handler ); plainJButton.addActionListener( handler ); } // end ButtonFrame constructor
// inner class for button event handling private class ButtonHandler implements ActionListener { // handle button event public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( ButtonFrame.this, String.format( "You pressed: %s", event.getActionCommand() ) ); } // end method actionPerformed } // end private inner class ButtonHandler } // end class ButtonFrame
// Fig. 14.16: ButtonTest.java
// Testing ButtonFrame.
import javax.swing.JFrame;
public class ButtonTest { public static void main( String[] args ) { ButtonFrame buttonFrame = new ButtonFrame(); // create ButtonFrame buttonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); buttonFrame.setSize( 275, 110 ); // set frame size buttonFrame.setVisible( true ); // display frame } // end main } // end class ButtonTest

Components That Maintain State

  • Based on state of component, allows you to do specific things

JButton

// Fig. 14.17: CheckBoxFrame.java
// Creating JCheckBox buttons.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;,
public class CheckBoxFrame extends JFrame { private JTextField textField; // displays text in changing fonts private JCheckBox boldJCheckBox; // to select/deselect bold private JCheckBox italicJCheckBox; // to select/deselect italic
// CheckBoxFrame constructor adds JCheckBoxes to JFrame public CheckBoxFrame() { super( "JCheckBox Test" ); setLayout( new FlowLayout() ); // set frame layout
// set up JTextField and set its font textField = new JTextField( "Watch the font style change", 20 ); textField.setFont( new Font( "Serif", Font.PLAIN, 14 ) ); add( textField ); // add textField to JFrame
boldJCheckBox = new JCheckBox( "Bold" ); // create bold checkbox italicJCheckBox = new JCheckBox( "Italic" ); // create italic add( boldJCheckBox ); // add bold checkbox to JFrame add( italicJCheckBox ); // add italic checkbox to JFrame</ br> // register listeners for JCheckBoxes CheckBoxHandler handler = new CheckBoxHandler(); boldJCheckBox.addItemListener( handler ); italicJCheckBox.addItemListener( handler ); } // end CheckBoxFrame constructor
// private inner class for ItemListener event handling private class CheckBoxHandler implements ItemListener { // respond to checkbox events public void itemStateChanged( ItemEvent event ) { Font font = null; // stores the new Font
// determine which CheckBoxes are checked and create Font if ( boldJCheckBox.isSelected() && italicJCheckBox.isSelected() ) font = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 ); else if ( boldJCheckBox.isSelected() ) font = new Font( "Serif", Font.BOLD, 14 ); else if ( italicJCheckBox.isSelected() ) font = new Font( "Serif", Font.ITALIC, 14 ); else font = new Font( "Serif", Font.PLAIN, 14 );
textField.setFont( font ); // set textField's font } // end method itemStateChanged } // end private inner class CheckBoxHandler } // end class CheckBoxFrame
// Fig. 14.18: CheckBoxTest.java
// Testing CheckBoxFrame.
import javax.swing.JFrame;
public class CheckBoxTest { public static void main( String[] args ) { CheckBoxFrame checkBoxFrame = new CheckBoxFrame(); checkBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); checkBoxFrame.setSize( 275, 100 ); // set frame size checkBoxFrame.setVisible( true ); // display frame } // end main } // end class CheckBoxTest

JRadioButtons

  • Similar to checkboxes in that they have two states; selected and not selected
  • The logical relationship between radio buttons is maintained by a ButtonGroup object (from Java.swing), which itself is not a GUI component.
// Fig. 14.19: RadioButtonFrame.java
// Creating radio buttons using ButtonGroup and JRadioButton.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
public class RadioButtonFrame extends JFrame { private JTextField textField; // used to display font changes private Font plainFont; // font for plain text private Font boldFont; // font for bold text private Font italicFont; // font for italic text private Font boldItalicFont; // font for bold and italic text private JRadioButton plainJRadioButton; // selects plain text private JRadioButton boldJRadioButton; // selects bold text private JRadioButton italicJRadioButton; // selects italic text private JRadioButton boldItalicJRadioButton; // bold and italic private ButtonGroup radioGroup; // buttongroup to hold radio buttons
// RadioButtonFrame constructor adds JRadioButtons to JFrame public RadioButtonFrame() { super( "RadioButton Test" ); setLayout( new FlowLayout() ); // set frame layout
textField = new JTextField( "Watch the font style change", 25 ); add( textField ); // add textField to JFrame
// create radio buttons plainJRadioButton = new JRadioButton( "Plain", true ); boldJRadioButton = new JRadioButton( "Bold", false ); italicJRadioButton = new JRadioButton( "Italic", false ); boldItalicJRadioButton = new JRadioButton( "Bold/Italic", false ); add( plainJRadioButton ); // add plain button to JFrame add( boldJRadioButton ); // add bold button to JFrame add( italicJRadioButton ); // add italic button to JFrame add( boldItalicJRadioButton ); // add bold and italic button
// create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); // create ButtonGroup radioGroup.add( plainJRadioButton ); // add plain to group radioGroup.add( boldJRadioButton ); // add bold to group radioGroup.add( italicJRadioButton ); // add italic to group radioGroup.add( boldItalicJRadioButton ); // add bold and italic
// create font objects plainFont = new Font( "Serif", Font.PLAIN, 14 ); boldFont = new Font( "Serif", Font.BOLD, 14 ); italicFont = new Font( "Serif", Font.ITALIC, 14 ); boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 ); textField.setFont( plainFont ); // set initial font to plain
// register events for JRadioButtons plainJRadioButton.addItemListener( new RadioButtonHandler( plainFont ) ); boldJRadioButton.addItemListener( new RadioButtonHandler( boldFont ) ); italicJRadioButton.addItemListener( new RadioButtonHandler( italicFont ) ); boldItalicJRadioButton.addItemListener( new RadioButtonHandler( boldItalicFont ) ); } // end RadioButtonFrame constructor
// private inner class to handle radio button events private class RadioButtonHandler implements ItemListener { private Font font; // font associated with this listener
public RadioButtonHandler( Font f ) { font = f; // set the font of this listener } // end constructor RadioButtonHandler
// handle radio button events public void itemStateChanged( ItemEvent event ) { textField.setFont( font ); // set font of textField } // end method itemStateChanged } // end private inner class RadioButtonHandler } // end class RadioButtonFrame
// Fig. 14.20: RadioButtonTest.java
// Testing RadioButtonFrame.
import javax.swing.JFrame;
public class RadioButtonTest { public static void main( String[] args ) { RadioButtonFrame radioButtonFrame = new RadioButtonFrame(); radioButtonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); radioButtonFrame.setSize( 300, 100 ); // set frame size radioButtonFrame.setVisible( true ); // display frame } // end main } // end class RadioButtonTest

JComboBox

  • a.k.a Drop down list
  • Drop down list that gives you a selectable list of items
// Fig. 14.21: ComboBoxFrame.java
// JComboBox that displays a list of image names.
import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class ComboBoxFrame extends JFrame { private JComboBox imagesJComboBox; // combobox to hold names of icons private JLabel label; // label to display selected icon
private static final String[] names = { "bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif" }; private Icon[] icons = { new ImageIcon( getClass().getResource( names[ 0 ] ) ), new ImageIcon( getClass().getResource( names[ 1 ] ) ), new ImageIcon( getClass().getResource( names[ 2 ] ) ), new ImageIcon( getClass().getResource( names[ 3 ] ) ) };
// ComboBoxFrame constructor adds JComboBox to JFrame public ComboBoxFrame() { super( "Testing JComboBox" ); setLayout( new FlowLayout() ); // set frame layout
imagesJComboBox = new JComboBox( names ); // set up JComboBox imagesJComboBox.setMaximumRowCount( 3 ); // display three rows
imagesJComboBox.addItemListener( new ItemListener() // anonymous inner class { // handle JComboBox event public void itemStateChanged( ItemEvent event ) { // determine whether item selected if ( event.getStateChange() == ItemEvent.SELECTED ) label.setIcon( icons[ imagesJComboBox.getSelectedIndex() ] ); } // end method itemStateChanged } // end anonymous inner class ); // end call to addItemListener
add( imagesJComboBox ); // add combobox to JFrame label = new JLabel( icons[ 0 ] ); // display first icon add( label ); // add label to JFrame } // end ComboBoxFrame constructor } // end class ComboBoxFrame
// Fig. 14.22: ComboBoxTest.java
// Testing ComboBoxFrame.
import javax.swing.JFrame;
public class ComboBoxTest { public static void main( String[] args ) { ComboBoxFrame comboBoxFrame = new ComboBoxFrame(); comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); comboBoxFrame.setSize( 350, 150 ); // set frame size comboBoxFrame.setVisible( true ); // display frame } // end main } // end class ComboBoxTest

JList

  • JList's can support either single-selection lists (allow only one item to be selected at a time) or multiple-selection lists (allow any number of items to be selected).
example of single-selection list
// Fig. 14.23: ListFrame.java
// JList that displays a list of colors.
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
public class ListFrame extends JFrame { private JList colorJList; // list to display colors private static final String[] colorNames = { "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow" }; private static final Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
// ListFrame constructor add JScrollPane containing JList to JFrame public ListFrame() { super( "List Test" ); setLayout( new FlowLayout() ); // set frame layout
colorJList = new JList( colorNames ); // create with colorNames colorJList.setVisibleRowCount( 5 ); // display five rows at once
// do not allow multiple selections colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
// add a JScrollPane containing JList to frame add( new JScrollPane( colorJList ) );
colorJList.addListSelectionListener( new ListSelectionListener() // anonymous inner class { // handle list selection events public void valueChanged( ListSelectionEvent event ) { getContentPane().setBackground( colors[ colorJList.getSelectedIndex() ] ); } // end method valueChanged } // end anonymous inner class ); // end call to addListSelectionListener } // end ListFrame constructor } // end class ListFrame
// Fig. 14.24: ListTest.java
// Selecting colors from a JList.
import javax.swing.JFrame;
public class ListTest { public static void main( String[] args ) { ListFrame listFrame = new ListFrame(); // create ListFrame listFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); listFrame.setSize( 350, 150 ); // set frame size listFrame.setVisible( true ); // display frame } // end main } // end class ListTest
example of multi-selection list
// Fig. 14.25: MultipleSelectionFrame.java
// Copying items from one List to another.
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class MultipleSelectionFrame extends JFrame { private JList colorJList; // list to hold color names private JList copyJList; // list to copy color names into private JButton copyJButton; // button to copy selected names private static final String[] colorNames = { "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow" };
// MultipleSelectionFrame constructor public MultipleSelectionFrame() { super( "Multiple Selection Lists" ); setLayout( new FlowLayout() ); // set frame layout
colorJList = new JList( colorNames ); // holds names of all colors colorJList.setVisibleRowCount( 5 ); // show five rows colorJList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); add( new JScrollPane( colorJList ) ); // add list with scrollpane
copyJButton = new JButton( "Copy >>>" ); // create copy button copyJButton.addActionListener(
new ActionListener() // anonymous inner class { // handle button event public void actionPerformed( ActionEvent event ) { // place selected values in copyJList copyJList.setListData( colorJList.getSelectedValues() ); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener
add( copyJButton ); // add copy button to JFrame
copyJList = new JList(); // create list to hold copied color names copyJList.setVisibleRowCount( 5 ); // show 5 rows copyJList.setFixedCellWidth( 100 ); // set width copyJList.setFixedCellHeight( 15 ); // set height copyJList.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION ); add( new JScrollPane( copyJList ) ); // add list with scrollpane } // end MultipleSelectionFrame constructor } // end class MultipleSelectionFrame
// Fig. 14.26: MultipleSelectionTest.java
// Testing MultipleSelectionFrame.
import javax.swing.JFrame;
public class MultipleSelectionTest { public static void main( String[] args ) { MultipleSelectionFrame multipleSelectionFrame = new MultipleSelectionFrame(); multipleSelectionFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); multipleSelectionFrame.setSize( 350, 150 ); // set frame size multipleSelectionFrame.setVisible( true ); // display frame } // end main } // end class MultipleSelectionTest

Mouse Operations

  • Figure 14.27 on page 596 provides a list of MouseListener and MouseMotionListener interface methods. Since these are interfaces all of the methods for each interface must be implemented.

Figure 14.28 gives example of implementing multiple interfaces.

// Fig. 14.28: MouseTrackerFrame.java
// Demonstrating mouse events.
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MouseTrackerFrame extends JFrame { private JPanel mousePanel; // panel in which mouse events will occur private JLabel statusBar; // label that displays event information
// MouseTrackerFrame constructor sets up GUI and // registers mouse event handlers public MouseTrackerFrame() { super( "Demonstrating Mouse Events" );
mousePanel = new JPanel(); // create panel mousePanel.setBackground( Color.WHITE ); // set background color add( mousePanel, BorderLayout.CENTER ); // add panel to JFrame
statusBar = new JLabel( "Mouse outside JPanel" ); add( statusBar, BorderLayout.SOUTH ); // add label to JFrame
// create and register listener for mouse and mouse motion events MouseHandler handler = new MouseHandler(); mousePanel.addMouseListener( handler ); mousePanel.addMouseMotionListener( handler ); } // end MouseTrackerFrame constructor
private class MouseHandler implements MouseListener, MouseMotionListener { // MouseListener event handlers // handle event when mouse released immediately after press public void mouseClicked( MouseEvent event ) { statusBar.setText( String.format( "Clicked at [%d, %d]", event.getX(), event.getY() ) ); } // end method mouseClicked
// handle event when mouse pressed public void mousePressed( MouseEvent event ) { statusBar.setText( String.format( "Pressed at [%d, %d]", event.getX(), event.getY() ) ); } // end method mousePressed
// handle event when mouse released public void mouseReleased( MouseEvent event ) { statusBar.setText( String.format( "Released at [%d, %d]", event.getX(), event.getY() ) ); } // end method mouseReleased
// handle event when mouse enters area public void mouseEntered( MouseEvent event ) { statusBar.setText( String.format( "Mouse entered at [%d, %d]", event.getX(), event.getY() ) ); mousePanel.setBackground( Color.GREEN ); } // end method mouseEntered
// handle event when mouse exits area public void mouseExited( MouseEvent event ) { statusBar.setText( "Mouse outside JPanel" ); mousePanel.setBackground( Color.WHITE ); } // end method mouseExited
// MouseMotionListener event handlers // handle event when user drags mouse with button pressed public void mouseDragged( MouseEvent event ) { statusBar.setText( String.format( "Dragged at [%d, %d]", event.getX(), event.getY() ) ); } // end method mouseDragged
// handle event when user moves mouse public void mouseMoved( MouseEvent event ) { statusBar.setText( String.format( "Moved at [%d, %d]", event.getX(), event.getY() ) ); } // end method mouseMoved } // end inner class MouseHandler } // end class MouseTrackerFrame
// Fig. 14.29: MouseTrackerFrame.java
// Testing MouseTrackerFrame.
import javax.swing.JFrame;
public class MouseTracker { public static void main( String[] args ) { MouseTrackerFrame mouseTrackerFrame = new MouseTrackerFrame(); mouseTrackerFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mouseTrackerFrame.setSize( 300, 100 ); // set frame size mouseTrackerFrame.setVisible( true ); // display frame } // end main } // end class MouseTracker

Adapter Classes

  • An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface.
  • Figure 14.30 on page 601 provides Event-adapter classes and the interfaces they implement in java.awt.event
// Fig. 14.31: MouseDetailsFrame.java
// Demonstrating mouse clicks and distinguishing between mouse buttons.
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseDetailsFrame extends JFrame { private String details; // String that is displayed in the statusBar private JLabel statusBar; // JLabel that appears at bottom of window
// constructor sets title bar String and register mouse listener public MouseDetailsFrame() { super( "Mouse Clicks and Buttons" );
statusBar = new JLabel( "Click the mouse" ); add( statusBar, BorderLayout.SOUTH ); addMouseListener( new MouseClickHandler() ); // add handler } // end MouseDetailsFrame constructor
// inner class to handle mouse events private class MouseClickHandler extends MouseAdapter { // handle mouse-click event and determine which button was pressed public void mouseClicked( MouseEvent event ) { int xPos = event.getX(); // get x-position of mouse int yPos = event.getY(); // get y-position of mouse
details = String.format( "Clicked %d time(s)", event.getClickCount() );
if ( event.isMetaDown() ) // right mouse button details += " with right mouse button"; else if ( event.isAltDown() ) // middle mouse button details += " with center mouse button"; else // left mouse button details += " with left mouse button";
statusBar.setText( details ); // display message in statusBar } // end method mouseClicked } // end private inner class MouseClickHandler } // end class MouseDetailsFrame
// Fig. 14.32: MouseDetails.java
// Testing MouseDetailsFrame.
import javax.swing.JFrame;
public class MouseDetails { public static void main( String[] args ) { MouseDetailsFrame mouseDetailsFrame = new MouseDetailsFrame(); mouseDetailsFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mouseDetailsFrame.setSize( 400, 150 ); // set frame size mouseDetailsFrame.setVisible( true ); // display frame } // end main } // end class MouseDetails
  • Figure 14.33 on page 603 provides InputEvent methods that help distinguish among left-, center-, and right- mouse button clicks.

Key Event Handling

  • Key events are generated when keys on the keyboard are pressed and released.
  • A class that implements KeyListener must provide declarations for methods keyPressed, keyReleased, and keyTyped.

Fig 14.36 and 14.37 demonstrate how to implement "hot keys"

// Fig. 14.36: KeyDemoFrame.java
// Demonstrating keystroke events.
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class KeyDemoFrame extends JFrame implements KeyListener { private String line1 = ""; // first line of textarea private String line2 = ""; // second line of textarea private String line3 = ""; // third line of textarea private JTextArea textArea; // textarea to display output
// KeyDemoFrame constructor public KeyDemoFrame() { super( "Demonstrating Keystroke Events" );
textArea = new JTextArea( 10, 15 ); // set up JTextArea textArea.setText( "Press any key on the keyboard..." ); textArea.setEnabled( false ); // disable textarea textArea.setDisabledTextColor( Color.BLACK ); // set text color add( textArea ); // add textarea to JFrame
addKeyListener( this ); // allow frame to process key events } // end KeyDemoFrame constructor
// handle press of any key public void keyPressed( KeyEvent event ) { line1 = String.format( "Key pressed: %s", KeyEvent.getKeyText( event.getKeyCode() ) ); // show pressed key setLines2and3( event ); // set output lines two and three } // end method keyPressed
// handle release of any key public void keyReleased( KeyEvent event ) { line1 = String.format( "Key released: %s", KeyEvent.getKeyText( event.getKeyCode() ) ); // show released key setLines2and3( event ); // set output lines two and three } // end method keyReleased
// handle press of an action key public void keyTyped( KeyEvent event ) { line1 = String.format( "Key typed: %s", event.getKeyChar() ); setLines2and3( event ); // set output lines two and three } // end method keyTyped
// set second and third lines of output private void setLines2and3( KeyEvent event ) { line2 = String.format( "This key is %san action key", ( event.isActionKey() ? "" : "not " ) );
String temp = KeyEvent.getKeyModifiersText( event.getModifiers() );
line3 = String.format( "Modifier keys pressed: %s", ( temp.equals( "" ) ? "none" : temp ) ); // output modifiers
textArea.setText( String.format( "%s\n%s\n%s\n", line1, line2, line3 ) ); // output three lines of text } // end method setLines2and3 } // end class KeyDemoFrame
// Fig. 14.37: KeyDemo.java
// Testing KeyDemoFrame.
import javax.swing.JFrame;
public class KeyDemo { public static void main( String[] args ) { KeyDemoFrame keyDemoFrame = new KeyDemoFrame(); keyDemoFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); keyDemoFrame.setSize( 350, 100 ); // set frame size keyDemoFrame.setVisible( true ); // display frame } // end main } // end class KeyDemo

Introduction to Layout Managers

  • Allows you to concentrate on basic look and feel, let manager take care of specific position and size
  • All layout managers implement the LayoutManager interface.
  • Figure 14.38 on page 611-612 give an explanation of the layout managers: FlowLayout, BorderLayout, and GridLayout.
  • There are three ways to arrange items:
1. Absolute positioning
2. Layout Managers
3. Visual Programming in an IDE


More on layout managers here: A Visual Guide to Layout Managers


Absolute positioning- This method of configuring a applications layout involves the programmer taking each individual component and setting there position by coordinates. This method can be very time consuming and re-size events must be taken into account to replace each item depending on frame size.

Layout Manager- Saves much time while coding an application since the programmer doesn't have to spend much time with trial and error method of positioning each component. It is also very flexible and IDE independent.

Visual Programming in an IDE- Great for getting programs out quick and setting up the look of a program. Makes the work of creating frames, buttons,panels,etc.. very minimal and easy to handle. The drawbacks of a visual IDE layout is that very dependent on the environment and has trouble porting to different IDE's this can be a killer in large group projects. A issue with this style that it also generates a large amount of code that may or may not be optimized for the program being created.

Personal tools