Example usage for javax.swing JFileChooser addActionListener

List of usage examples for javax.swing JFileChooser addActionListener

Introduction

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

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the file chooser.

Usage

From source file:Main.java

public static void main(String[] args) {
    final JFileChooser chooser = new JFileChooser(new File(".")) {
        public void approveSelection() {
            if (getSelectedFile().exists()) {
                super.approveSelection();
            } else
                System.out.println("File doesn't exist");
        }//from  w w  w .j  a v a2 s. c  o  m
    };

    chooser.addActionListener(e -> System.out.println(e));

    chooser.setSelectedFile(new File("something.txt"));
    int returnVal = chooser.showSaveDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println(chooser.getSelectedFile());
    }

}

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 . jav a  2  s  .  c o m*/
        }
    };
    fileChooser.addActionListener(actionListener);
    frame.pack();
    frame.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);
            }//from   w w w . j a  va 2 s  .  com
        }
    };
    fileChooser.addActionListener(actionListener);
    fileChooser.removeActionListener(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(" ");
            }// w w  w .j  a va2s  .c o  m
        }
    };
    fileChooser.addActionListener(actionListener);

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

From source file:Main.java

/**
 * Displays a specified <code>JFileChooser</code> in a JInternalFrame. <br />
 * The JInternalFrame will close when the dialog is closed. <br />
 * /*from  w  w w . ja v  a2  s.c o  m*/
 * @param desktop the JDesktopPane on which to display the JFileChooser
 * @param ch the JFileChooser to display
 */
public static void showInternalFileChooser(JDesktopPane desktop, final JFileChooser ch) {
    final JInternalFrame frm = new JInternalFrame(ch.getDialogTitle());
    frm.setClosable(true);
    frm.setResizable(true);
    frm.setLayout(new BorderLayout());
    frm.add(ch, BorderLayout.CENTER);
    frm.setVisible(true);

    frm.pack();

    Dimension size = frm.getSize();
    frm.setLocation(desktop.getWidth() / 2 - size.width / 2, desktop.getHeight() / 2 - size.height / 2);
    desktop.add(frm, 0);

    ch.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            frm.dispose();
            ch.removeActionListener(this);
        }
    });

    try {
        frm.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {
    }
}

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   ww  w .  ja  v  a  2 s . co  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: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 ww  .  ja va 2  s .co  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(" ");
            }/* ww w.  j  a  va2  s .c  o  m*/
            frame.setVisible(false);
        }
    };
    fileChooser.addActionListener(actionListener);

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