1 Reply Latest reply: Apr 10, 2013 8:15 AM by Jörg RSS

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

    Dinud123
      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.
      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

      Edited by: Dinud123 on Apr 9, 2013 10:41 PM

      Edited by: Dinud123 on Apr 9, 2013 10:43 PM

      Edited by: Dinud123 on Apr 9, 2013 10:47 PM