Results 1 to 7 of 7
  1. #1
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default JFRAME : How to close old frame when a new frame occurs...

    folks,
    I want to check conditions if frame already exist

    ie : if frame exist,close frame upon creating frame1
    and if frame1 exist ,close frame1 upon creating frame.

    i have the below code, the 2 constructors creates frame and frame1.

    Java Code:
    package moxaclient;
    
    import java.awt.Color;
    import java.sql.*;
    import java.util.*;
    
    import javax.swing.*;
    import javax.swing.table.TableColumn;
    
    public class Showalarm 
      {
    	 JFrame frame = new JFrame();
    	 JFrame frame1 = new JFrame();
        Showalarm() 
        {   	
               if(frame1.isFocused())                                //here check if frame1 is in foreground,if it is close frame1 and generate frame
        	{
        	System.out.println("close frame1");
        	}
            Vector columnNames = new Vector();
            Vector data = new Vector();
            JPanel panel = new JPanel();   //
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection con = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/moxa", "root", "root");
                String sql = "Select * from alarmval1";
                Statement statement = con.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columns = metaData.getColumnCount();
                for (int i = 1; i <= columns; i++) {
                    columnNames.addElement(metaData.getColumnName(i));
                }
                while (resultSet.next()) {
                    Vector row = new Vector(columns);
                    for (int i = 1; i <= columns; i++) {
                        row.addElement(resultSet.getObject(i));
                    }
                    data.addElement(row);
                }
                resultSet.close();
                statement.close();
            } catch (Exception e) {
                System.out.println(e);
            }
            JTable table = new JTable(data, columnNames);
            TableColumn column;
            for (int i = 0; i < table.getColumnCount(); i++) {
                column = table.getColumnModel().getColumn(i);
                column.setMaxWidth(250);
            }
            JScrollPane scrollPane = new JScrollPane(table);       
            panel.add(scrollPane);               
            JFrame frame = new JFrame();
            frame.setTitle("Alarm");
            frame.add(panel);         //adding panel to the frame
            frame.setSize(600, 400); //setting frame size
            frame.setVisible(true);  //setting visibility true
        }
      
        Showalarm(int a)
        {
       	JLabel History=new JLabel("HISTORY");
          //  Showhistory() 
            //{
        	if(frame.isFocused())                        //here check if frame is in foreground,if it is close frame1 and generate frame
        	{ 
        	System.out.println("close frame");
        	}
                Vector columnNames = new Vector();
                Vector data = new Vector();
                JPanel panel = new JPanel();   //
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    Connection con = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/moxa", "root", "root");
                    String sql = "Select * from History";
                    Statement statement = con.createStatement();
                    ResultSet resultSet = statement.executeQuery(sql);
                    ResultSetMetaData metaData = resultSet.getMetaData();
                    int columns = metaData.getColumnCount();
                    for (int i = 1; i <= columns; i++) {
                        columnNames.addElement(metaData.getColumnName(i));
                    }
                    while (resultSet.next()) {
                        Vector row = new Vector(columns);
                        for (int i = 1; i <= columns; i++) {
                            row.addElement(resultSet.getObject(i));
                        }
                        data.addElement(row);
                    }
                    resultSet.close();
                    statement.close();
                } catch (Exception e) {
                    System.out.println(e);
                }
                JTable table = new JTable(data, columnNames);
                TableColumn column;
                for (int i = 0; i < table.getColumnCount(); i++) {
                    column = table.getColumnModel().getColumn(i);
                    column.setMaxWidth(250);
                }
                JScrollPane scrollPane = new JScrollPane(table);       
                panel.add(scrollPane);               
               // JFrame frame = new JFrame();
                History.setBackground(Color.GRAY);
                History.setBounds(300,300,300, 300);
                frame1.add(History);
                frame1.add(panel);         //adding panel to the frame
                frame1.setSize(600, 400); //setting frame size
                frame1.setVisible(true);  //setting visibility true
            }
        
    }
    thanks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    Why not wrap your frame creation code in a method and track the frame in a field.

    Java Code:
    public JFrame createFrame() {
    if (currentFrame != null) {
        currentFrame.dipose();
    }
    currentFrame = new JFrame();
    // set frame parameters here
    return currentFrame;
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    HOW ??? how do i implement that in program:

    and moreover both frame have different frame properties
    Last edited by dinu; 04-11-2013 at 06:46 AM.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    Perhaps if you explained what it is you are trying to accomplish I could provide better assistance. As soon as you create an instance of Showalarm, your two frames exist simultaneously. Depending on which constructor you use, it seems you want to close down the other frame. So why not just create the appropriate frame in the constructor to begin with. Or perhaps I am not understanding what you really want.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    the real scene is that i have a main program with two buttons. 1 button calls the constructor showalarm() and 2nd button calls the
    constructor showalarm(int a).

    Suppose i click on button 1 a frame is created ,and when I click the second button 2nd frame is created.. I DONT want 2 frames visible on
    screen.. onclick on 2nd button close already existing frame that is visible and vice versa ..

    IN short only 1 frame should be visible ,and that frame is the one with the latest button click
    Last edited by dinu; 04-11-2013 at 06:31 PM.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    Ok, as I see it, you are invoking a new frame instance whenever you press a particular button, and disposing another instance.
    Java Code:
    // button1's actionListener:
    public void actionPerformed(ActionEvent ae) {
    if (frame1 != null) {
      frame1.dispose();
    }
    Showalarm showAlarm = new Showalarm();
    }
    
    // button2's actionListener:
    public void actionPerformed(ActionEvent ae) {
    if (frame != null) {
     frame.dispose();
    }
      Showalarm showAlarm = new Showalarm(a);
    }
    However, a JFrame is a JFrame regardless of how it is decorated. Since they are not shown at the same time, why not just use a single frame field for both frames, but use the buttons to close it and recreate the one of choice. So in each constructor, just
    create and intialize the appropriate frame.

    Note: because you are calling constructors via your buttons, I have been assuming that you want to dispose of the other frame. You can also opt to keep both instances of each frame but set their visibility so that they don't show at the same time.

    Regards,
    Jim
    Last edited by jim829; 04-11-2013 at 07:37 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: JFRAME : How to close old frame when a new frame occurs...

    hey thanks that worked !!

Similar Threads

  1. Replies: 7
    Last Post: 04-01-2012, 04:11 AM
  2. Close a frame from another class
    By aborgeld in forum New To Java
    Replies: 11
    Last Post: 03-27-2011, 08:22 PM
  3. How do I close a frame with a button?
    By Psyclone in forum AWT / Swing
    Replies: 7
    Last Post: 02-19-2010, 10:43 PM
  4. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 10:22 PM
  5. Frame close operation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •