Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:28:37+00:00 2026-06-15T01:28:37+00:00

I am trying to create a custom panel for a very basic statistical calculator

  • 0

I am trying to create a custom panel for a very basic statistical calculator program. I have buttons from one to 9 to enter the numbers, and a single inner class which implements ActionListener to handle them. The numbers should show up on a JLabel called display. The program compiles properly, however, the listener does not seem to work, and nothing happens when I click on those buttons. Here is the code :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial") public class StatCalcPanelBeta extends JPanel{

    private class InputHandler implements ActionListener{
        @Override public void actionPerformed(ActionEvent evt){
            String str = evt.toString();
            switch(str){
            case "1":
                inputString.append("1");
                inputField.setText(inputString.toString());
                break;
            case "2":
                inputString.append("2");
                inputField.setText(inputString.toString());
                break;
            case "3":
                inputString.append("3");
                inputField.setText(inputString.toString());
                break;
            case "4":
                inputString.append("4");
                inputField.setText(inputString.toString());
                break;
            case "5":
                inputString.append("5");
                inputField.setText(inputString.toString());
                break;
            case "6":
                inputString.append("6");
                inputField.setText(inputString.toString());
                break;
            case "7":
                inputString.append("7");
                inputField.setText(inputString.toString());
                break;
            case "8":
                inputString.append("8");
                inputField.setText(inputString.toString());
                break;
            case "9":
                inputString.append("9");
                inputField.setText(inputString.toString());
                break;
            }
        }
    }

    private final StatCalc calculator;
    private JLabel display;
    private JPanel displayPanel;
    private JTextField inputField;
    private final StringBuffer inputString;
    private JButton enter;
    private JButton button_1;
    private JButton button_2;
    private JButton button_3;
    private JButton button_4;
    private JButton button_5;
    private JButton button_6;
    private JButton button_7;
    private JButton button_8;
    private JButton button_9;
    private JPanel inputPanel;
    private InputHandler ioListener;
    private JButton count;
    private JButton sum;
    private JButton mean;
    private JButton max;
    private JButton min;
    private JButton standardDeviation;
    private JPanel functionPanel;

    public StatCalcPanelBeta(){
        calculator = new StatCalc();
        inputString = new StringBuffer();
        initialize();
    }

    private void initialize(){
        setLayout(null);

        displayPanel = new JPanel();
        displayPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
                null, null), "Display", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        displayPanel.setBounds(10, 11, 190, 53);
        add(displayPanel);
        displayPanel.setLayout(null);

        display = new JLabel("Display");
        display.setBounds(0, 11, 180, 37);
        displayPanel.add(display);
        display.setHorizontalAlignment(SwingConstants.CENTER);

        inputPanel = new JPanel();
        inputPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
                "Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        inputPanel.setBounds(0, 72, 211, 145);
        this.add(inputPanel);
        inputPanel.setLayout(null);

        ioListener = new InputHandler();

        inputField = new JTextField(inputString.toString());
        inputField.setEditable(false);
        inputField.setBounds(9, 16, 86, 20);
        inputPanel.add(inputField);
        inputField.setColumns(10);

        enter = new JButton("Enter");
        enter.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                try{
                    calculator.enter(Double.parseDouble(inputString.toString()));
                } catch(NumberFormatException e){
                    e.printStackTrace();
                }
                display.setText("");
            }
        });
        enter.setToolTipText("Enters the number displayed on the input field");
        enter.setBounds(110, 16, 89, 23);
        inputPanel.add(enter);

        button_1 = new JButton("1");
        button_1.setBounds(6, 47, 65, 23);
        button_1.addActionListener(ioListener);
        inputPanel.add(button_1);

        button_2 = new JButton("2");
        button_2.setBounds(73, 47, 65, 23);
        button_2.addActionListener(ioListener);
        inputPanel.add(button_2);

        button_3 = new JButton("3");
        button_3.setBounds(140, 47, 65, 23);
        button_3.addActionListener(ioListener);
        inputPanel.add(button_3);

        button_4 = new JButton("4");
        button_4.setBounds(6, 81, 65, 23);
        button_4.addActionListener(ioListener);
        inputPanel.add(button_4);

        button_5 = new JButton("5");
        button_5.setBounds(73, 81, 65, 23);
        button_5.addActionListener(ioListener);
        inputPanel.add(button_5);

        button_6 = new JButton("6");
        button_6.setBounds(140, 81, 65, 23);
        button_6.addActionListener(ioListener);
        inputPanel.add(button_6);

        button_7 = new JButton("7");
        button_7.setBounds(6, 115, 65, 23);
        button_7.addActionListener(ioListener);
        inputPanel.add(button_7);

        button_8 = new JButton("8");
        button_8.setBounds(73, 115, 65, 23);
        button_8.addActionListener(ioListener);
        inputPanel.add(button_8);

        button_9 = new JButton("9");
        button_9.setBounds(140, 115, 65, 23);
        button_9.addActionListener(ioListener);
        inputPanel.add(button_9);

        functionPanel = new JPanel();
        functionPanel.setBorder(new TitledBorder(
                new EtchedBorder(EtchedBorder.LOWERED, null, null), "Functions",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        functionPanel.setBounds(0, 228, 211, 113);
        this.add(functionPanel);
        functionPanel.setLayout(null);

        count = new JButton("Count");
        count.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Integer.toString(calculator.getCount()));
            }
        });
        count.setBounds(6, 16, 65, 23);
        functionPanel.add(count);

        sum = new JButton("Sum");
        sum.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getSum()));
            }
        });
        sum.setBounds(73, 16, 65, 23);
        functionPanel.add(sum);

        mean = new JButton("Mean");
        mean.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMean()));
            }
        });
        mean.setBounds(140, 16, 65, 23);
        functionPanel.add(mean);

        max = new JButton("Maximum");
        max.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMax()));
            }
        });
        max.setBounds(10, 49, 89, 23);
        functionPanel.add(max);

        min = new JButton("Minimum");
        min.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getMin()));
            }
        });
        min.setBounds(111, 49, 89, 23);
        functionPanel.add(min);

        standardDeviation = new JButton("Standard Deviation");
        standardDeviation.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent arg0){
                display.setText(Double.toString(calculator.getStandardDeviation()));
            }
        });
        standardDeviation.setBounds(33, 83, 147, 23);
        functionPanel.add(standardDeviation);
    }
}

All other classes associated with this are working properly, and so are the listeners of the other buttons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:28:38+00:00Added an answer on June 15, 2026 at 1:28 am
    String str = evt.toString();
    

    This will never return “1” (or any of the other digits). If you want to retrieve the button, use evt.getSource() and cast it to a JButton. From that JButton you can then retrieve the correct information.

    The other option is to use the action-command. See JButton#setActionCommand and ActionEvent#getActionCommand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a simple custom control in C# Winforms that inherits from Panel,
I'm trying to create a custom component to extend PrimeFaces. I have a simple
I am trying to create a custom popup view that can be called from
I am trying to create a custom style panel ( StyledStackPanel ) which is
I have a custom content type with custom fields. I'm trying to create a
I've been trying to create a custom control that works exactly like the Panel
Trying to create a custom component that gets it's layout from an XML file
I'm trying to create custom view that draws image downloaded from Url. The code
I'm trying to create custom drop-down select like the one used on twitter when
I'm trying to create a custom User Control with the following: var panel =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.