views:

30

answers:

2

I'm writing a Java application that will have an on-screen number pad available for touch-input. Normal key input will also be available, but I'd like the keypad there for tablets and such. I've made a class that extends JPanel. It has 10 buttons laid out in the normal keypad configuration. Now I'm trying to figure out how to make it act like a regular keypad.

I just don't know how to issue KeyEvents. Here's what I've tried so far:

I tried adding a new KeyListener. In the JPanel, when a button was pressed, the action listener called a method that created a new KeyEvent and sent it to all the KeyListeners that were added to the JPanel. However, no matter how many times I added KeyListeners, the didn't seem to be any associated with the panel.

Another thing I tried was passing the target JTextField to the JPanel and setting a member object to the JTextField. But every time I try to append text to it, the member object is null. It's really perplexing to me.

I'm hoping someone could point me in the correct direction with how to implement this keypad to make it as modular is possible so it can be used inside several different screens.

Thanks in advance!

Brent

A: 

I'm writing a Java application that will have an on-screen number pad available for touch-input.

So I assume that when the button is "touched" an ActionEvent will be generated. Then I assume you will want to add the character related to the button to a text field. If so, then the following examples should get you started. You don't need to generated KeyEvents, you just respond to the ActionEvents:

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

public class ButtonCalculator extends JFrame implements ActionListener
{
    private JButton[] buttons;
    private JTextField display;

    public ButtonCalculator()
    {
        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        buttons = new JButton[10];

        for (int i = 0; i < buttons.length; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( this );
            button.setMnemonic( text.charAt(0) );
            buttons[i] = button;
            buttonPanel.add( button );
        }

        getContentPane().add(display, BorderLayout.NORTH);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        setResizable( false );
    }

    public void actionPerformed(ActionEvent e)
    {
        JButton source = (JButton)e.getSource();
        display.replaceSelection( source.getActionCommand() );
    }

    public static void main(String[] args)
    {
        ButtonCalculator frame = new ButtonCalculator();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

If that doesn't solve your problem, then you should be looking into Key Bindings instead of using KeyEvents.

camickr
+1  A: 

You dont need the KeyListener you can associate yourself the outcome effect of pressing a button (its not a key) on the JTextField.

Maybe something like:

New JButton button = new JButton(new KeyPressedAction(mTextField,"0"));

Where

public class KeyPressedAction extends Action{
  JTextField tf;
  String num;
  public KeyPressedAction(JTextField textField,String num){
    this.tf = textField;
    this.num = num;
  }

  public void actionPerformed(ActionEvent e){
    textField.setText(textField.getText+num);
  }
}
Ido