Jump to content




Recent Status Updates

  • Photo
      25 Dec
    MaheshKumar

    JER HR Consulting LLC, originally established in 1990 as James E. Rocco Associates, Inc., was created to provide quality compensation consulting services to non-profit organizations by offering a comprehensive suite of services that support an effective Total Rewards Strategy. Our team of specialists provides expert assistance that is tailored to the individual needs and challenges of each valued client. We work closely with Human Resources Departments, Chief Executive Officers and other sen...

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Java:Tutorial - Making multiple objects work differently


  • Please log in to reply
3 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 11 January 2007 - 12:10 PM

This is the sixth tutorial that will show you how to create graphical user interfaces using java.

Prerequisites

You should know how to create a window in Java.
LINK GOES HERE

You should know how to add a button to the window in Java.
LINK GOES HERE

The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner. Adding buttons to your interface allow for user interaction which is the reason for a GUI. In pervious tutorials, our two buttons did the same thing, but real applications have different buttons that perform different functions.

Solution
Lets start with a shell we’ve formulated in previous tutorials:
package cctuts;

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

public class InterfaceSix implements ActionListener{
	
JFrame interfaceFrame;
JButton startButton, stopButton;
	
	public InterfaceSix(){
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {

	}
	
	public static void main(String[] args){ 
		new InterfaceSix();
	}
}

Now just to recap what we have here: a class that makes a visible window 200px by 70px. The window looks pretty because we set the default look and feel. The window contains two button objects which are positioned using a grid layout. Each button as an action listener, so when the button is pressed the actionPerformed method is invoked. If anything I just said doesn’t make sense, you should read my previous tutorials. In the actionPerformed method we need to determine which button is pressed so we can have our program act accordingly. We do this by using the a.getSource() method, which tells us which button is being pressed. We can then perform simple logical operations to tell our program what to do.

For example if the startButton is pressed, a.getSource() == startButton. We then can say something like:
if(a.getSource() == startButton){
System.out.println(“The start button was pushed…”);

Incorporated into a full class it would look something like this:
package cctuts;

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

public class InterfaceSix implements ActionListener{
	

	JFrame interfaceFrame;
    JButton startButton, stopButton;
	
	public InterfaceSix(){
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {
		if(a.getSource() == startButton){
			System.out.println("Start button was pressed...");
		}
		
		if(a.getSource() == stopButton){
			System.out.println("Stop button was pressed...");
		}
	}
	
	public static void main(String[] args){ 
		new InterfaceSix();
	}
}

  • 0

#2 ljimmyd

ljimmyd

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 16 June 2010 - 07:21 PM

hi John:

I'm curious that what is the "ActionEvent e". I checked in the Java API showing that it is a class. Does it mean that we are creating an object called 'e' here? If so, why isn't there a 'new' operation? Cheers
  • 0

#3 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 160 posts

Posted 18 June 2010 - 08:08 PM

hi John:

I'm curious that what is the "ActionEvent e". I checked in the Java API showing that it is a class. Does it mean that we are creating an object called 'e' here? If so, why isn't there a 'new' operation? Cheers


ActionEvent 'e' is an ActionEvent object. It contains info detailing what occured and from where it occured. You are causing an ActionEvent object to be created when you click a button. Java's event dispatch thread creates an ActionEvent object referenced by 'e' when it detects that a button is clicked. The ActionEvent 'e' object is sent by the event dispatch thread as an argument to the actionPerformed() method. ActionPerformed then knows what type of action happened and from which button it originated.
  • 0

#4 ljimmyd

ljimmyd

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 19 June 2010 - 05:00 AM

ActionEvent 'e' is an ActionEvent object. It contains info detailing what occured and from where it occured. You are causing an ActionEvent object to be created when you click a button. Java's event dispatch thread creates an ActionEvent object referenced by 'e' when it detects that a button is clicked. The ActionEvent 'e' object is sent by the event dispatch thread as an argument to the actionPerformed() method. ActionPerformed then knows what type of action happened and from which button it originated.


Cheers mate. Helps a lot.
  • 0




Powered by binpress