2

i'm trying to make enter key react instead of the mouse click.

i have no idea how to do that using java.

here is the code and the output.

import java.util.Random;
import javax.swing.*;
import java.util.Arrays;
import java.text.DecimalFormat;
import java.awt.event.*;        
import java.awt.*;

public class PayRate extends JFrame
{
    private JPanel panel;       
    private JLabel rateLabel;
    private JLabel hoursLabel;
    private JLabel payLabel;
    private JTextField rateTextField; 
    private JTextField hoursTextField;
    private JTextField payTextField;  
    private JButton calcButton;
    private JButton clearButton;
    private final int WINDOW_WIDTH = 350;
    private final int WINDOW_HEIGHT = 160;

    public PayRate()
    {
        setTitle("PAY RATE");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel()
    {
        rateLabel = new JLabel("RATE");
        hoursLabel = new JLabel("HOUR");
        payLabel = new JLabel("");
        rateTextField = new JTextField(8);
        hoursTextField = new JTextField(8);
        payTextField = new JTextField(27);
        calcButton = new JButton("CALCULATE PAY");
       clearButton = new JButton("   CLEAR   ");
        calcButton.addActionListener(new CalcButtonListener());
      clearButton.addActionListener(new clearButtonListener());
      getRootPane().setDefaultButton(calcButton);    // make the enter key react instead of mouse click
      //calcButton.setMnemonic(KeyEvent.VK_E);       // make  (ALT + E) response as an enter key

        panel = new JPanel();

        payTextField.setBackground(Color.ORANGE);
        rateTextField.setBackground(Color.LIGHT_GRAY); // Set the Background of rateTextField to LIGHT_GRAY
        hoursTextField.setBackground(Color.LIGHT_GRAY);// Set the Background of hoursTextField to LIGHT_GRAY
        calcButton.setBackground(Color.GREEN); // Set the background of CalcButton to GREEN
       rateLabel.setForeground(Color.BLUE);   // set the Foreground of rate label to blue
       hoursLabel.setForeground(Color.BLUE); // set the Foreground of hours label to blue
       payLabel.setForeground(Color.BLUE); // set the Foreground of pay label to blue
        panel.setBackground(Color.PINK);// Set the background of the panel to yellow
        panel.add(rateLabel);           // Add rate label to the panel
        panel.add(rateTextField);   // add rate text field to the panel
        panel.add(hoursLabel);     // add hour label to the panel
        panel.add(hoursTextField); // add hours text field to the panel
        panel.add(calcButton);      // add calculate button to the panel 
        panel.add(payLabel);      // add the pay label to the panel
        panel.add(payTextField); // add pay text field to the panel
            panel.add(clearButton);



    }
    private class CalcButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         double rt ;
         String input;  
            String display ="";
            String output = "    Your total pay for this week is: ";
         double hrs;  
            double sum = 0;
            DecimalFormat formatter = new DecimalFormat("#0.00");

         input = rateTextField.getText();
            rt = Double.parseDouble(input);


         input = hoursTextField.getText();
            hrs = Double.parseDouble(input);

            sum = hrs * rt;


         display = display + output.toUpperCase() + formatter.format(sum);
            payTextField.setText(display);


      }


   }

   private class clearButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         payTextField.setText("");
         hoursTextField.setText("");
         rateTextField.setText("");

      }
   }

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

Here is the output.

enter image description here

I Want the the calculate pay button to react to enter key instead of clicking on it using the mouse.

Thank You in advance.

2 Answers 2

4

Option 1: make the JButton of interest the default button for your JFrame's JRootPane:

  calcButton = new JButton("CALCULATE PAY");
  calcButton.addActionListener(new CalcButtonListener());

  getRootPane().setDefaultButton(calcButton);  // **** add this line ****

Option 2: add the same ActionListener to your JTextFields:

  CalcButtonListener calcListener = new CalcButtonListener();
  calcButton.addActionListener(calcListener);
  rateTextField.addActionListener(calcListener);
  payTextField.addActionListener(calcListener);

Edit
You ask in comment:

what if i want another key (such as space key) to react as the enter key? is that possible?

Answer:
A JButton is already wired to respond to space key press if the button has the focus. Otherwise, 1) set the button's mnemonic to respond to an alt-key combination, or 2) use key bindings to bind the button to any key or key combination.

An example of a mnemonic:

calcButton.setMnemonic(KeyEvent.VK_C);

If you add this to your program, you'll see that the first "C" in the button's text is underlined. Your button will also respond to alt-c presses.

Sign up to request clarification or add additional context in comments.

9 Comments

what if i want another key (such as space key) to react as the enter key? is that possible?
@user3080461 for that,you need to register a keyBinding in the button's component inputmap
Hovercraft Full Of Eels: i have another question. i'm trying to add a clear button to clear the form. i create a button and added to the panel, however, i don't know how to make it clear or rather reset the form from everything. is there a way to do that?
@user3080461: what specifically do you want to clear?
Hovercraft Full Of Eels: i want to clear all the text fields that i have.
|
1

Add this code

        public PayRate(){
            setTitle("PAY RATE");
            setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            buildPanel();
            add(panel);
            setVisible(true);
            getRootPane().setDefaultButton(calcButton);// here it is 
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.