Here you can find the source of showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)
Parameter | Description |
---|---|
chooser | the file chooser to display. |
parent | the parent window. |
approveButtonText | the text for the approve button. |
public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)
//package com.java2s; /*//from www .ja va2s . c om * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved. * * This software is distributable under the BSD license. See the terms of the * BSD license in the documentation provided with this software. */ import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JDialog; import javax.swing.JFileChooser; public class Main { /** * Displays the given file chooser. Utility method for avoiding of memory leak in JDK * 1.3 {@link javax.swing.JFileChooser#showDialog}. * * @param chooser the file chooser to display. * @param parent the parent window. * @param approveButtonText the text for the approve button. * * @return the return code of the chooser. */ public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) { if (approveButtonText != null) { chooser.setApproveButtonText(approveButtonText); chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG); } Frame frame = (parent instanceof Frame) ? (Frame) parent : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent); String title = chooser.getDialogTitle(); if (title == null) { title = chooser.getUI().getDialogTitle(chooser); } final JDialog dialog = new 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.setLocationRelativeTo(parent); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION }; ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent ev) { if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) { retValue[0] = JFileChooser.APPROVE_OPTION; } dialog.setVisible(false); dialog.dispose(); } }; chooser.addActionListener(l); dialog.show(); return (retValue[0]); } }