Creating a working button

Is there a way in java, using the event handler, rather than having either button output the same result, to have two buttons that do different things?

If so, is there a command like click.ButtonName() or something like there is in Basic or C# or C++?

Here is what i have for my event handler, but i need the upButton and downButton to do different things.

Any ideas how i'd go about implementing this?

I will post my code separately.

[466 byte] By [Zerofurya] at [2007-11-27 10:01:31]
# 1

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 Button extends JFrame

{

private JButton upBUtton;

private JButton downButton;

public Button //constructor

{

super( "Browse Inventory" );

setLayout( new FlowLayout() );

upButton = new Jbutton ("Previous Item");

add( upButton );

downButton = newJbutton ("Next Item");

add( downButton );

ButtonHandler handler = new ButtonHandler();

upButton.addActionListener( handler );

downButton.addActionListener( handler );

} // end constructor

private class ButtonHandler implements ActionListener

{

public void actionPerformed( ActionEvent event )

{

JOptionPane.showMessageDialog( Button.this, String.format (myItems.get(itemChosen).toString()),

JOptionPane.PLAIN_MESSAGE);

}

}

Zerofurya at 2007-7-13 0:34:43 > top of Java-index,Java Essentials,Java Programming...
# 2
So basically i want the upButton to do one thing, and the downButton to do something else. I can figure out what code and display info to put within the function myself, but how do i specify what function what button does?Am i even on the right track here?Thanks in advance.
Zerofurya at 2007-7-13 0:34:43 > top of Java-index,Java Essentials,Java Programming...
# 3

> two buttons that do different things

Most straightforwardly you add different action listeners to the two buttons. Or you can use the same action listener but have it check for the source of the event which triggers its actionPerformed() method. Based on the source you do different things.

There is, in general, no one-to-one relationship between action listeners and buttons.

pbrockway2a at 2007-7-13 0:34:43 > top of Java-index,Java Essentials,Java Programming...
# 4
Read the JButton API and click on the link to the Swing tutorial on "How to Use Buttons" for an example showing one way to do this.
camickra at 2007-7-13 0:34:44 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank you everyone
Zerofurya at 2007-7-13 0:34:44 > top of Java-index,Java Essentials,Java Programming...