swing Ctrl+S 保存配置

以下是使用Java swing写的一个小工具:
swing Ctrl+S 保存配置
 
swing Ctrl+S 保存配置
 

我现在不细说功能,而是说明如何保存使用痕迹,比如上图,我在"old path"输入了"com\common\jn\img\path.png",我想下次打开时"old path"自动保存上一次的记录.

如何实现呢?

(1)可以把文本框的内容保存到properties配置文件中;

(2)关闭窗口或者按下Ctrl+S 时触发保存事件

(3)下次启动窗口时,从配置文件中读取使用痕迹,并恢复到界面上.

具体实现如下

配置文件为

Java代码  swing Ctrl+S 保存配置
  1. public static final String configFilePath="C:\\.path_config.properties";  

 

(a)获取界面上文本框的内容,并设置到properties中,然后写入磁盘

Java代码  swing Ctrl+S 保存配置
  1. /*** 
  2.      * 保存到配置文件中 
  3.      * @throws IOException  
  4.      */  
  5.     protected void saveConfig() throws IOException{  
  6.         File configFile=new File(configFilePath);  
  7.         if(!configFile.exists()){  
  8.             try {  
  9.                 SystemHWUtil.createEmptyFile(configFile);  
  10.             } catch (IOException e) {  
  11.                 e.printStackTrace();  
  12.                 GUIUtil23.errorDialog(e);  
  13.             }  
  14.         }  
  15.         CMDUtil.show(configFilePath);//因为隐藏文件是只读的  
  16. //        FileUtils.writeToFile(configFilePath, content);  
  17.         if(ValueWidget.isNullOrEmpty(props)){  
  18.             props= new Properties();  
  19.         }  
  20.         String old_path=oldPathTF.getText();  
  21.         if(!ValueWidget.isNullOrEmpty(old_path)){  
  22.             props.setProperty(PROP_KEY_OLD_PATH, old_path);  
  23.         }  
  24.           
  25.         String folder1=this.compareFolderPanel.getFolderOneTextField().getText();  
  26.         if(!ValueWidget.isNullOrEmpty(folder1)){  
  27.             props.setProperty(PROP_KEY_COMPARED_FOLDERONE, folder1);  
  28.         }  
  29.         String folder2=this.compareFolderPanel.getFolder2TextField().getText();  
  30.         if(!ValueWidget.isNullOrEmpty(folder2)){  
  31.             props.setProperty(PROP_KEY_COMPARED_FOLDER2, folder2);  
  32.         }  
  33.         String sourceFile=this.checkSamePanel.getSourceFileTF().getText();  
  34.         if(!ValueWidget.isNullOrEmpty(sourceFile)){  
  35.             props.setProperty(PROP_KEY_SOURCE_FILE, sourceFile);  
  36.         }  
  37.         String targetFile=this.checkSamePanel.getTargetFileTF().getText();  
  38.         if(!ValueWidget.isNullOrEmpty(targetFile)){  
  39.             props.setProperty(PROP_KEY_TARGET_FILE, targetFile);  
  40.         }  
  41.         /*** 
  42.          * 增量包中,复制class时是否弹出确认提示框 
  43.          */  
  44.         boolean isSure=createFolderByPackage.getSureCheckbox().isSelected();  
  45.         props.setProperty(PROP_KEY_COPY_JAVA_SURE, String.valueOf(isSure));  
  46.           
  47.         boolean isCopyPath=copyCheckbox.isSelected();  
  48.         props.setProperty(PROP_KEY_IS_COPY_PATH, String.valueOf(isCopyPath));  
  49.         
  50.         
  51.         setCombox(PROP_KEY_ROOT_PATHS, this.createFolderByPackage.getRootFolderTextField(),this.createFolderByPackage.getRootPathComboBox());  
  52.         setCombox(PROP_KEY_JAVA_FILE_PATHS, this.createFolderByPackage.getClassTextField(),this.createFolderByPackage.getJavaPathComboBox());  
  53.           
  54.         OutputStream out=new FileOutputStream(configFile);  
  55.         props.store(out, TimeHWUtil.formatDateTimeZH(null));  
  56.         out.close();//及时关闭资源  
  57.         CMDUtil.hide(configFilePath);//隐藏文件:attrib ".mqtt_client.properties" +H  
  58.         System.out.println("save complete.");  
  59.     }  
  60.    /*** 
  61.      * 以;;分隔 
  62.      * @param prop_key 
  63.      * @param tf 
  64.      */  
  65.     private void setCombox(String prop_key,JTextField tf,JComboBox<String>comboBox){  
  66.         String rootPath=tf.getText();  
  67.         if(ValueWidget.isNullOrEmpty(rootPath)){  
  68.             return;  
  69.         }  
  70.         String roots=props.getProperty(prop_key);  
  71.         if(ValueWidget.isNullOrEmpty(roots)){  
  72.             roots=rootPath;  
  73.         }else{  
  74.             String roots_old[]=roots.split(SHAREDPICDIVISION);  
  75.             if(!SystemHWUtil.isContains(roots_old, rootPath)){  
  76.                 roots=roots+SHAREDPICDIVISION+rootPath;  
  77.                 comboBox.addItem(rootPath);  
  78.             }  
  79.             String urls_old2[]=roots.split(SHAREDPICDIVISION);  
  80.             String urls_new[]=SystemHWUtil.unique(urls_old2);  
  81.             roots=StringUtils.join(urls_new, SHAREDPICDIVISION);  
  82.         }  
  83.         props.setProperty(prop_key, roots);  
  84.     }  

 注意:CMDUtil.show 是去掉配置文件的隐藏属性,因为隐藏文件不可写.

(b)窗口关闭时触发保存事件

Java代码  swing Ctrl+S 保存配置
  1. this.addWindowListener(new WindowAdapter() {  
  2.   
  3.             @Override  
  4.             public void windowClosing(WindowEvent e) {  
  5.                 System.out.println("closing....");  
  6.                 try {  
  7.                     saveConfig();  
  8.                 } catch (IOException e1) {  
  9.                     e1.printStackTrace();  
  10.                     GUIUtil23.errorDialog(e1);  
  11.                 }  
  12.                 super.windowClosing(e);  
  13.             }  
  14.   
  15.             @Override  
  16.             public void windowClosed(WindowEvent e) {  
  17.                 /*System.out.println("closed"); 
  18.                 try { 
  19.                     saveConfig(); 
  20.                 } catch (IOException e1) { 
  21.                     e1.printStackTrace(); 
  22.                 }*/  
  23.                 super.windowClosed(e);  
  24.             }  
  25.         });  

 注意:窗口关闭时只会执行windowClosing(),而不会执行windowClosed()
swing Ctrl+S 保存配置
 

 

(c)增加全局快捷键

Java代码  swing Ctrl+S 保存配置
  1. /*** 
  2.          * 增加全局快捷键.<Br> 
  3.          * Ctrl+S,导致"比例"文本框聚焦 
  4.          */  
  5.         protected void setGlobalShortCuts() {  
  6.             // Add global shortcuts  
  7.             Toolkit toolkit = Toolkit.getDefaultToolkit();  
  8.             // 注册应用程序全局键盘事件, 所有的键盘事件都会被此事件监听器处理.  
  9.             toolkit.addAWTEventListener(new java.awt.event.AWTEventListener() {  
  10.                 public void eventDispatched(AWTEvent event) {  
  11.                     if (event.getClass() == KeyEvent.class) {  
  12.                         KeyEvent kE = ((KeyEvent) event);  
  13.                         // 处理按键事件 Ctrl+S  
  14.                         if (kE.getKeyCode() == KeyEvent.VK_S  
  15.                                 && kE.isControlDown()&&!kE.isAltDown()  
  16.                                 && kE.getID() == KeyEvent.KEY_PRESSED) {  
  17.                             try {  
  18.                                 saveConfig();  
  19.                             } catch (IOException e) {  
  20.                                 e.printStackTrace();  
  21.                                 GUIUtil23.errorDialog(e);  
  22.                             }  
  23.                         }   
  24.                     }  
  25.                 }  
  26.             }, java.awt.AWTEvent.KEY_EVENT_MASK);  
  27.   
  28.         }  

 注意:为什么要加上条件 kE.getID() == KeyEvent.KEY_PRESSED,根本原因是为了防止触发两次

 

(d)启动程序时,先读取配置文件

Java代码  swing Ctrl+S 保存配置
  1. /*** 
  2.      * 读取配置文件 
  3.      * @throws IOException 
  4.      */  
  5.     private void readConfig() throws IOException{  
  6.         File configFile=new File(configFilePath);  
  7.         if(configFile.exists()){  
  8.             InputStream inStream=new FileInputStream(configFile);  
  9.             props.load(inStream);  
  10.             inStream.close();//及时关闭资源  
  11.         }  
  12.         String old_path=getPropValue(PROP_KEY_OLD_PATH);  
  13.         //"比较文件夹"  
  14.         String compared_folderOne=getPropValue(PROP_KEY_COMPARED_FOLDERONE);  
  15.         //"比较文件夹"  
  16.         String compared_folder2=getPropValue(PROP_KEY_COMPARED_FOLDER2);  
  17.         //增量包  
  18.         String root_path=getPropValue(PROP_KEY_ROOT_PATHS);  
  19.         //增量包  
  20.         String java_path=getPropValue(PROP_KEY_JAVA_FILE_PATHS);  
  21.         //比较二进制文件  
  22.         String sourceFile=getPropValue(PROP_KEY_SOURCE_FILE);  
  23.         //比较二进制文件  
  24.         String targetFile=getPropValue(PROP_KEY_TARGET_FILE);  
  25.         String isSureStr=getPropValue(PROP_KEY_COPY_JAVA_SURE);  
  26.         String isCopyPath=getPropValue(PROP_KEY_IS_COPY_PATH);  
  27.         if(!ValueWidget.isNullOrEmpty(old_path)){  
  28.             oldPathTF.setText(old_path);  
  29.         }  
  30.         if(!ValueWidget.isNullOrEmpty(root_path)){  
  31.             String roots[]=root_path.split(SHAREDPICDIVISION);  
  32.             ComponentUtil.fillComboBox(createFolderByPackage.getRootPathComboBox(), roots);  
  33.         }  
  34.         if(!ValueWidget.isNullOrEmpty(java_path)){  
  35.             String java_paths[]=java_path.split(SHAREDPICDIVISION);  
  36.             ComponentUtil.fillComboBox(createFolderByPackage.getJavaPathComboBox(), java_paths);  
  37.         }  
  38.         if(!ValueWidget.isNullOrEmpty(compared_folderOne)){  
  39.             compareFolderPanel.getFolderOneTextField().setText(compared_folderOne);;  
  40.         }  
  41.         if(!ValueWidget.isNullOrEmpty(compared_folder2)){  
  42.             compareFolderPanel.getFolder2TextField().setText(compared_folder2);;  
  43.         }  
  44.         if(!ValueWidget.isNullOrEmpty(sourceFile)){  
  45.             checkSamePanel.getSourceFileTF().setText(sourceFile);  
  46.         }  
  47.         if(!ValueWidget.isNullOrEmpty(targetFile)){  
  48.             checkSamePanel.getTargetFileTF().setText(targetFile);  
  49.         }  
  50.         if(!ValueWidget.isNullOrEmpty(isSureStr)){  
  51.             boolean  isSure2=Boolean.parseBoolean(isSureStr);  
  52.             createFolderByPackage.getSureCheckbox().setSelected(isSure2);  
  53.         }  
  54.         if(!ValueWidget.isNullOrEmpty(isCopyPath)){  
  55.             boolean  isCopyPath2=Boolean.parseBoolean(isCopyPath);  
  56.             copyCheckbox.setSelected(isCopyPath2);  
  57.         }  
  58.     }  

 

上述代码中的一些变量声明:

Java代码  swing Ctrl+S 保存配置
  1. public static final String configFilePath="C:\\.path_config.properties";  
  2.     private Properties props=new Properties();  
  3.     /*** 
  4.      * path tools 
  5.      */  
  6.     public static final String PROP_KEY_OLD_PATH="old_path";  
  7.     /*** 
  8.      * 比较文件夹 
  9.      */  
  10.     public static final String PROP_KEY_COMPARED_FOLDERONE="compared_folderOne";  
  11.     /*** 
  12.      * 比较文件夹 
  13.      */  
  14.     public static final String PROP_KEY_COMPARED_FOLDER2="compared_folder2";  
  15.     /*** 
  16.      * 增量包 
  17.      */  
  18.     public static final String PROP_KEY_ROOT_PATHS="root_paths";  
  19.     /** 
  20.      * 增量包的"class文件" 
  21.      */  
  22.     public static final String PROP_KEY_JAVA_FILE_PATHS="java_paths";  
  23.     /*** 
  24.      * 比较二进制文件 
  25.      */  
  26.     public static final String PROP_KEY_SOURCE_FILE="sourceFile";  
  27.     /*** 
  28.      * 比较二进制文件 
  29.      */  
  30.     public static final String PROP_KEY_TARGET_FILE="targetFile";  
  31.     /*** 
  32.      * 增量包中复制class时是否弹出确认提示框 
  33.      */  
  34.     public static final String PROP_KEY_COPY_JAVA_SURE="isSure";  
  35.     /*** 
  36.      * 是否复制路径 
  37.      */  
  38.     public static final String PROP_KEY_IS_COPY_PATH="isCopyPath";  
  39.     public static final String SHAREDPICDIVISION=";;";  

 注意:

(1)window中隐藏文件是只读的,不可写
swing Ctrl+S 保存配置
 

 

(2)对于下拉框,多个路径之间使用两个分号进行分割

配置文件范例:
swing Ctrl+S 保存配置