MORE BUTTON EXAMPLES

CONTENTS

1. Introduction
2. Combining buttons and text fields
3. Combining buttons and text fields (Swing example)
4. Combining buttons and text areas (Swing example including append method)


1. INTRODUCTION

In this WWW page I intend to inlude further examples of GUIs involving buttons as and when the need arrises.



2. COMBINING BUTTONS AND TEXT FIELDS

The code presented in Table 1 causes text to appear in the text field each time the button is pressed. Some example output is given in Figure 1.

// Button Example 1 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

import java.awt.*;
import java.awt.event.*;

public class ButtonExample1 extends Frame implements 
			ActionListener, WindowListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      FIELDS                       */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    private TextField aTextField = new TextField(45);
    private Button pushButton    = new Button("press me");
    
    private int numButtonPresses = 0;    
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public ButtonExample1(String text) {
        super(text);
	
        setLayout(new FlowLayout());
	setBackground(Color.yellow);
        addWindowListener(this);
	
	// Text fields
	
	add(aTextField);
	
	// Button
        
        Button pushButton = new Button("press me");
        add(pushButton);
	
	// Register event handlers
	
	pushButton.addActionListener(this);
        }
   
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      METHODS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process button press event */
                     
    public void actionPerformed(ActionEvent event) {
        numButtonPresses++;
	               
        if (event.getActionCommand().equals("press me")) {
	    aTextField.setText("Number of button presses = " +
	    				numButtonPresses);
	    }
        } 

     /* WINDOW EVENT HANDLERS */
     	   
    /* Window Closing */    
    public void windowClosing(WindowEvent event) {
        System.exit(0);
        } 
    /* Window Closed */ 
    public void windowClosed(WindowEvent event) {}
    /* Window Deiconified */   
    public void windowDeiconified(WindowEvent event) {}
    /* Window  Iconified */    
    public void windowIconified(WindowEvent event) {}
    /* Window Activated */   
    public void windowActivated(WindowEvent event) {}
    /* Window Deactivated */   
    public void windowDeactivated(WindowEvent event) {}
    /* Window Opened */    
    public void windowOpened(WindowEvent event) {}
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class ButtonExample1App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        ButtonExample1 screen = new ButtonExample1("Button example1 1");
        
        screen.setSize(500,100);
        screen.setVisible(true);
        }
    }

Table 1:Linking button presses and text fields

BUTTON TO TEXT FIELD GUI 1

Figure 1: Some output produced by code presemted in Table 1



3. COMBINING BUTTONS AND TEXT FIELDS (SWING EXAMPLE)

The Java code presented in Table 2 has the same functionality to that presented in table 1 but is encoded using Java Swing. Some sample output is presented in Figure 2.

// Button Example 2 (Swing) 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

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

public class ButtonExample2 extends JFrame implements ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      FIELDS                       */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    private JTextField aTextField = new JTextField(40);
    private JButton pushButton    = new JButton("press me");
    
    private int numButtonPresses = 0;    
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public ButtonExample2(String text) {
        super(text);
	Container container = getContentPane();
	container.setLayout(new FlowLayout());
	container.setBackground(Color.yellow);
	
	// Text fields
	
	container.add(aTextField);
	
	// Button
        
        container.add(pushButton);
	
	// Register event handlers
	
	pushButton.addActionListener(this);
        }
   
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      METHODS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process button press event */
                     
    public void actionPerformed(ActionEvent event) {
        numButtonPresses++;
	               
        if (event.getActionCommand().equals("press me")) {
	    aTextField.setText("Number of button presses = " +
	    				numButtonPresses);
	    }
        } 
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class ButtonExample2App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        ButtonExample2 screen = new ButtonExample2("Button example1 1");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        screen.setSize(500,100);
        screen.setVisible(true);
        }
    }

Table 2:Linking button presses and text fields (Swing example)

BUTTON TO TEXT FIELD GUI 2

Figure 2: Some output produced by code presemted in Table 2



4. COMBINING BUTTONS AND TEXT AREAS (SWING EXAMPLE INCLUDING append METHOD)

The Java code presented in Table 3 uses a button to update a text area using the append method in rhe TextArea calss. Some sample output is presented in Figure 3.

// Button Example 3 (Swing) 
// Frans Coenen
// Thursday 20 March 2003
// Dept. of Comp. Sci., University of Liverpool

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

public class ButtonExample3 extends JFrame implements ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      FIELDS                       */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    private JTextArea aTextArea = new JTextArea(7,40);
    private JButton pushButton  = new JButton("press me");
    
    private int numButtonPresses = 0;    
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public ButtonExample3(String text) {
        super(text);
	Container container = getContentPane();
	container.setLayout(new FlowLayout());
	container.setBackground(Color.yellow);
	
	/// Text fields
        
        container.add(new JScrollPane(aTextArea));   
	
	// Button
        
        Button pushButton = new Button("press me");
        container.add(pushButton);
	
	// Register event handlers
	
	pushButton.addActionListener(this);
        }
   
    /* ------------------------------------------------- */
    /*                                                   */
    /*                      METHODS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process button press event */
                     
    public void actionPerformed(ActionEvent event) {
        numButtonPresses++;
	               
        if (event.getActionCommand().equals("press me")) {
	    aTextArea.append("Number of button presses = " +
	    				numButtonPresses + "\n");
	    }
        } 
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class ButtonExample3App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        ButtonExample3 screen = new ButtonExample3("Button example1 3");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        screen.setSize(500,200);
        screen.setVisible(true);
        }
    }

Table 3:Linking button presses and text areas (Swing example)

BUTTON TO TEXT AREA GUI 1

Figure 3: Some output produced by code presemted in Table 3




Created and maintained by Frans Coenen. Last updated 21 March 2003