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]

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);
}
}
> 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.