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

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

Description

Set the location of a JFrame using the values in a specified properties file at a specified location.

License

Open Source License

Parameter

Parameter Description
jfrm the JFrame whose properties are 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 setMainWindowLocation(JFrame jfrm, String strPropertiesFilePath,
        String strPropertiesFileName) 

Method Source Code


//package com.java2s;

import java.io.FileInputStream;

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 ww w  . j a v  a2 s  . c o  m
     * Set the location of a JFrame using the values in a specified properties file 
     * at a specified location.
     * @param jfrm the JFrame whose properties are 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 setMainWindowLocation(JFrame jfrm, String strPropertiesFilePath,
            String strPropertiesFileName) {
        if (props == null) {
            props = new Properties();
            loadProperties(strPropertiesFilePath, strPropertiesFileName);
        }
        setMainWindowLocation(jfrm);
    }

    /**
     * This method reads the default properties file (if it exists) and
     * sets the main window location to what it was the last time the
     * program was run.
     * @param jfrm the JFrame whose properties are to be set
     */
    public static void setMainWindowLocation(JFrame jfrm) {
        int intWindowX = jfrm.getX();
        int intWindowY = jfrm.getY();
        int intWindowWidth = jfrm.getWidth();
        int intWindowHeight = jfrm.getHeight();
        int intWindowState = jfrm.getExtendedState();

        if (props == null) {
            props = new Properties();
        } else {
            if (props.getProperty("WindowX") != null) {
                intWindowX = Integer.parseInt(props.getProperty("WindowX"));
            }
            if (props.getProperty("WindowY") != null) {
                intWindowY = Integer.parseInt(props.getProperty("WindowY"));
            }
            if (props.getProperty("WindowWidth") != null) {
                intWindowWidth = Integer.parseInt(props.getProperty("WindowWidth"));
            }
            if (props.getProperty("WindowHeight") != null) {
                intWindowHeight = Integer.parseInt(props.getProperty("WindowHeight"));
            }
            if (props.getProperty("WindowState") != null) {
                intWindowState = Integer.parseInt(props.getProperty("WindowState"));
            }
        }

        // set window location
        jfrm.setLocation(intWindowX, intWindowY);

        // set window size
        // maximize window if it was maximized when it was last closed
        // note that getProperty returns a String but MAXIMIZED_BOTH is an int
        //    therefore, the comparison is done with strings
        if (intWindowState == JFrame.MAXIMIZED_BOTH) {
            jfrm.setExtendedState(JFrame.MAXIMIZED_BOTH);
        } else { // set stored window size if it was not maximized
            jfrm.setSize(intWindowWidth, intWindowHeight);
        }
    }

    /**
     * Attempt to open the default properties file and load its contents.
     * @return true if properties file is opened and loaded successfully, false otherwise
     */
    public static boolean loadProperties() {
        return loadProperties(null, null);
    }

    /**
     * Attempt to open a properties file and load its contents.
     * @param strPropertiesFilePath path to properties file, default = "{user.home}/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "CompositionProject.properties" ;
     * @return true if properties file is opened and loaded successfully, false otherwise
     */
    public static boolean loadProperties(String strPropertiesFilePath, String strPropertiesFileName) {

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

        // create Properties object if it does not exist
        if (props == null) {
            props = new Properties();
        }

        try {
            // load properties from file
            FileInputStream in = new FileInputStream(strPropertiesFile);
            props.load(in);
            in.close();
            return true;
        } catch (IOException ioe) {
            // System.out.println(ioe.getMessage());
            return false;
        }
    }

    /**
     * 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. setFramePositon(JFrame inTargetFrame)
  2. setFrameTitle(JFrame frame, File file)
  3. setGeneralParameters(final JFrame window)
  4. setInfo(String info, JFrame root)
  5. setJFrameAlwaysOnTop(JFrame frame, boolean newValue)
  6. setPanel(JPanel parent, JPanel child, JFrame f)
  7. setPersistentExtendedStateMask(JFrame frame, int persistentExtendedStateMask)
  8. setWindowListenerDispose(JFrame frame)
  9. setWindowRightSide(final JFrame frame)