/** * Displays a custom file chooser sheet with a custom approve button. * * @param chooser the chooser * @param parent the parent component of the dialog; can be {@code null} * @param approveButtonText the text of the {@code ApproveButton} * @param listener The listener for SheetEvents. */ public static void showSheet( final JFileChooser chooser, Component parent, String approveButtonText, SheetListener listener) { if (approveButtonText != null) { chooser.setApproveButtonText(approveButtonText); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); } // Begin Create Dialog Frame frame = parent instanceof Frame ? (Frame) parent : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); String title = chooser.getUI().getDialogTitle(chooser); chooser.getAccessibleContext().setAccessibleDescription(title); final JSheet sheet = new JSheet(frame); sheet.addSheetListener(listener); Container contentPane = sheet.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(chooser, BorderLayout.CENTER); // End Create Dialog final ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent evt) { int option; if (evt.getActionCommand().equals("ApproveSelection")) { option = JFileChooser.APPROVE_OPTION; } else { option = JFileChooser.CANCEL_OPTION; } sheet.hide(); sheet.fireOptionSelected(chooser, option); chooser.removeActionListener(this); } }; chooser.addActionListener(actionListener); sheet.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { sheet.fireOptionSelected(chooser, JFileChooser.CANCEL_OPTION); chooser.removeActionListener(actionListener); } }); chooser.rescanCurrentDirectory(); sheet.pack(); sheet.show(); sheet.toFront(); }
private static JSheet createSheet(final JOptionPane pane, Component parentComponent, int style) { Window window = getWindowForComponent(parentComponent); final JSheet sheet; if (window instanceof Frame) { sheet = new JSheet((Frame) window); } else { sheet = new JSheet((Dialog) window); } JComponent contentPane = (JComponent) sheet.getContentPane(); contentPane.setLayout(new BorderLayout()); if (isNativeSheetSupported()) { contentPane.setBorder(new EmptyBorder(12, 0, 0, 0)); } contentPane.add(pane, BorderLayout.CENTER); sheet.setResizable(false); sheet.addWindowListener( new WindowAdapter() { private boolean gotFocus = false; @Override public void windowClosing(WindowEvent we) { pane.setValue(null); } @Override public void windowClosed(WindowEvent we) { if (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) { sheet.fireOptionSelected(pane); } } @Override public void windowGainedFocus(WindowEvent we) { // Once window gets focus, set initial focus if (!gotFocus) { // Ugly dirty hack: JOptionPane.selectInitialValue() is protected. // So we call directly into the UI. This may cause mayhem, // because we override the encapsulation. // pane.selectInitialValue(); OptionPaneUI ui = pane.getUI(); if (ui != null) { ui.selectInitialValue(pane); } gotFocus = true; } } }); sheet.addComponentListener( new ComponentAdapter() { @Override public void componentShown(ComponentEvent ce) { // reset value to ensure closing works properly pane.setValue(JOptionPane.UNINITIALIZED_VALUE); } }); pane.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { // Let the defaultCloseOperation handle the closing // if the user closed the window without selecting a button // (newValue = null in that case). Otherwise, close the sheet. if (sheet.isVisible() && event.getSource() == pane && (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { sheet.setVisible(false); sheet.fireOptionSelected(pane); } } }); sheet.pack(); return sheet; }