Java development notes (one hundred thirty-five) Swing file dialog

In addition to conventional prompt dialog box, there is a dialog box is also very common, it is called the file dialog. File dialog box is divided into two subcategories: Open File dialog box, save the file dialog, but they are used to express the type of JFileChooser in Swing. The following is a commonly used method of JFileChooser Description:
The title of the file dialog: setDialogTitle.
setApproveButtonText: Setting the OK button text.
setCurrentDirectory: Set the initial directory file dialog.
setMultiSelectionEnabled: Set whether to support select multiple files. Value true support multiple choice, false representation does not support multiple selection, the default does not allow multiple selections.
setFileSelectionMode: Select mode settings file. There are three selection modes: JFileChooser.FILES_ONLY (only files, but the real test will be found to display the directory), JFileChooser.DIRECTORIES_ONLY (show only directory), JFileChooser.FILES_AND_DIRECTORIES (display files and directories).
setFileFilter: Set the file selection filters.
setDialogType: Settings dialog box type. This is representative of the value JFileChooser.OPEN_DIALOG File Open dialog box, JFileChooser.SAVE_DIALOGG represent this is the file save dialog.
showOpenDialog: File Open dialog box. The return value reflects whether or not the file selection, when expressed in JFileChooser.APPROVE_OPTION pressed the OK button on the dialog, when expressed in JFileChooser.CANCEL_OPTION pressed the cancel button on the dialog.
showSaveDialog: File Save dialog box is displayed. The return value of the method described with showOpenDialog.
getSelectedFile: Get File object that is currently selected.
getSelectedFiles: only in the case of multiple choice, access to an array of objects currently selected file.
Among the methods described above, particularly setFileFilter should be noted that, at first glance the input parameters for the method FileFilter type, but it is not java.io following file filters, but Swing own file dialog filter. This filter is compared with the same name as the filter IO library, as has accept method determines whether the current file satisfies the filter criteria; except that, the file dialog filter getDescription more a method, the method returns a string to be displayed is in the interior of the file type drop-down list box, which is equivalent to the file type to be a supplement. For example, txt type commonly known as a text file, jpg, gif, png several types collectively known as image files, ppt, pptx file type is called a slide and so on. Filter specific file calling code examples are as follows:

		JFileChooser chooser = new JFileChooser (); // create a file dialog 
		chooser.setCurrentDirectory (new File ( "E: /")); // set the current directory the file dialog 
		chooser.setFileFilter (new FileFilter () {// set file dialog file filters 
			@Override 
			public boolean the Accept (file file) {// determine whether the current file meets filter criteria, only displayed in the dialog will meet the conditions 
				// directory to meet the conditions, the extension of txt the condition files 
				return file.isDirectory () || file.getName () the toLowerCase () endsWith ( "TXT.");.. 
			} 

			@Override 
			public String getDescription () {// Get the description of the filter 
				return "*. TXT (text file) "; 
			} 
		});

Then take a look at how the demo file dialog box, you'd click a button to register the listener, call the method showOpenDialog File dialog box pops up when you click the button. To File Open dialog box, for example, a specific calling code looks like this:

		btnOpenFile.addActionListener (new ActionListener () {// button to register a click listener 
			@Override 
			public void actionPerformed (ActionEvent E) {// click event occurs 
				// type settings file dialog box, where the dialog box ready to open file 
				chooser.setDialogType (JFileChooser.OPEN_DIALOG); 
				// file open dialog 
				int the Result = chooser.showOpenDialog (Frame); 
				IF (the Result == JFileChooser.APPROVE_OPTION) {// click the OK button 
					// get the file file dialog box, select 
					file file chooser.getSelectedFile = (); 
					label.setText ( "<HTML> is ready to open the file path:" file.getAbsolutePath + () + "</ HTML>"); 
				} the else {/ / not OK button 
					label.setText ( "cancel open file"); 
				} 
			} 
		});

 

Run the test program, click the button pops open file dialog box as shown below.


Double-click the file dialog to enter the inside of the lower directory, find a text file and click it, a column "filename" File dialog box displays the name of the file, it said it had selected the file, then the dialog interface as shown below shows.

Then click the dialog box below the "Open" button to return to the program as shown in the main interface, the main interface visible success just know the full path to the selected file.



Click another button to register the same listener, call showSaveDialog method when you click the button to bring up the file save dialog box, this time calling code looks like this:

		btnSaveFile.addActionListener(new ActionListener() { // 给按钮注册一个点击监听器
			@Override
			public void actionPerformed(ActionEvent e) { // 发生了单击事件
				// 设置文件对话框的类型,这里的对话框准备保存文件
				chooser.setDialogType(JFileChooser.SAVE_DIALOG);
				// 显示文件保存的对话框
				int result = chooser.showSaveDialog(frame);
				if (result == JFileChooser.APPROVE_OPTION) { // 单击了确定按钮
					// 获取在文件对话框中选择的文件
					File file = chooser.getSelectedFile();
					label.setText("<html>准备保存的文件路径为:" + file.getAbsolutePath() + "</html>");
				} else { // 未单击确定按钮
					label.setText("取消保存文件");
				}
			}
		});

 

运行测试程序,单击按钮弹出的文件保存对话框如下图所示。


与文件打开对话框相比,文件保存对话框的左上角标题由“打开”改为“保存”,下方的“打开”按钮改为“保存”按钮,除了这两个地方有变化,其它都一模一样。在对话框的“文件名”一栏填写待保存的文件名,然后单击“保存”按钮,回到如下图所示的程序主界面,可见主界面成功获知那个待保存文件的完整路径。


文件对话框的内部字体也不能通过setFont方法直接修改,原因很简单,对话框只是一个框架,框架内部又有许多控件,故而需要遍历这些内部控件,再一一设置每个控件的文本字体。详细的对话框字体设置方法定义如下:

	// 设置对话框的内部字体。第一个参数需要传入文件对话框的实例
	private static void setComponentFont(Component component, Font font) {
		component.setFont(font); // 设置当前组件的字体
		if (component instanceof Container) { // 如果该组件是容器
			Container container = (Container) component; // 把该组件强制转为容器
			int count = container.getComponentCount(); // 获取容器内部的组件数量
			for (int i = 0; i < count; i++) { // 遍历该容器的所有组件
				// 给每个组件再设置一遍内部字体
				setComponentFont(container.getComponent(i), font);
			}
		}
	}

 



更多Java技术文章参见《Java开发笔记(序)章节目录

Guess you like

Origin www.cnblogs.com/pinlantu/p/11330913.html