Here you can find the source of showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
Parameter | Description |
---|---|
parent | a parameter |
approveButtonText | a parameter |
public static final int showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
//package com.java2s; /*/* w w w. ja va2 s . c o m*/ * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ import java.awt.*; public class Main { /** Utility method for avoiding of memory leak in JDK 1.3 / JFileChooser.showDialog(...) * @param parent * @param approveButtonText * @deprecated Not needed in JDK 1.4. */ public static final int showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText) { if (approveButtonText != null) { chooser.setApproveButtonText(approveButtonText); chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG); } Frame frame = null; Dialog parentDlg = null; if (parent instanceof Dialog) parentDlg = (Dialog) parent; else frame = parent instanceof java.awt.Frame ? (Frame) parent : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(Frame.class, parent); String title = chooser.getDialogTitle(); if (title == null) { title = chooser.getUI().getDialogTitle(chooser); } final javax.swing.JDialog dialog; if (parentDlg != null) dialog = new javax.swing.JDialog(parentDlg, title, true); else dialog = new javax.swing.JDialog(frame, title, true); dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); Container contentPane = dialog.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(chooser, BorderLayout.CENTER); dialog.pack(); dialog.setBounds(findCenterBounds(parent.getGraphicsConfiguration(), dialog.getSize())); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION }; java.awt.event.ActionListener l = new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent ev) { if (ev.getActionCommand() == javax.swing.JFileChooser.APPROVE_SELECTION) { retValue[0] = javax.swing.JFileChooser.APPROVE_OPTION; } dialog.setVisible(false); dialog.dispose(); } }; chooser.addActionListener(l); dialog.show(); return retValue[0]; } /** * Helps client code place components on the center of the screen. It * handles multiple monitor configuration correctly * * @param componentSize the size of the component * @return bounds of the centered component * * @since 2.5 */ public static Rectangle findCenterBounds(Dimension componentSize) { return findCenterBounds(getCurrentGraphicsConfiguration(), componentSize); } /** * Helps client code place components on the center of the screen. It * handles multiple monitor configuration correctly * * @param gconf the GraphicsConfiguration of the monitor * @param componentSize the size of the component * @return bounds of the centered component */ private static Rectangle findCenterBounds(GraphicsConfiguration gconf, Dimension componentSize) { if (gconf == null) gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration(); Rectangle bounds = gconf.getBounds(); return new Rectangle(bounds.x + (bounds.width - componentSize.width) / 2, bounds.y + (bounds.height - componentSize.height) / 2, componentSize.width, componentSize.height); } /** * Finds out the monitor where the user currently has the input focus. * This method is usually used to help the client code to figure out on * which monitor it should place newly created windows/frames/dialogs. * * @return the GraphicsConfiguration of the monitor which currently has the * input focus */ private static GraphicsConfiguration getCurrentGraphicsConfiguration() { Frame[] frames = Frame.getFrames(); for (int i = 0; i < frames.length; i++) { if (javax.swing.SwingUtilities.findFocusOwner(frames[i]) != null) { return frames[i].getGraphicsConfiguration(); } } return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); } }