centeredfilechooser.java

来自「MyDownloader 是一款使用 http 协议(RFC 1867)用于下载」· Java 代码 · 共 142 行

JAVA
142
字号
/* * Copyright 2007 JavaAtWork All rights reserved. * Use is subject to license terms. */package com.javaatwork.mydownloader.dialog;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.HeadlessException;import java.awt.Toolkit;import java.awt.event.MouseListener;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JList;import javax.swing.SwingUtilities;import javax.swing.UIManager;import com.javaatwork.mydownloader.utils.LocaleManager;import com.javaatwork.mydownloader.utils.Logger;/** * A JFileChooser that is in the center of the screen.  *  * @author Johannes Postma */ public class CenteredFileChooser extends JFileChooser {	/**	 * serialVersionUID	 */	private static final long serialVersionUID = -5006688261460210335L;	/**	 * Creates a new CenteredFileChooser	 */	public CenteredFileChooser() {		super();		UIManager.put("FileChooser.readOnly", Boolean.TRUE);		this.setMultiSelectionEnabled(true);		this.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);				// remove the file rename functionality in the JFileChooser		// this is done for JDK 1.4 and less		// the preferred way is UIManager.put("FileChooser.readOnly", Boolean.TRUE) (JDK 1.5) see above;		JList list = searchJList(this);		if (list != null) {			String listenerClassName;			MouseListener[] listeners = list.getMouseListeners();			for (int i = 0; i < listeners.length; i++) {				listenerClassName = listeners[i].getClass().getName();				if (listenerClassName.indexOf("SingleClick") != -1) {					list.removeMouseListener(listeners[i]);					break;				}			}		}					final String dialogTitle = LocaleManager.getInstance().getDialogTitle();		// the dialogtitle must come from a resourcebundle		// the title is not given in the english resourcebundle		// and therefore the return dialogtitle is null		// the default dialogtitle will be used.		if (dialogTitle != null) {			try {				SwingUtilities.invokeLater(new Runnable() {					public void run() {						setDialogTitle(dialogTitle);					}				});			} catch (Exception e) {				Logger.log("CenteredFileChooser", "CenteredFileChooser()", e.toString());			}		}			}	/**	 * Creates a JDialog.	 * 	 * @param parent The parent component.	 * @throws HeadlessException If an error occurred.	 */	protected JDialog createDialog(Component parent) throws HeadlessException {		JDialog dialog = super.createDialog(parent);				//  sets the frame to the center		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();		Dimension frameSize = dialog.getSize();		if (frameSize.height > screenSize.height) {			frameSize.height = screenSize.height;		}		if (frameSize.width > screenSize.width) {			frameSize.width = screenSize.width;		}		dialog.setLocation(			(screenSize.width - frameSize.width) / 2,			(screenSize.height - frameSize.height) / 2);				return dialog;	}		/**	 * Returns the JList of a JFileChooser.	 * 	 * @param fileChooser The JFileChooser.	 * @return JList	 */	private JList searchJList(Container fileChooser) {				JList ret = null;				if (fileChooser instanceof JList) {			ret = (JList) fileChooser;		}			else {			Component[] children = fileChooser.getComponents();						for (int i = children.length - 1; i >= 0; i--) {				if (children[i] instanceof Container) {					ret = searchJList((Container) children[i]);					if (ret != null) {						break;					}				}			}		}		return ret;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?