Binding keys to buttons

For my calculator that I have been messing around with I want to bind certain keys to certain buttons. I was wondering what would be the easiest way to do it. Any help would be greatly appreciated. My code can be found here.Thanks,-Dan
[256 byte] By [danny9894a] at [2007-10-3 4:05:34]
# 1
I gave you an answer in your last posting.
camickra at 2007-7-14 22:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 2
See in my code I am still clueless on how I would set it up. I read it but now I am more confused.
danny9894a at 2007-7-14 22:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

What code?

Here is a simple example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SimpleKeypad extends JFrame implements ActionListener

{

private JTextField display;

public SimpleKeypad()

{

display = new JTextField();

display.setEditable( false );

display.setFocusable( false );

display.setHorizontalAlignment(JTextField.RIGHT);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout( new GridLayout(0, 5) );

for (int i = 0; i < 10; i++)

{

String text = String.valueOf(i);

JButton button = new JButton( text );

button.setFocusPainted( false );

button.setFocusable( false );

button.addActionListener( this );

buttonPanel.add( button );

}

getContentPane().add(display, BorderLayout.NORTH);

getContentPane().add(buttonPanel, BorderLayout.SOUTH);

setResizable( false );

// Support keyboard entry of numbers

InputMap im = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

String key = "NumberTyped";

for (int i = 0; i < 10; i++)

{

String digit = String.valueOf(i);

KeyStroke ks = KeyStroke.getKeyStroke(digit.charAt(0));

im.put(ks, key);

}

getRootPane().getActionMap().put(key, new NumberTypedAction());

}

public void actionPerformed(ActionEvent e)

{

display.replaceSelection( e.getActionCommand() );

}

class NumberTypedAction extends AbstractAction

{

public void actionPerformed(ActionEvent e)

{

display.replaceSelection( e.getActionCommand() );

}

}

public static void main(String[] args)

{

SimpleKeypad frame = new SimpleKeypad();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

camickra at 2007-7-14 22:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 4

Sorry I forgot to paste my code.

import java.awt.*;

import java.awt.event.*;

import java.io.File;

import javax.swing.*;

// Frame for button panel

class CalculatorFrame extends JFrame

{

public CalculatorFrame()

{

Toolkit kit = Toolkit.getDefaultToolkit();

// sets title & icon image for window

setTitle("Java Calculator by D. Burkland");

Image img = kit.getImage("calc.gif");

setIconImage(img);

// add panel to frame

CalculatorPanel calculator = new CalculatorPanel();

add(calculator);

pack();

// centers frame

Dimension screenSize = kit.getScreenSize();

int screenHeight = screenSize.height;

int screenWidth = screenSize.width;

// center & set dimensions of frame

setSize(width, height);

setLocation(screenWidth / 3, screenHeight / 3);

// set file chooser

chooser = new JFileChooser();

chooser.setCurrentDirectory(new File("."));

// set up menu bar

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu filemenu = new JMenu("File");

JMenu helpmenu = new JMenu("Help");

menuBar.add(filemenu);

menuBar.add(helpmenu);

// action of the exit text

JMenuItem exitItem = new JMenuItem("Exit");

filemenu.add(exitItem);

exitItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

System.exit(0);

}

});

JMenuItem helpItem = new JMenuItem("About");

helpmenu.add(helpItem);

helpItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

JOptionPane.showMessageDialog(null, "Java Calculator By: D. Burkland Summer 2006");

}

});

}

public static final int width = 350;

public static final int height = 200;

private JFileChooser chooser;

}

// Calculator panel

class CalculatorPanel extends JPanel

{

public CalculatorPanel()

{

setLayout(new BorderLayout());

result = 0;

lastCommand = "=";

start = true;

// add the display

display = new JButton("Hello");

display.setEnabled(false);

add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();

ActionListener command = new CommandAction();

ActionListener clrcommand = new ClearAction();

// adds the buttons in a 4 x 4 grid

calculator = new JPanel();

calculator.setLayout(new GridLayout(5, 4));

addButton("7", insert);

addButton("8", insert);

addButton("9", insert);

addButton("/", command);

addButton("4", insert);

addButton("5", insert);

addButton("6", insert);

addButton("*", command);

addButton("1", insert);

addButton("2", insert);

addButton("3", insert);

addButton("-", command);

addButton("0", insert);

addButton(".", insert);

addButton("=", command);

addButton("+", command);

addButton("CLR", clrcommand);

add(calculator, BorderLayout.CENTER);

}

/**

Adds a button to the center panel

@param lable the button label

@param listener the button listener

*/

private void addButton(String label, ActionListener listener)

{

JButton button = new JButton(label);

button.addActionListener(listener);

calculator.add(button);

}

// Inserts the button action string to the end of the display

private class InsertAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String input = event.getActionCommand();

if (start)

{

display.setText("");

start = false;

}

display.setText(display.getText() + input);

}

}

// Executes the command that the button action string demotes

private class CommandAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String command = event.getActionCommand();

if (start)

{

if (command.equals("-"))

{

display.setText(command);

start = false;

}

else

lastCommand = command;

}

else

{

calculate(Double.parseDouble(display.getText()));

lastCommand = command;

start = true;

}

}

}

private class ClearAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String clrcommand = event.getActionCommand();

if (clrcommand.equals("CLR"))

{

display.setText(" ");

start = true;

}

else

start = false;

}

}

// Carries out the pending calculation

public void calculate(double x)

{

if (lastCommand.equals("+")) result += x;

else if (lastCommand.equals("-")) result -= x;

else if (lastCommand.equals("*")) result *= x;

else if (lastCommand.equals("/")) result /= x;

else if (lastCommand.equals("=")) result = x;

display.setText("" + result);

}

private JButton display;

private JPanel calculator;

private double result;

private String lastCommand;

private boolean start;

}

public class Calculator

{

public static void main(String[] args)

{

CalculatorFrame calculator = new CalculatorFrame();

calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

calculator.setVisible(true);

}

}

Message was edited by:

danny9894

Message was edited by:

danny9894

danny9894a at 2007-7-14 22:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Sorry I forgot to paste my code.I don't see why you are posting your code now. I gave you a working example of using Key Bindings. Now its up to you to incorporate the ideas into your code.
camickra at 2007-7-14 22:04:55 > top of Java-index,Desktop,Core GUI APIs...