Example usage for javax.swing JFileChooser APPROVE_SELECTION

List of usage examples for javax.swing JFileChooser APPROVE_SELECTION

Introduction

In this page you can find the example usage for javax.swing JFileChooser APPROVE_SELECTION.

Prototype

String APPROVE_SELECTION

To view the source code for javax.swing JFileChooser APPROVE_SELECTION.

Click Source Link

Document

Instruction to approve the current selection (same as pressing yes or ok).

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    MyFileChooser chooser = new MyFileChooser();
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    final JDialog dialog = chooser.createDialog(null);
    chooser.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent evt) {
            JFileChooser chooser = (JFileChooser) evt.getSource();
            if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {
                dialog.setVisible(false);
            } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) {
                dialog.setVisible(false);
            }//from www . j  av  a2s . co  m
        }
    });

    dialog.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            dialog.setVisible(false);
        }
    });

    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                System.out.println(selectedFile.getParent());
                System.out.println(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                System.out.println(JFileChooser.CANCEL_SELECTION);
            }/*  www  .  j a v a 2 s .  com*/
        }
    };
    fileChooser.addActionListener(actionListener);
    fileChooser.removeActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                System.out.println(selectedFile.getParent());
                System.out.println(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                System.out.println(JFileChooser.CANCEL_SELECTION);
            }//from  w w  w. j  a v a 2s  .c  o m
        }
    };
    fileChooser.addActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
}

From source file:FileSamplePanel.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel directoryLabel = new JLabel(" ");
    directoryLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel(" ");
    filenameLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(filenameLabel, BorderLayout.SOUTH);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    //  Create ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }//from   www.j  a  v  a2  s  .c  o  m
        }
    };
    fileChooser.addActionListener(actionListener);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * 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.//from  w  w w  .jav  a  2  s. c o  m
 * @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]);
}

From source file:nz.co.fortytwo.freeboard.installer.InstalManager.java

private void addWidgets() {

    JTabbedPane tabPane = new JTabbedPane();
    this.add(tabPane, BorderLayout.CENTER);
    // upload to arduinos
    JPanel uploadPanel = new JPanel();
    uploadPanel.setLayout(new BorderLayout());
    uploadPanel.add(uploadingPanel, BorderLayout.CENTER);
    final JPanel westUploadPanel = new JPanel(new MigLayout());

    String info = "\nUse this panel to upload compiled code to the arduino devices.\n\n"
            + "NOTE: directories with spaces will probably not work!\n\n"
            + "First select the base directory of your Arduino IDE installation, eg C:/devtools/arduino-1.5.2\n\n"
            + "Then select target files to upload, these are ended in '.hex'\n"
            + "\nand can be downloaded from github (https://github.com/rob42),\n"
            + " see the 'Release*' sub-directories\n\n"
            + "Output of the process will display in the right-side window\n\n";
    JTextArea jTextInfo = new JTextArea(info);
    jTextInfo.setEditable(false);//from w  w  w  . j a  v  a2  s  .  co m
    westUploadPanel.add(jTextInfo, "span,wrap");

    westUploadPanel.add(new JLabel("Select Arduino IDE directory:"), "wrap");
    arduinoDirTextField.setEditable(false);
    westUploadPanel.add(arduinoDirTextField, "span 2");
    arduinoIdeChooser.setApproveButtonText("Select");
    arduinoIdeChooser.setAcceptAllFileFilterUsed(false);
    arduinoIdeChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    arduinoIdeChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {

                toolsDir = new File(arduinoIdeChooser.getSelectedFile(),
                        File.separator + "hardware" + File.separator + "tools" + File.separator);
                if (!toolsDir.exists()) {
                    toolsDir = null;
                    JOptionPane.showMessageDialog(westUploadPanel, "Not a valid Arduino IDE directory");
                    return;
                }
                arduinoDirTextField.setText(arduinoIdeChooser.getSelectedFile().getAbsolutePath());
            }
        }
    });
    JButton arduinoDirButton = new JButton("Select");
    arduinoDirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            arduinoIdeChooser.showDialog(westUploadPanel, "Select");

        }
    });
    westUploadPanel.add(arduinoDirButton, "wrap");

    westUploadPanel.add(new JLabel("Select comm port:"));
    westUploadPanel.add(portComboBox, "wrap");

    westUploadPanel.add(new JLabel("Select device:"), "gap unrelated");
    westUploadPanel.add(deviceComboBox, "wrap");

    hexFileChooser.setApproveButtonText("Upload");
    hexFileChooser.setAcceptAllFileFilterUsed(false);
    hexFileChooser.addChoosableFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "*.hex - Hex file";
        }

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            if (f.getName().toUpperCase().endsWith(".HEX")) {
                return true;
            }
            return false;
        }
    });
    westUploadPanel.add(hexFileChooser, "span, wrap");

    uploadPanel.add(westUploadPanel, BorderLayout.WEST);
    tabPane.addTab("Upload", uploadPanel);

    // charts
    JPanel chartPanel = new JPanel();
    chartPanel.setLayout(new BorderLayout());
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Charts", "tiff", "kap", "KAP", "TIFF", "tif",
            "TIF");
    chartFileChooser.setFileFilter(filter);
    chartFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chartFileChooser.setMultiSelectionEnabled(true);
    final JPanel chartWestPanel = new JPanel(new MigLayout());
    String info2 = "\nUse this panel to convert charts into the correct format for FreeBoard.\n"
            + "\nYou need to select the charts or directories containing charts, then click 'Process'.\n "
            + "\nThe results will be in a directory with the same name as the chart, and the chart "
            + "\ndirectory will also be compressed into a zip file ready to transfer to your FreeBoard "
            + "\nserver\n" + "\nOutput of the process will display in the right-side window\n\n";
    JTextArea jTextInfo2 = new JTextArea(info2);
    jTextInfo2.setEditable(false);
    chartWestPanel.add(jTextInfo2, "wrap");

    chartFileChooser.setApproveButtonText("Process");
    chartWestPanel.add(chartFileChooser, "span,wrap");

    final JPanel loggingPanel = new JPanel(new MigLayout());
    loggingGroup.add(infoButton);
    loggingGroup.add(debugButton);
    debugButton.setSelected(logger.isDebugEnabled());
    infoButton.setSelected(!logger.isDebugEnabled());
    infoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (infoButton.isSelected()) {
                LogManager.getRootLogger().setLevel(Level.INFO);
            }
        }
    });
    debugButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (debugButton.isSelected()) {
                LogManager.getRootLogger().setLevel(Level.DEBUG);
            }
        }
    });

    loggingPanel.add(new JLabel("Logging Level"));
    loggingPanel.add(infoButton);
    loggingPanel.add(debugButton);
    chartWestPanel.add(loggingPanel, "span,wrap");

    final JPanel transparentPanel = new JPanel(new MigLayout());
    charsetGroup.add(utf8Button);
    charsetGroup.add(iso8859Button);
    iso8859Button.setSelected(logger.isDebugEnabled());
    utf8Button.setSelected(!logger.isDebugEnabled());
    utf8Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (utf8Button.isSelected()) {
                charset = "UTF-8";
            }
        }
    });
    iso8859Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (iso8859Button.isSelected()) {
                charset = "ISO-8859-1";
            }
        }
    });

    transparentPanel.add(new JLabel("KAP Character set:"));
    transparentPanel.add(utf8Button);
    transparentPanel.add(iso8859Button);
    chartWestPanel.add(transparentPanel);

    chartPanel.add(chartWestPanel, BorderLayout.WEST);
    chartPanel.add(processingPanel, BorderLayout.CENTER);
    tabPane.addTab("Charts", chartPanel);

    // IMU calibration
    JPanel calPanel = new JPanel();
    calPanel.setLayout(new BorderLayout());
    JPanel westCalPanel = new JPanel(new MigLayout());
    String info3 = "\nUse this panel to calibrate your ArduIMU.\n"
            + "\nYou should do this as near to the final location as possible,\n"
            + "and like all compasses, as far from wires and magnetic materials \n" + "as possible.\n"
            + "\nSelect your comm port, then click 'Start'.\n "
            + "\nSmoothly and steadily rotate the ArduIMU around all 3 axes (x,y,z)\n"
            + "several times. Then press stop and the calibration will be performed and\n"
            + "uploaded to the ArduIMU\n\n" + "Output of the process will display in the right-side window\n\n";
    JTextArea jTextInfo3 = new JTextArea(info3);
    jTextInfo3.setEditable(false);
    westCalPanel.add(jTextInfo3, "span, wrap");
    westCalPanel.add(new JLabel("Select comm port:"));

    westCalPanel.add(portComboBox1, "wrap");
    JButton startCal = new JButton("Start");
    startCal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calibrationPanel.process((String) portComboBox.getSelectedItem());
        }
    });
    westCalPanel.add(startCal);
    JButton stopCal = new JButton("Stop");
    stopCal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            calibrationPanel.stopProcess();
        }
    });
    westCalPanel.add(stopCal);

    calPanel.add(westCalPanel, BorderLayout.WEST);
    calPanel.add(calibrationPanel, BorderLayout.CENTER);

    tabPane.addTab("Calibration", calPanel);

}

From source file:ome.formats.importer.gui.FileQueueHandler.java

public void actionPerformed(ActionEvent event) {
    final String action = event.getActionCommand();
    //final File[] files = fileChooser.getSelectedFiles();

    //If the directory changed, don't show an image.
    if (action.equals(JFileChooser.APPROVE_SELECTION)) {
        doSelection(true);/*from w  w  w .j  a v a2s.co m*/
    }
}

From source file:org.zeromeaner.gui.reskin.StandaloneFrame.java

private void createCards() {
    introPanel = new StandaloneLicensePanel();
    content.add(introPanel, CARD_INTRO);

    playCard = new JPanel(new BorderLayout());
    content.add(playCard, CARD_PLAY);/*from  w w w .ja v  a  2  s  . c o m*/

    JPanel confirm = new JPanel(new GridBagLayout());
    JOptionPane option = new JOptionPane("A game is in open.  End this game?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_PLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_PLAY);
                currentCard = CARD_PLAY;
                playButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    GridBagConstraints cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_PLAY_END);

    content.add(new StandaloneModeselectPanel(), CARD_MODESELECT);

    netplayCard = new JPanel(new BorderLayout());
    netplayCard.add(netLobby, BorderLayout.SOUTH);
    content.add(netplayCard, CARD_NETPLAY);

    confirm = new JPanel(new GridBagLayout());
    option = new JOptionPane("A netplay game is open.  End this game and disconnect?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_NETPLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                netLobby.disconnect();
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_NETPLAY);
                currentCard = CARD_NETPLAY;
                netplayButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_NETPLAY_END);

    StandaloneKeyConfig kc = new StandaloneKeyConfig(this);
    kc.load(0);
    content.add(kc, CARD_KEYS_1P);
    kc = new StandaloneKeyConfig(this);
    kc.load(1);
    content.add(kc, CARD_KEYS_2P);

    StandaloneGameTuningPanel gt = new StandaloneGameTuningPanel();
    gt.load(0);
    content.add(gt, CARD_TUNING_1P);
    gt = new StandaloneGameTuningPanel();
    gt.load(1);
    content.add(gt, CARD_TUNING_2P);

    StandaloneAISelectPanel ai = new StandaloneAISelectPanel();
    ai.load(0);
    content.add(ai, CARD_AI_1P);
    ai = new StandaloneAISelectPanel();
    ai.load(1);
    content.add(ai, CARD_AI_2P);

    StandaloneGeneralConfigPanel gc = new StandaloneGeneralConfigPanel();
    gc.load();
    content.add(gc, CARD_GENERAL);

    final JFileChooser fc = FileSystemViews.get().fileChooser("replay/");

    fc.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "Zeromeaner Replay Files";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().endsWith(".zrep");
        }
    });

    fc.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))
                return;
            JFileChooser fc = (JFileChooser) e.getSource();
            String path = fc.getSelectedFile().getPath();
            if (!path.contains("replay/"))
                path = "replay/" + path;
            startReplayGame(path);
        }
    });
    fc.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
            fc.rescanCurrentDirectory();
        }
    });
    content.add(fc, CARD_OPEN);

    content.add(new StandaloneFeedbackPanel(), CARD_FEEDBACK);
}

From source file:SeedGenerator.MainForm.java

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel(" ");
    directoryLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel(" ");
    filenameLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(true);
    contentPane.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
            String command = actionEvent.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                File selectedFile = theFileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }/*from w  w w.  ja va2  s  .c o m*/
            frame.setVisible(false);
        }
    };
    fileChooser.addActionListener(actionListener);

    frame.pack();
    frame.setVisible(true); // TODO add your handling code here:
}