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 8068277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:36:57+00:00 2026-06-05T12:36:57+00:00

I’m creating an addStudent method and it looks like this: package gui; import java.awt.*;

  • 0

I’m creating an “addStudent” method and it looks like this:

package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

import dataManager.DataManager;



public class test extends JFrame {
    private static boolean addHowManyStudentsSet=false;
    private static int addHowManyStudents=0;
    private static JFrame addStudentFrame = new JFrame("Add Student");
    private static JTextField newStudentName = new JTextField();
    private static JTextField newStudentID = new JTextField();
    private static JLabel label1 = new JLabel("");
    private static final JButton addButton = new JButton("ADD");
    private static JButton addStudent = new JButton("SET");
    private static JPanel addStudentPanel = new JPanel();
    /**
     * Constructor of the GUI, creating labels, buttons, and other stuff.  Then they are added onto the interface.
     */
    public test() {
        super("test");
        setSize(200, 200);
        setLocation(10, 10);
        final JPanel panel = new JPanel();

        addStudent.setBounds(10,60,80,25);
        panel.add(addStudent);
        add(panel);
        addStudent.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                if(!addHowManyStudentsSet){
                    try{
                        addHowManyStudents=Integer.parseInt(JOptionPane.showInputDialog(panel, "Add how many students..."));
                        JOptionPane.showMessageDialog(panel,"Set, please click this button again");
                        addStudent.setText("ADD");
                        addHowManyStudentsSet=true;
                    }
                    catch(NumberFormatException ex){
                        JOptionPane.showMessageDialog(panel, "Please enter a number");
                    }
                }

                else{

                    addStudentPanel.setLayout(null);
                    label1.setText("    "+(addHowManyStudents-1)+" more students to add...");
                    label1.setFont(new Font("Segoe UI Light",Font.PLAIN,30));
                    label1.setBounds(5,20,400,25);
                    newStudentName.setBounds(270,100,140,30);
                    newStudentID.setBounds(270,150,140,30);
                    final JLabel label2 = new JLabel("New Student Name:");
                    final JLabel label3 = new JLabel("New Student Number:");
                    label2.setBounds(30,100,200,30);
                    label2.setFont(new Font("Segoe UI Light",Font.PLAIN,21));
                    label3.setBounds(30,150,200,30);
                    label3.setFont(new Font("Segoe UI Light",Font.PLAIN,21));
                    //      final JButton addButton = new JButton("ADD");
                    addButton.setBounds(330,220,80,25);
                    addStudentPanel.add(addButton);
                    addButton.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae){            
                            addStudent();
                            //      addStudentFrame.dispose();
                        }
                    });                 
                    addStudentPanel.add(label1);
                    addStudentPanel.add(label2);
                    addStudentPanel.add(label3);
                    addStudentPanel.add(newStudentName);
                    addStudentPanel.add(newStudentID);
                    addStudentFrame.add(addStudentPanel);
                    addStudentFrame.setVisible(true);
                    addStudentFrame.setLocation(40,40);
                    addStudentFrame.setSize(470,335);
                }

            }

        });
    }

    public static void main(String[] args) {
        JFrame f = new test(  );

        f.addWindowListener(new WindowAdapter(  ) {
            public void windowClosing(WindowEvent we) { 
                System.exit(0); }
        });
        f.setVisible(true);
    }

    private static void addStudent(){
        if(addHowManyStudents>0){
            addHowManyStudents--;           
            //          addButton.addActionListener(new ActionListener(){
            //              public void actionPerformed(ActionEvent ae){        
            System.out.println("add");
            //          //  JLabel label1 = new JLabel((StudentList.getHowManyStudentToAdd()-1)+"more students to add");
            try{
                String studentName = newStudentName.getText();
                long studentNum = Long.parseLong(newStudentID.getText());
                //          //  DataManager.addStudent(studentNum, studentName);
                System.out.println("Done: "+studentNum+", "+studentName);
            }
            catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(addStudentFrame, "Student ID can only be numbers");
            }
            if(addHowManyStudents!=0){
                label1.setText("    "+(addHowManyStudents-1)+" more students to add...");

            }
            newStudentName.setText("");
            newStudentID.setText("");
            addStudent();
            //              }               
            //          });

        }
        else if(addHowManyStudents==0){
            JOptionPane.showMessageDialog(addStudentFrame,"Done!");
            addStudentFrame.dispose();
            addHowManyStudentsSet=false;
            addStudent.setText("SET");
        }
    }
}

It’s actually pretty interesting because the first time the user clicks the “add” button it
only adds the student once (for instance, if you wanted to add 14 students it works
properly the first time and tells you there are 13 more students to add.)

However, when the user clicks the “add” button for the second time it adds the student
twice (there are 11 more students to add); it adds 8 times on the third click(3 more
students to add), and so on.

I don’t know what was happening but it just doesn’t work properly.

  • 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-05T12:36:59+00:00Added an answer on June 5, 2026 at 12:36 pm

    Every time you call addStudent() you add the ActionListener to the JButton, and this will cause the JButton to have the listener added many times eventually. This means that when the button is pressed, the listener will be called several times, which is something you really don’t want to happen. The solution is not to do that. Instead add the listener to the JButton only once in the constructor or init method and leave it be at that.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.