Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1

    Finestra per salvare file

    mi servirebbe sapere se in netbeans c'è un componente che apre la classica finestra che ti dice dove salvare un file.
    esiste?
    sennò come posso crearlo??

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Finestra per salvare file

    Originariamente inviato da fermat
    mi servirebbe sapere se in netbeans c'è un componente che apre la classica finestra che ti dice dove salvare un file.
    In Swing esiste JFileChooser (tutorial: How to Use File Choosers)
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    si filechooser lo conoscevo ma di default netbeans ha solo l'opzione per aprirli e nn per slavarli....
    ho scaricato l'esempio e aggiunto un filechooser al form.
    vedo se riesco a modificarlo per slavare i file invece che aprirli.

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da fermat
    ma di default netbeans ha solo l'opzione per aprirli e nn per slavarli....
    Ma che vuol dire!!? JFileChooser è di Swing e ha i metodi showOpenDialog e showSaveDialog che comunque devi invocare tu, con del tuo codice.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    niente scusa nn mi sono spiegato.
    allora, io ho creato da netbeans un JFrame e dentro ci ho messo un FileChooser:
    codice:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * SalvaFile.java
     *
     * Created on 16-lug-2010, 22.57.39
     */
    
    package banca;
    
    /**
     *
     * @author matte
     */
    public class SalvaFile extends javax.swing.JFrame {
    
        /** Creates new form SalvaFile */
        public SalvaFile() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jFileChooser1 = new javax.swing.JFileChooser();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jFileChooser1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jFileChooser1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
                    try {
                javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
    //            com.jtattoo.plaf.noire.NoireLookAndFeel
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SalvaFile().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JFileChooser jFileChooser1;
        // End of variables declaration                   
    
    }
    questo codice è molto diverso da quello dell'esempio che ho visto dove mi hai indicato te.
    qui dentro dovr sovrei implentare il metodo showSaveDialog?

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da fermat
    questo codice è molto diverso da quello dell'esempio che ho visto dove mi hai indicato te.
    qui dentro dovr sovrei implentare il metodo showSaveDialog?
    E come faccio a saperlo se non hai precisato quando e a seguito di cosa vuoi mostrare la dialog del JFileChooser? Hai un pulsante "Salva"? Una voce di un menù "Salva" o "Salva con nome..."? Premendo uno di questi? O nel momento in cui si apre il JFrame? O in un altro momento, a seguito di altro?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    eh a saperlo.
    nel senso che il file chooser creato in automatico da net beans crea due pulsanti che però nn ti fa selezionare per impostargli un evento.
    ed infatti nel codice nn ci sono, anche se poi sono visibili.
    nn so se mi sono spiegato.

    forse dovrei provare a crearlo a mano partendo dalla spiegazione che mi ha linkato.

  8. #8
    cmq diciamo che nel frame principale ho creato un classico menu File->Salva e quando clicco su salva mi si dovrebbe aprire il filechooser per permettermi di salvare:
    codice:
        private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {                                        
            
        }
    sto provando a farlo a mano ma con scarso successo .

  9. #9
    purtroppo nn riesco e in giro trovo sempre esempi come questo:
    codice:
    package banca;
    
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FileChooserDemo extends JPanel implements ActionListener {
    
        static private final String newline = "\n";
        JButton openButton, saveButton;
        JTextArea log;
        JFileChooser fc;
    
        public FileChooserDemo() {
            super(new BorderLayout());
    
            //Create the log first, because the action listeners
            //need to refer to it.
            log = new JTextArea(5, 20);
            log.setMargin(new Insets(5, 5, 5, 5));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);
    
            //Create a file chooser
            fc = new JFileChooser();
    
            //Uncomment one of the following lines to try a different
            //file selection mode.  The first allows just directories
            //to be selected (and, at least in the Java look and feel,
            //shown).  The second allows both files and directories
            //to be selected.  If you leave these lines commented out,
            //then the default mode (FILES_ONLY) will be used.
            //
            //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    
            //Create the open button.  We use the image from the JLF
            //Graphics Repository (but we extracted it from the jar).
            openButton = new JButton("Open a File...", createImageIcon("images/Open16.gif"));
            openButton.addActionListener(this);
    
            //Create the save button.  We use the image from the JLF
            //Graphics Repository (but we extracted it from the jar).
            saveButton = new JButton("Save a File...", createImageIcon("images/Save16.gif"));
            saveButton.addActionListener(this);
    
            //For layout purposes, put the buttons in a separate panel
            JPanel buttonPanel = new JPanel(); //use FlowLayout
            buttonPanel.add(openButton);
            buttonPanel.add(saveButton);
    
            //Add the buttons and the log to this panel.
            add(buttonPanel, BorderLayout.PAGE_START);
            add(logScrollPane, BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e) {
    
            //Handle open button action.
            if (e.getSource() == openButton) {
                int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would open the file.
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
    
                //Handle save button action.
            } else if (e.getSource() == saveButton) {
                int returnVal = fc.showSaveDialog(FileChooserDemo.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would save the file.
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = FileChooserDemo.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("FileChooserDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add content to the window.
            frame.add(new FileChooserDemo());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    //Turn off metal's use of bold fonts
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
    }
    che hanno però sempre prima un pannello con due bottoni (open e save) con una text area per i log.
    nn riesco a ridurli a un semplice jfilechooser per salvare.
    potreste darmi una mano?'

  10. #10
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Originariamente inviato da fermat
    nn riesco a ridurli a un semplice jfilechooser per salvare.
    potreste darmi una mano?'
    Per finestra "salva":
    codice:
    int returnVal = fc.showSaveDialog(FileChooserDemo.this);
    Per finestra "apri"
    codice:
    int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    Non capisco dove sia il problema :master:
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.