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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:01:14+00:00 2026-06-13T22:01:14+00:00

I am having problem with my JFrame. When I click on Back button, the

  • 0

I am having problem with my JFrame.
When I click on “Back” button, the “Classroomdetails” menu appears but the classroom input JFrame form (addclassroom) stays in the background instead of closing
Please help me to correct it.

Here is my code::

public class addclassroom extends JFrame{

    public JTextField txt_classno,txt_bname,txt_size,txt_resources;
    public JComboBox cmbfloor;
    public JLabel label1, label2, label3, label4, label5, label6;
    public JButton Save;
    public JButton Reset;
    public JButton Back;

    public addclassroom(){


        label1 = new javax.swing.JLabel();
        label1.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
        label1.setForeground(Color.BLACK);
        label1.setText("Classroom Records Input");


        label2 = new javax.swing.JLabel("Classroom_No:");
        txt_classno = new javax.swing.JTextField(8);

        label3 = new javax.swing.JLabel("Building Name:");
        txt_bname = new javax.swing.JTextField(15);

        label4 = new javax.swing.JLabel("Building Floor:");
        String floor[] = {"Ground floor","1st floor","2nd floor","3rd floor","4th floor", "5th floor", "6th floor", "7th floor", "8th floor"};
        cmbfloor = new javax.swing.JComboBox(floor);

        label5 = new javax.swing.JLabel("Size Capacity:");
        txt_size = new javax.swing.JTextField(5);


        label6 = new javax.swing.JLabel("Resources");
        txt_resources = new javax.swing.JTextField(20);




        Save = new javax.swing.JButton();
        Save.setText("Save");

        Reset = new javax.swing.JButton();
        Reset.setText("Reset");

        Back = new javax.swing.JButton();
        Back.setText("Back");


        label1.setBounds(100, 25, 200, 50);

        label2.setBounds(50, 100, 100, 50);
        txt_classno.setBounds(180, 100, 100, 30);



        label3.setBounds(50, 150, 100, 50);
        txt_bname.setBounds(180, 150, 140, 30);

        label4.setBounds(50, 200, 100, 50);
        cmbfloor.setBounds(180, 200, 115, 30);



        label5.setBounds(50, 250, 100, 50);
        txt_size.setBounds(180, 250, 65, 30);



        label6.setBounds(50, 300, 100, 50);
        txt_resources.setBounds(180, 300, 175, 30);



        Save.setBounds(100, 400, 75, 20);
        Reset.setBounds(200, 400, 75, 20);
        Back.setBounds(300,400,75,20);

        JFrame ciFrame = new JFrame("Classroom Records Input Form");

        ciFrame.add(label1);
        ciFrame.add(label2);
        ciFrame.add(txt_classno);
        ciFrame.add(label3);
        ciFrame.add(txt_bname);
        ciFrame.add(label4);
        ciFrame.add(cmbfloor);
        ciFrame.add(label5);
        ciFrame.add(txt_size);
        ciFrame.add(label6);
        ciFrame.add(txt_resources);

        ciFrame.add(Save);

        ciFrame.add(Reset);
        ciFrame.add(Back);


        ciFrame.setLayout(null);

        ciFrame.setVisible(true);

        ciFrame.setSize(500, 500);

        ciFrame.setResizable(false);

        ciFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 










        Save.setMnemonic('S');
        Save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                Save.setEnabled(true);
                }
        });



        Reset.setMnemonic('R');
        Back.setMnemonic('B');


        Handleraddclassroom h = new Handleraddclassroom();
        Save.addActionListener(h);
        Reset.addActionListener(h);
        Back.addActionListener(h);




    }


    public class Handleraddclassroom implements ActionListener{
        public void actionPerformed(ActionEvent event){

            String name = ((JButton)event.getSource()).getText();


            if (name.equals("Back")){
                dispose();

                Classroomdetails cm = new Classroomdetails();


            }


            if (name.equals("Reset")){

                txt_classno.setText("");
                txt_bname.setText("");
                txt_size.setText("");
                txt_resources.setText("");




            }



            if (name.equals("Save")){
                String filename = "C:/JAVA/schedule.accdb";
                String database = "jdbc:odbc:scheduling";




                //Character & Presence checks

                String nsize = txt_size.getText();
                String rsc = txt_resources.getText();

                Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                Matcher Cnsize = p.matcher(nsize);

                Pattern q = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                Matcher Rsc = q.matcher(rsc);


                if(txt_classno.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Classroom No field should not be blank");

                }

                if(txt_bname.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Building Name field should not be blank");

                }

                if(txt_size.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Size Capacity field should not be blank");

                }else if(Cnsize.find()){
                    JOptionPane.showMessageDialog(addclassroom.this,"Invalid value for Size capacity entered. Please check again");

                }




                if(txt_resources.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Resources field should not be blank");

                }else if(!Rsc.find()){
                    JOptionPane.showMessageDialog(addclassroom.this,"Invalid value for Resources entered. Please check again");
                }









            else{
                try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


                    Connection con = DriverManager.getConnection(database ,"","");  //username &password not being used

                    String flr="";
                    if(cmbfloor.getSelectedIndex()==0){
                        flr="Ground floor"; }
                        else if(cmbfloor.getSelectedIndex()==1){
                            flr="1st floor";}

                        else if(cmbfloor.getSelectedIndex()==2){
                            flr="2nd floor";}
                        else if(cmbfloor.getSelectedIndex()==3){
                            flr="3rd floor";}
                        else if(cmbfloor.getSelectedIndex()==4){
                            flr="4th floor";}
                        else if(cmbfloor.getSelectedIndex()==5){
                            flr="5th floor";}
                        else if(cmbfloor.getSelectedIndex()==6){
                            flr="6th floor";}
                        else if(cmbfloor.getSelectedIndex()==7){
                            flr="7th floor";}
                        else if(cmbfloor.getSelectedIndex()==8){
                            flr="8th floor";}







                Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //The Result set can now scroll forward and back



                String sql = "INSERT INTO classroom VALUES('" 

                    + txt_classno.getText()+ "','"
                    + txt_bname.getText() + "','"

                    + flr + "','" 
                    + txt_size.getText() + "','"
                    + txt_resources.getText()+ "' )";

                JOptionPane.showMessageDialog(addclassroom.this,"Classroom Records Saved.");
                dispose();

                addclassroom ac = new addclassroom();


                s.execute(sql);



                s.close();
                con.close();

                } catch (Exception e) {System.out.print("Error: " + e);}
                }
            }
        }   


    }



    }
  • 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-13T22:01:14+00:00Added an answer on June 13, 2026 at 10:01 pm

    Your code is confusing because your main class extends JFrame, but you are also creating an internal JFrame called ciFrame which is the one that is actually displayed.

    A quick fix would be to move the declaration of the ciFrame next to the JButton Back (don’t make this public and call it back, not Back). Then, instead of dispose() do ciFrame.dispose();

    Long term, please change your code such as you either extend JFrame and only use this instance of the JFrame or create one as a variable and don’t extend JFrame.

    I don’t like posting 100s of line of code, but here it is, your code, modified to work:

    public class addclassroom extends JFrame {
    
        public JTextField txt_classno, txt_bname, txt_size, txt_resources;
        public JComboBox cmbfloor;
        public JLabel label1, label2, label3, label4, label5, label6;
        public JButton Save;
        public JButton Reset;
        public JButton Back;
        JFrame ciFrame = new JFrame("Classroom Records Input Form");
    
        public addclassroom() {
    
            label1 = new javax.swing.JLabel();
            label1.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
            label1.setForeground(Color.BLACK);
            label1.setText("Classroom Records Input");
    
            label2 = new javax.swing.JLabel("Classroom_No:");
            txt_classno = new javax.swing.JTextField(8);
    
            label3 = new javax.swing.JLabel("Building Name:");
            txt_bname = new javax.swing.JTextField(15);
    
            label4 = new javax.swing.JLabel("Building Floor:");
            String floor[] = { "Ground floor", "1st floor", "2nd floor", "3rd floor", "4th floor", "5th floor",
                    "6th floor", "7th floor", "8th floor" };
            cmbfloor = new javax.swing.JComboBox(floor);
    
            label5 = new javax.swing.JLabel("Size Capacity:");
            txt_size = new javax.swing.JTextField(5);
    
            label6 = new javax.swing.JLabel("Resources");
            txt_resources = new javax.swing.JTextField(20);
    
            Save = new javax.swing.JButton();
            Save.setText("Save");
    
            Reset = new javax.swing.JButton();
            Reset.setText("Reset");
    
            Back = new javax.swing.JButton();
            Back.setText("Back");
    
            label1.setBounds(100, 25, 200, 50);
    
            label2.setBounds(50, 100, 100, 50);
            txt_classno.setBounds(180, 100, 100, 30);
    
            label3.setBounds(50, 150, 100, 50);
            txt_bname.setBounds(180, 150, 140, 30);
    
            label4.setBounds(50, 200, 100, 50);
            cmbfloor.setBounds(180, 200, 115, 30);
    
            label5.setBounds(50, 250, 100, 50);
            txt_size.setBounds(180, 250, 65, 30);
    
            label6.setBounds(50, 300, 100, 50);
            txt_resources.setBounds(180, 300, 175, 30);
    
            Save.setBounds(100, 400, 75, 20);
            Reset.setBounds(200, 400, 75, 20);
            Back.setBounds(300, 400, 75, 20);
    
    
            ciFrame.add(label1);
            ciFrame.add(label2);
            ciFrame.add(txt_classno);
            ciFrame.add(label3);
            ciFrame.add(txt_bname);
            ciFrame.add(label4);
            ciFrame.add(cmbfloor);
            ciFrame.add(label5);
            ciFrame.add(txt_size);
            ciFrame.add(label6);
            ciFrame.add(txt_resources);
    
            ciFrame.add(Save);
    
            ciFrame.add(Reset);
            ciFrame.add(Back);
    
            ciFrame.setLayout(null);
    
            ciFrame.setVisible(true);
    
            ciFrame.setSize(500, 500);
    
            ciFrame.setResizable(false);
    
            ciFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Save.setMnemonic('S');
            Save.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Save.setEnabled(true);
                }
            });
    
            Reset.setMnemonic('R');
            Back.setMnemonic('B');
    
            Handleraddclassroom h = new Handleraddclassroom();
            Save.addActionListener(h);
            Reset.addActionListener(h);
            Back.addActionListener(h);
    
        }
    
        public static void main(String[] args) {
            new addclassroom();
        }
    
        public class Handleraddclassroom implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    
                String name = ((JButton) event.getSource()).getText();
    
                if (name.equals("Back")) {
                    ciFrame.dispose();
    //                Classroomdetails cm = new Classroomdetails();
    
                }
    
                if (name.equals("Reset")) {
    
                    txt_classno.setText("");
                    txt_bname.setText("");
                    txt_size.setText("");
                    txt_resources.setText("");
    
                }
    
                if (name.equals("Save")) {
                    String filename = "C:/JAVA/schedule.accdb";
                    String database = "jdbc:odbc:scheduling";
    
                    // Character & Presence checks
    
                    String nsize = txt_size.getText();
                    String rsc = txt_resources.getText();
    
                    Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                    Matcher Cnsize = p.matcher(nsize);
    
                    Pattern q = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                    Matcher Rsc = q.matcher(rsc);
    
                    if (txt_classno.getText().equals("")) {
                        JOptionPane.showMessageDialog(addclassroom.this, "Classroom No field should not be blank");
    
                    }
    
                    if (txt_bname.getText().equals("")) {
                        JOptionPane.showMessageDialog(addclassroom.this, "Building Name field should not be blank");
    
                    }
    
                    if (txt_size.getText().equals("")) {
                        JOptionPane.showMessageDialog(addclassroom.this, "Size Capacity field should not be blank");
    
                    } else if (Cnsize.find()) {
                        JOptionPane.showMessageDialog(addclassroom.this,
                                "Invalid value for Size capacity entered. Please check again");
    
                    }
    
                    if (txt_resources.getText().equals("")) {
                        JOptionPane.showMessageDialog(addclassroom.this, "Resources field should not be blank");
    
                    } else if (!Rsc.find()) {
                        JOptionPane.showMessageDialog(addclassroom.this,
                                "Invalid value for Resources entered. Please check again");
                    }
    
                    else {
                        try {
                            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
                            Connection con = DriverManager.getConnection(database, "", ""); // username
                                                                                            // &password
                                                                                            // not
                                                                                            // being
                                                                                            // used
    
                            String flr = "";
                            if (cmbfloor.getSelectedIndex() == 0) {
                                flr = "Ground floor";
                            } else if (cmbfloor.getSelectedIndex() == 1) {
                                flr = "1st floor";
                            }
    
                            else if (cmbfloor.getSelectedIndex() == 2) {
                                flr = "2nd floor";
                            } else if (cmbfloor.getSelectedIndex() == 3) {
                                flr = "3rd floor";
                            } else if (cmbfloor.getSelectedIndex() == 4) {
                                flr = "4th floor";
                            } else if (cmbfloor.getSelectedIndex() == 5) {
                                flr = "5th floor";
                            } else if (cmbfloor.getSelectedIndex() == 6) {
                                flr = "6th floor";
                            } else if (cmbfloor.getSelectedIndex() == 7) {
                                flr = "7th floor";
                            } else if (cmbfloor.getSelectedIndex() == 8) {
                                flr = "8th floor";
                            }
    
                            Statement s = con
                                    .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); // The
                                                                                                                     // Result
                                                                                                                     // set
                                                                                                                     // can
                                                                                                                     // now
                                                                                                                     // scroll
                                                                                                                     // forward
                                                                                                                     // and
                                                                                                                     // back
    
                            String sql = "INSERT INTO classroom VALUES('"
    
                            + txt_classno.getText() + "','" + txt_bname.getText() + "','"
    
                            + flr + "','" + txt_size.getText() + "','" + txt_resources.getText() + "' )";
    
                            JOptionPane.showMessageDialog(addclassroom.this, "Classroom Records Saved.");
                            dispose();
    
                            addclassroom ac = new addclassroom();
    
                            s.execute(sql);
    
                            s.close();
                            con.close();
    
                        } catch (Exception e) {
                            System.out.print("Error: " + e);
                        }
                    }
                }
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a simple problem, but somehow I'm having issues with this code.
I am having a bit of problem regarding Swing. I have a JFrame called
I'm having a problem with in merging time and button together... I can't get
I am creating a simple video player but I'm having problem to show the
I am having problem displaying my Main Menu on the screen. I don't see
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I am having problem using mvc:resources in spring 3.1 configuration. Initially i was working
I am having problem with previewing custom performance counters with PerflibV2. Performance Monitor shows
I am having problem to get a numerical value for this expression where I

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.