Results 1 to 17 of 17
  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default JFilePicker class in java swing with generated code

    Hi

    can someone help me putting this component into my swing form
    This is the JFilePicker class
    Java Code:
    public class JFilePicker extends JPanel {
    	private final String textFieldLabel;
    	private final String buttonLabel;
    	private final JLabel label;
    	private final JTextField textField;
    	private final JButton button;
    	private final JFileChooser fileChooser;
    	
    	private int mode;
    	public static final int MODE_OPEN = 1;
    	public static final int MODE_SAVE = 2;
    	
    	public JFilePicker(String textFieldLabel, String buttonLabel) {
    		this.textFieldLabel = textFieldLabel;
    		this.buttonLabel = buttonLabel;
    		fileChooser = new JFileChooser();
    		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    		label = new JLabel(textFieldLabel);
    		textField = new JTextField(30);
    		button = new JButton(buttonLabel);
    		button.addActionListener((ActionEvent evt) -> {
                        buttonActionPerformed(evt);
                    });
    		add(label);
    		add(textField);
    		add(button);
    	}
    	
    	private void buttonActionPerformed(ActionEvent evt) {
    		if (mode == MODE_OPEN) {
    			if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
    				textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
    			}
    		} else if (mode == MODE_SAVE) {
    			if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
    				textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
    			}
    		}
    	}
    
    	public void addFileTypeFilter(String extension, String description) {
    		FileTypeFilter filter = new FileTypeFilter(extension, description);
    		fileChooser.addChoosableFileFilter(filter);
    	}
    	
    	public void setMode(int mode) {
    		this.mode = mode;
    	}
    	
    	public String getSelectedFilePath() {
    		return textField.getText();
    	}
    	
    	public JFileChooser getFileChooser() {
    		return this.fileChooser;
    	}
    }
    By the example iv'e seen the gui form is created by using GridBagLayout() and GridBagConstraints
    ex:
    Java Code:
    private JFilePicker filePicker = new JFilePicker("Attached", "Attach File...");
    constraints.gridx = 0;
    		constraints.gridy = 2;
    		constraints.gridheight = 1;
    		constraints.gridwidth = 3;
    		filePicker.setMode(JFilePicker.MODE_OPEN);
    		add(filePicker, constraints);
    Mine is created adding swing components to the Jframe so since i don't have this layout how can i put this in my form??
    Iv'e tried this but the button don't do anything (i have no button)

    Java Code:
    public class EmailSenderForm extends javax.swing.JFrame {
    
        private final ConfigUtility configUtil = new ConfigUtility();
        private final JFilePicker filePicker = new JFilePicker("Anexado", "Anexar");
    
        public EmailSenderForm() throws IOException {
            initComponents();
            setLocationRelativeTo(null);
            String imagePath = "/img/email_32_32.png";
            InputStream imgStream = Entrada.class.getResourceAsStream(imagePath);
            BufferedImage myImg = ImageIO.read(imgStream);
            setIconImage(myImg);
        }
    
        private boolean validarCampos() {
            if (fieldTo.getText().equals("")) {
                JOptionPane.showMessageDialog(this,
                        "Introduza o destinatário!",
                        "Error", JOptionPane.ERROR_MESSAGE);
                fieldTo.requestFocus();
                return false;
            }
    
            if (fieldSubject.getText().equals("")) {
                JOptionPane.showMessageDialog(this,
                        "Introduza o assunto deste email!",
                        "Error", JOptionPane.ERROR_MESSAGE);
                fieldSubject.requestFocus();
                return false;
            }
    
            if (textAreaMessage.getText().equals("")) {
                JOptionPane.showMessageDialog(this,
                        "Introduza a sua mensagem!",
                        "Error", JOptionPane.ERROR_MESSAGE);
                textAreaMessage.requestFocus();
                return false;
            }
    
            return true;
        }
    
        /**
         * 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() {
    
            labelPara = new javax.swing.JLabel();
            labelAssunto = new javax.swing.JLabel();
            labelAnexo = new javax.swing.JLabel();
            fieldTo = new javax.swing.JTextField();
            fieldSubject = new javax.swing.JTextField();
            fieldAnexo = new javax.swing.JTextField();
            sendEmail = new javax.swing.JButton();
            pesquisarBt = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            textAreaMessage = new javax.swing.JTextArea();
            jMenuBar1 = new javax.swing.JMenuBar();
            menuPrincipal = new javax.swing.JMenu();
            menuItemTools = new javax.swing.JMenuItem();
            menuItemExit = new javax.swing.JMenuItem();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Envio de Email");
            setPreferredSize(new java.awt.Dimension(500, 350));
    
            labelPara.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
            labelPara.setText("Para:");
    
            labelAssunto.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
            labelAssunto.setText("Assunto:");
    
            labelAnexo.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
            labelAnexo.setText("Anexo:");
    
            sendEmail.setIcon(new javax.swing.ImageIcon("C:\\Users\\gilca\\Documents\\NetBeansProjects\\gmc.esmaior\\src\\main\\resources\\img\\email_32_32.png")); // NOI18N
            sendEmail.setToolTipText("Enviar");
            sendEmail.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    sendEmailActionPerformed(evt);
                }
            });
    
            pesquisarBt.setIcon(new javax.swing.ImageIcon("C:\\Users\\gilca\\Documents\\NetBeansProjects\\gmc.esmaior\\src\\main\\resources\\img\\attach_32_32.png")); // NOI18N
            pesquisarBt.setToolTipText("Anexo");
            pesquisarBt.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    pesquisarBtActionPerformed(evt);
                }
            });
    
            textAreaMessage.setColumns(20);
            textAreaMessage.setRows(5);
            jScrollPane1.setViewportView(textAreaMessage);
    
            menuPrincipal.setText("Ferramentas");
    
            menuItemTools.setText("Definições Conta");
            menuPrincipal.add(menuItemTools);
    
            menuItemExit.setText("Sair");
            menuPrincipal.add(menuItemExit);
    
            jMenuBar1.add(menuPrincipal);
    
            setJMenuBar(jMenuBar1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(labelPara)
                                .addComponent(labelAssunto)
                                .addComponent(labelAnexo))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(fieldTo, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(fieldAnexo)
                                        .addComponent(fieldSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sendEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(pesquisarBt, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(labelPara)
                                .addComponent(fieldTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(labelAssunto)
                                .addComponent(fieldSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(sendEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(labelAnexo)
                                .addComponent(fieldAnexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(9, 9, 9)
                            .addComponent(pesquisarBt)))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void sendEmailActionPerformed(java.awt.event.ActionEvent evt) {                                          
            File[] attachFiles = null;
            if (!validarCampos()) {
                return;
            }
            String toAddress = fieldTo.getText();
            String subject = fieldSubject.getText();
            String message = textAreaMessage.getText();
            if (!filePicker.getSelectedFilePath().equals("")) {
                File selectedFile = new File(filePicker.getSelectedFilePath());
                attachFiles = new File[]{selectedFile};
            }
            try {
                Properties smtpProperties = configUtil.loadProperties();
                EmailUtility.sendEmail(smtpProperties, toAddress, subject, message, attachFiles);
                JOptionPane.showMessageDialog(this, "O e-mail foi enviado com suesso!");
    
            } catch (IOException | MessagingException | HeadlessException ex) {
                JOptionPane.showMessageDialog(this, "Erro no envio deste email, tente mais tarde: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }                                         
                                         
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(EmailSenderForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(EmailSenderForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(EmailSenderForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(EmailSenderForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(() -> {
                try {
                    new EmailSenderForm().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(EmailSenderForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            });
        }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    how can i put this in my form??
    Are you asking how to add a component to a container in Swing?

    One problem I see is that much of the GUI looks like it has been built by am IDE's gui builder. That could mean that you need to use the IDE's tools to change the GUI.
    Changing a machine generated layout by hand is very difficult. It could be easier if you replaced lines 112 through 165 with your own code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Yes, i would like to add this to my frame.
    Most of the code as been inserted automatically by netbeans ide.
    But those lines can't be edited...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFilePicker class in java swing with generated code

    Quote Originally Posted by alphasil View Post
    But those lines can't be edited...
    Which is exactly why you should abandon the Netbeans GUI Builder and use layout managers to create your GUI. But then, why not just add a button to your GUI which invokes the JFileChooser? Seems like that would be much easier to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    Most of the code as been inserted automatically by netbeans ide.
    If the IDE controls the GUI, you need to study how to use the IDE so you can use it to change the GUI.

    You can't make changes to the GUI code without hurting what the IDE has done and preventing the future use of the IDE to make changes.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Iv'e tried to do that (Method with JFileChooser) but then i need to pass the value to this method.

    Java Code:
    private void sendEmailActionPerformed(java.awt.event.ActionEvent evt) {                                          
            if (!validarCampos()) {
                return;
            }
            String toAddress = fieldTo.getText();
            String subject = fieldSubject.getText();
            String message = textAreaMessage.getText();
                   try {
                Properties smtpProperties = configUtil.loadProperties();
                EmailUtility.sendEmail(smtpProperties, toAddress, subject, message, attachFiles);
                JOptionPane.showMessageDialog(this, "O e-mail foi enviado com suesso!");
    
            } catch (IOException | MessagingException | HeadlessException ex) {
                JOptionPane.showMessageDialog(this, "Erro no envio deste email, tente mais tarde: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    And the JFileChooser like this
    Java Code:
    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();
            } 
            }
       }
    }
    So how can i pass the file tothe first method?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    pass the file tothe first method?
    Not sure what you mean by "pass". I think that to pass means as an arg to a method: firstMethod(theFile)

    One way to make a value set by one method available to another method is to use a class instance variable to hold the value.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Ok I have create a new class

    Java Code:
    public class Anexos {
        
        File[] file;
    
        public File[] getFile() {
            return file;
        }
    
        public void setFile(File[] file) {
            this.file = file;
        }
       
    }
    And the JFileChooser actionPerformed
    Java Code:
    private void pesquisarBtActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
                 JFileChooser chooser;
            FileNameExtensionFilter filter;
            chooser = new JFileChooser();
            File[] file;
            filter = new FileNameExtensionFilter("jpeg, gif e png", "jpg", "gif", "png");
            chooser.addChoosableFileFilter(filter);
            int i = chooser.showOpenDialog(this);
            if (i == JFileChooser.APPROVE_OPTION) {
                file = chooser.getSelectedFiles();
                fieldAnexo.setText(chooser.getSelectedFile().getPath());
                anexo.setFile(file);
            }
            
        }
    And the final Button to send

    Java Code:
    private void sendEmailActionPerformed(java.awt.event.ActionEvent evt) {                                          
            File[] attachFiles = null;
            if (!validarCampos()) {
                return;
            }
            String toAddress = fieldTo.getText();
            String subject = fieldSubject.getText();
            String message = textAreaMessage.getText();
    //        if (!filePicker.getSelectedFilePath().equals("")) {
    //            File selectedFile = new File(filePicker.getSelectedFilePath());
    //            attachFiles = new File[]{selectedFile};
    //        }
            attachFiles = anexo.getFile();
            try {
                Properties smtpProperties = configUtil.loadProperties();
                EmailUtility.sendEmail(smtpProperties, toAddress, subject, message, attachFiles);
                JOptionPane.showMessageDialog(this, "O e-mail foi enviado com suesso!");
    
            } catch (IOException | MessagingException | HeadlessException ex) {
                JOptionPane.showMessageDialog(this, "Erro no envio deste email, tente mais tarde: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    The email is sended but without the attachment...

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    without the attachment
    Where is the variable that references the attachment?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Here

    attachFiles = anexo.getFile();

    The problem is in this method

    Java Code:
    private void pesquisarBtActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
            JFileChooser chooser;
            FileNameExtensionFilter filter;
            chooser = new JFileChooser();
            filter = new FileNameExtensionFilter("jpeg, gif e png", "jpg", "gif", "png");
            chooser.addChoosableFileFilter(filter);
            int i = chooser.showOpenDialog(this);
            if (i == JFileChooser.APPROVE_OPTION) {
                File[] files = chooser.getSelectedFiles();
                if(files.length==0){
                    System.out.println("aqui");
                }else{
                fieldAnexo.setText(chooser.getSelectedFile().getPath());
                anexo.setFile(files);  
                }
            }
            
        }
    I have the System.out.println("aqui"); telling me the lenght is 0, so is not getting the file

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    Did you read the API doc for the getSelectedFiles method?
    Last edited by Norm; 12-22-2015 at 02:07 AM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Yes i did....but i don't see what i'm doing wrong...

    I have fieldAnexo.setText(chooser.getSelectedFile().getPa th()); who is returning the right path...and this anexo.setFile(files); doesn't work...

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    nexo.setFile(files); doesn't work...

    i don't see what i'm doing wrong
    Copy and Paste here the API doc for the getSelectedFiles method so I can explain what it means.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code


  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    Can you copy the text of the API doc for the getSelectedFiles method and paste it here so I can explain what it means? Not a link.

    If you read the API doc you will see the problem.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    41
    Rep Power
    0

    Default Re: JFilePicker class in java swing with generated code

    Fixed

    Java Code:
     private void pesquisarBtActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
            JFileChooser chooser;
            FileNameExtensionFilter filter;
            chooser = new JFileChooser();
            filter = new FileNameExtensionFilter("jpeg, gif e png", "jpg", "gif", "png");
            chooser.setMultiSelectionEnabled(true); 
            chooser.addChoosableFileFilter(filter);
            int i = chooser.showOpenDialog(this);
            if (i == JFileChooser.APPROVE_OPTION) {
                final File[] files = chooser.getSelectedFiles();
                    if (files != null && files.length > 0){
                    for (int x = 0; x < files.length; x++)
                    { 
                       fieldAnexo.setText(chooser.getSelectedFile().getPath());
                       anexo.setFile(files); 
                    }
                    } 
            }
        }
    I didn't realise that i need loop.

    Thanks

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: JFilePicker class in java swing with generated code

    What about the statement on line 7 that calls: setMultiSelectionEnabled()?

    I didn't realise that i need loop.
    You do not need a loop.
    What is the loop for?
    Line 16 does not need to be inside of the loop.
    Line 15 does the same thing every time around the loop and does not need to be in the loop

    Remove those 2 lines and the loop is empty and useless.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: 06-02-2012, 05:04 PM
  2. Replies: 2
    Last Post: 11-13-2011, 10:28 PM
  3. Replies: 1
    Last Post: 12-27-2010, 11:09 PM
  4. Showing NetBeans-generated Swing forms
    By _SAS in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 02:45 AM
  5. How to delete pre-generated code?
    By Terentius in forum NetBeans
    Replies: 0
    Last Post: 08-19-2008, 04:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •