Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with my JFrame; JFrame not closing and stay in background

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with my JFrame; JFrame not closing and stay in background

    Hi to all,
    I am having problem with my JFrame.
    When I click on "Back" button, the classroom details menu appears but the classroom input JFrame stays in the background.
    Please help me to correct it.

    Here is my code::
    import java.awt.*; 
    import java.awt.event.*; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.sql.Statement; 
     
    import javax.swing.*; 
    import javax.swing.text.MaskFormatter; 
     
    import java.util.Date; 
    import java.util.regex.Matcher; 
    import java.util.regex.Pattern; 
    import java.text.DateFormat; 
    import java.text.ParseException; 
    import java.text.SimpleDateFormat; 
     
    import java.sql.*; 
    import javax.*; 
     
    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 = "jdbcdbc: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);} 
    } 
    } 
    }	
     
     
    } 
     
     
     
    }
    Last edited by helloworld922; November 6th, 2012 at 01:27 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my JFrame; JFrame not closing and stay in background

    Yikes, that sure is a lot of code for simply displaying two JFrames. I highly suggest you put together an SSCCE that we can look at instead.

    Also, it sounds like you might be looking for CardLayout.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  2. Help with closing a JFrame (not the program)
    By lanepulcini in forum AWT / Java Swing
    Replies: 1
    Last Post: February 10th, 2012, 12:20 PM
  3. help with JButton closing a JFrame :)
    By skerridge in forum AWT / Java Swing
    Replies: 15
    Last Post: January 16th, 2012, 01:11 AM
  4. Undecorated JFrame background shadow
    By md_suzon in forum AWT / Java Swing
    Replies: 1
    Last Post: October 11th, 2011, 09:42 AM
  5. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM

Tags for this Thread