List of usage examples for javax.swing JFileChooser getSelectedFile
public File getSelectedFile()
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 w ww . j a v a 2s. c o m*/ } }; fileChooser.addActionListener(actionListener); frame.pack(); frame.setVisible(true); }
From source file:FileChooserDialog.java
public static void main(String[] args) { JFileChooser fileopen = new JFileChooser(); FileFilter filter = new FileNameExtensionFilter("c files", "c"); fileopen.addChoosableFileFilter(filter); int ret = fileopen.showDialog(null, "Open file"); if (ret == JFileChooser.APPROVE_OPTION) { File file = fileopen.getSelectedFile(); System.out.println(file); }// w w w . ja v a 2s . c o m }
From source file:MainClass.java
public static void main(String[] a) { JFileChooser fileChooser = new JFileChooser("."); FileView view = new JavaFileView(); fileChooser.setFileView(view);//from w ww .ja v a 2s . c o m int status = fileChooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println(selectedFile.getParent()); System.out.println(selectedFile.getName()); } else if (status == JFileChooser.CANCEL_OPTION) { System.out.println("JFileChooser.CANCEL_OPTION"); } }
From source file:com.acmutv.ontoqa.GrammalexMain.java
/** * The app main method, executed when the program is launched. * @param args The command line arguments. * @throws IllegalAccessException /* w ww. ja v a 2s.c o m*/ * @throws InstantiationException */ public static void main(String[] args) throws InstantiationException, IllegalAccessException { //CliService.handleArguments(args); RuntimeManager.registerShutdownHooks(new ShutdownHook()); try { Path path = FileSystems.getDefault().getPath("data/lexicon").toAbsolutePath(); String currentDirectory = path.toString(); final JFileChooser fc = new JFileChooser(currentDirectory); int returnVal = fc.showOpenDialog(null); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); if (returnVal == JFileChooser.OPEN_DIALOG) { File file = fc.getSelectedFile(); System.out.println("File Select: " + file.getName() + "\n\n"); List<LexicalEntry> lEntries = LexiconUsage.getLexicalEntries(file.getAbsolutePath(), "", LexiconFormat.RDFXML); Grammar grammar = SerializeSltag.getAllElementarySltag(lEntries); SerializeSltag.writeGrammarOnFile(grammar, fileJson); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.exit(0); }
From source file:Main.java
public static void main(String[] args) { String path = System.getProperty("user.dir", "."); File dir = new File(path); JFileChooser jfc = new JFileChooser(dir); int result = jfc.showOpenDialog(null); switch (result) { case JFileChooser.CANCEL_OPTION: System.out.println("User cancelled OPEN dialog."); break;//from w w w. j a v a 2 s. c om case JFileChooser.APPROVE_OPTION: System.out.println("User chose file: " + jfc.getSelectedFile()); break; case JFileChooser.ERROR_OPTION: System.out.println("User encountered an error"); break; default: System.out.println("Confused"); break; } System.exit(0); }
From source file:DesktopDemo.java
public static void main(String[] args) { if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); } else {/*from w w w.j a v a 2 s. co m*/ System.out.println("Desktop class is not supported"); System.exit(1); } JMenuItem openItem = new JMenuItem("Open"); JMenuItem editItem = new JMenuItem("Edit"); JMenuItem printItem = new JMenuItem("Print"); JMenuItem browseToItem = new JMenuItem("Go to www.java2s.com"); JMenuItem mailToItem = new JMenuItem("Email to a@java.com"); JMenu fileMenu = new JMenu("File"); JMenu mailMenu = new JMenu("Email"); JMenu browseMenu = new JMenu("Browser"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.open(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(openItem); editItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.edit(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(editItem); printItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { desktop.print(chooser.getSelectedFile().getAbsoluteFile()); } catch (Exception ex) { ex.printStackTrace(); } } } }); fileMenu.add(printItem); browseToItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URI browseURI = new URI("www.java2s.com"); desktop.browse(browseURI); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }); browseMenu.add(browseToItem); mailToItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URI mailURI = new URI("mailto:support@java.com"); desktop.mail(mailURI); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }); mailMenu.add(mailToItem); JMenuBar jMenuBar = new JMenuBar(); jMenuBar.add(fileMenu); jMenuBar.add(browseMenu); jMenuBar.add(mailMenu); JFrame frame = new JFrame(); frame.setTitle("Desktop Helper Applications"); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(jMenuBar); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFileChooser fileChooser = new JFileChooser("."); FileFilter filter1 = new ExtensionFileFilter("JPG and JPEG", new String[] { "JPG", "JPEG" }); fileChooser.setFileFilter(filter1);// ww w . j a va2s. com int status = fileChooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println(selectedFile.getParent()); System.out.println(selectedFile.getName()); } else if (status == JFileChooser.CANCEL_OPTION) { System.out.println(JFileChooser.CANCEL_OPTION); } }
From source file:Main.java
public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton loadButton = new JButton("Display Image"); loadButton.addActionListener(ev -> { JFileChooser fc = new JFileChooser(System.getProperty("user.home")); fc.addChoosableFileFilter(/*w ww. j a v a 2 s . com*/ new FileNameExtensionFilter("Image files", new String[] { "png", "jpg", "jpeg", "gif" })); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { Image image = ImageIO.read(fc.getSelectedFile()); if (image != null) { JPanel panel = new JPanel(new BorderLayout(10, 10)); panel.add(new JLabel(fc.getSelectedFile().toString()), BorderLayout.NORTH); panel.add(new JLabel(new ImageIcon(image))); JOptionPane.showMessageDialog(frame, panel); } } catch (Exception ex) { ex.printStackTrace(); } } }); frame.add(loadButton); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(300, 500);//from w ww .j a va 2 s .c o m f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pan = new JPanel(new GridLayout(1, 1)); XmlJTree myTree = new XmlJTree(null); f.add(new JScrollPane(myTree)); JButton button = new JButton("Choose file"); button.addActionListener(e -> { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("XML file", "xml"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { myTree.setPath(chooser.getSelectedFile().getAbsolutePath()); } }); pan.add(button); f.add(pan, BorderLayout.SOUTH); f.setVisible(true); }
From source file:com.google.code.facebook.graph.sna.applet.GraphEditorDemo.java
/** * a driver for this demo//w ww. j av a2 s .c o m */ @SuppressWarnings("serial") public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final GraphEditorDemo demo = new GraphEditorDemo(); JMenu menu = new JMenu("File"); menu.add(new AbstractAction("Make Image") { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(demo); if (option == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); demo.writeJPEGImage(file); } } }); menu.add(new AbstractAction("Print") { public void actionPerformed(ActionEvent e) { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(demo); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } }); JPopupMenu.setDefaultLightWeightPopupEnabled(false); JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); frame.setJMenuBar(menuBar); frame.getContentPane().add(demo); frame.pack(); frame.setVisible(true); }