Java JFrame saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath, String strPropertiesFileName)

Here you can find the source of saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath, String strPropertiesFileName)

Description

This method saves the main window location, size, and state to the properties file.

License

Open Source License

Parameter

Parameter Description
jfrm the JFrame to be set
strPropertiesFilePath path to properties file, default = "user.home/Application Data/UMassLowellCS"
strPropertiesFileName name of properties file, default = "GUIProgramming.properties" ;

Declaration

public static void saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath,
        String strPropertiesFileName) 

Method Source Code


//package com.java2s;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFrame;

public class Main {
    /** properties read from properties file */
    private static Properties props = null;

    /**/*from   w ww.  j a  v  a 2 s .co m*/
     * This method saves the main window location, size, and state to the
     * properties file.
     * @param jfrm the JFrame to be set
     * @param strPropertiesFilePath path to properties file, default = "user.home/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "GUIProgramming.properties" ;
     */
    public static void saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath,
            String strPropertiesFileName) {
        saveMainWindowLocation(jfrm, strPropertiesFilePath, strPropertiesFileName, null);
    }

    /** 
     * This method saves the main window location, size, and state to the 
     * properties file.
     * @param jfrm the JFrame whose properties are to be saved
     * @param strPropertiesFilePath path to properties file, default = "user.home/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "GUIProgramming.properties" ;
     * @param strPropertiesTitle title to write into properties file, default = "GUI Programming"
     */
    public static void saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath,
            String strPropertiesFileName, String strPropertiesTitle) {

        if (jfrm != null && props != null) {
            props.setProperty("WindowX", "" + jfrm.getX());
            props.setProperty("WindowY", "" + jfrm.getY());
            props.setProperty("WindowWidth", "" + jfrm.getWidth());
            props.setProperty("WindowHeight", "" + jfrm.getHeight());
            props.setProperty("WindowState", "" + jfrm.getState());
            saveProperties(strPropertiesFilePath, strPropertiesFileName, strPropertiesTitle);
        } else {
            return;
        }
    }

    /**
     * Attempt to save the properties file.
     */
    public static void saveProperties() {
        saveProperties(null, null, null);
    }

    /**
     * Attempt to save the properties file.
     * @param strPropertiesFilePath path to properties file, default = "user.home/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "GUIProgramming.properties" ;
     * @param strPropertiesTitle title to write into properties file, default = "GUI Programming"
     */
    public static void saveProperties(String strPropertiesFilePath, String strPropertiesFileName,
            String strPropertiesTitle) {

        // String to pass to the FileInputStream constructor
        String strPropertiesFile = constructPropertiesFileString(strPropertiesFilePath, strPropertiesFileName);

        // set default properties title
        if (strPropertiesTitle == null) {
            strPropertiesTitle = "GUI Programming";
        }

        // isolate the properties file path
        if (strPropertiesFilePath == null) {
            strPropertiesFilePath = strPropertiesFile.substring(0,
                    strPropertiesFile.lastIndexOf(System.getProperty("file.separator")));
        }

        // if the properties file path does not exist, create it
        File filePropertiesFilePath = new File(strPropertiesFilePath);
        if (!filePropertiesFilePath.exists()) {
            filePropertiesFilePath.mkdirs();
        }

        // save properties file
        // reference: Deitel & Deitel, Java How To Program, 6th ed., p. 948
        try {
            File file = new File(strPropertiesFile);
            FileOutputStream out = new FileOutputStream(file);
            props.store(out, strPropertiesTitle);
            out.close();
        } catch (FileNotFoundException fnfe) {
            System.err.println(fnfe);
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }

    /**
     * Construct the full file reference for the properties file to be loaded or saved.
     * @param strPropertiesFilePath path to properties file, default = "{user.home}/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "CompositionProject.properties" ;
     * @return String to pass to the FileInputStream or FileOutputStream constructor
     */
    static String constructPropertiesFileString(String strPropertiesFilePath, String strPropertiesFileName) {

        // if no path is defined for the properties file, set it to the
        //    UMassLowellCS directory
        if (strPropertiesFilePath == null) {
            strPropertiesFilePath = System.getProperty("user.home") + System.getProperty("file.separator")
                    + "Application Data" + System.getProperty("file.separator") + "UMassLowellCS";
        }

        // if no name is defined for the properties file, set it to a generic name
        // if a file name is defined but it does not have an extension of .properties,
        //    append that extension
        if (strPropertiesFileName == null) {
            strPropertiesFileName = "CompositionProject.properties";
        } else if (!strPropertiesFileName.toLowerCase().endsWith(".properties")) {
            strPropertiesFileName += ".properties";
        }

        // return the full constructed String to pass to the FileInputStream or
        //    FileOutputStream constructor
        return strPropertiesFilePath + System.getProperty("file.separator") + strPropertiesFileName;
    }
}

Related

  1. restoreFrame(Class pClass, final JFrame pFrame, String pFrameId)
  2. restoreFrame(JFrame frame)
  3. rightShiftDialog(JDialog dialog, JFrame parent)
  4. run(JFrame frame, int width, int height)
  5. saveFrame(Class pClass, JFrame pFrame, String pFrameId)
  6. setBlockAllUserInput(final JFrame frame, final boolean yesNo)
  7. setCursorFree(JFrame frame)
  8. setDialogLocation(JFrame frame, JFrame parentFrame)
  9. setDirty(JFrame frame, boolean isDirty)