Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Dumb Netbeans Q

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default Dumb Netbeans Q

    I've made a program that basicall serves as a music organizer. Import iTunes playlist. Remove duplicates. Edit stuff. Create new playlists. Previously it just used System.out.println to display the playlists. Now I found the joy (curse) of Netbeans and made a pretty GUI for the whole thing.

    There is a class for reading text playlists, adding and showing music etc. Another one for extra sundry methods and then the new class for the JFrame Gui.

    I want to put a button in the JFrame to open a JFileChooser window to select a file to import.

    A made a new class for the FileChooser... But I have no idea how to make it go when I click the button...

    I've tried some things already but I can't get it to work.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    After adding the JFileChooser component add an action event to it. Get the action text and do the process.

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    here is some hints

    1. Add a ActionListener
    2. Get the return value of the JFilechooser (it can be OK or the Cancel button)
    Sample in getting the return value:
    Java Code:
    int RT = fileChooser.showOpenDialog(this);
    3. Compare the return value to your desired value
    Another Sample:
    Java Code:
     if (RT == JFileChooser.APPROVE_OPTION){
    //do your thing here for Yes or Ok button
    } else if (RT == JFileChooser.CANCEL_OPTION){
    //here is for Cancel or No button
    }
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  4. #4
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Thanks, I will try those ideas.

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I got it to show the FileChooser window, and return a file. The only problem is that it doesn't return the full directory path (I'm using Windows).

    I made a class based on a JInternalFrame called Browse, and the JFileChooser in this class I called browser.

    Here is my code:

    Browse br = new Browse();
    br.browser.showOpenDialog(null);
    File selection = br.browser.getSelectedFile());

    What method can I use to return the entire path?

    Thanks again for your help.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    getSelectedFile() gives the full path.

    What's your browser class do? I can't see nay file chooser instance here in your code segment.

  7. #7
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    That's just the bit of code that executes when the specific button is clicked.

    The Browse class is a JInternalFrame with a JFileChooser called browser in it. That's it.

    So I created a new object of the Browse class:
    Browse br = new Browse()
    Then I used the showOpenDialog method to show the JFileChooser called browser.
    Then I used the getSelectedFile method to return the selection made in browser.

    It all works perfectly, but it returns only the filename.
    i.e. Instead of returning D:\lists\songs.txt it just returns songs.txt

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    That only returns the immediate directory that the selected file is in.

    i.e. If the selected file was: C:\files\lists\songs.txt
    then getCurrentDirectory returns lists, and getSelectedFile returns songs.txt

    Surely that isn't how it's suppose to be, because this makes it quite useless...

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Sorry it's so much.
    I wasn't sure what you wanted exactly...

    Thanks again for your help.

    Java Code:
    package MusicMan;
    
    public class Start extends javax.swing.JFrame
    {
        Action ac = new Action(); //the Action class has all of the important methods
    
        public Start()
        {
            initComponents();
        }//dc
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
            bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
    
            btnImport = new javax.swing.JButton();
            btnPlaylist = new javax.swing.JButton();
            labelOutput = new javax.swing.JLabel();
            jScrollPane2 = new javax.swing.JScrollPane();
            list = new javax.swing.JList(ac.displayLists());
            labelPlaylists = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            output = new javax.swing.JList();
            btnEdit = new javax.swing.JButton();
            browse = new javax.swing.JButton();
            btnManual = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBounds(new java.awt.Rectangle(10, 10, 10, 10));
            setMaximizedBounds(new java.awt.Rectangle(0, 1, 0, 1));
    
            org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btnImport, org.jdesktop.beansbinding.ELProperty.create("MusicMan"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
            bindingGroup.addBinding(binding);
    
            btnImport.setText("Import Songs");
            btnImport.setToolTipText("Import songs from a text file");
            btnImport.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnImportActionPerformed(evt);
                }
            });
    
            btnPlaylist.setText("Add Playlist");
            btnPlaylist.setToolTipText("Select songs first");
            btnPlaylist.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnPlaylistActionPerformed(evt);
                }
            });
    
            labelOutput.setFont(new java.awt.Font("Courier New", 0, 12));
            labelOutput.setLabelFor(output);
            labelOutput.setText("Name                                  |  Artist                                |  Album                                 |  Date Added ");
            labelOutput.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    
            list.setFont(new java.awt.Font("Courier New", 0, 12));
            list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            list.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    listMouseClicked(evt);
                }
            });
            jScrollPane2.setViewportView(list);
    
            labelPlaylists.setLabelFor(list);
            labelPlaylists.setText("Playlists");
            labelPlaylists.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    
            output.setFont(new java.awt.Font("Courier New", 0, 12));
            jScrollPane1.setViewportView(output);
    
            btnEdit.setText("Edit Song Info");
            btnEdit.setToolTipText("Select songs first");
            btnEdit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEditActionPerformed(evt);
                }
            });
    
            browse.setText("Import iTunes Playlist");
            browse.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    browseActionPerformed(evt);
                }
            });
    
            btnManual.setText("Manual Song Add");
            btnManual.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnManualActionPerformed(evt);
                }
            });
    
            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)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(labelPlaylists, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addComponent(labelOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 1044, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1052, Short.MAX_VALUE)))
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(browse)
                            .addGap(18, 18, 18)
                            .addComponent(btnManual)
                            .addGap(18, 18, 18)
                            .addComponent(btnPlaylist)
                            .addGap(18, 18, 18)
                            .addComponent(btnEdit)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 421, Short.MAX_VALUE)
                            .addComponent(btnImport)
                            .addGap(173, 173, 173))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(browse)
                        .addComponent(btnPlaylist)
                        .addComponent(btnEdit)
                        .addComponent(btnManual)
                        .addComponent(btnImport))
                    .addGap(37, 37, 37)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelPlaylists)
                        .addComponent(labelOutput))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(390, 390, 390))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
                            .addContainerGap())))
            );
    
            bindingGroup.bind();
    
            pack();
        }// </editor-fold>//GEN-END:initComponents
    
    private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImportActionPerformed
        ac.addMusicFromText("songs.txt");
        output.setListData(ac.showMusic(0));
    }//GEN-LAST:event_btnImportActionPerformed
    
    private void btnPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlaylistActionPerformed
        output.setListData(ac.showMusic(ac.addPlaylist(output.getSelectedIndices())));
        list.setListData(ac.displayLists());
    }//GEN-LAST:event_btnPlaylistActionPerformed
    
    private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
        output.setListData(ac.showMusic(list.getSelectedIndex()));
    }//GEN-LAST:event_listMouseClicked
    
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        ac.edit(output.getSelectedIndex());
        output.setListData(ac.showMusic(0));
    }//GEN-LAST:event_btnEditActionPerformed
    
    [B]private void browseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseActionPerformed
        Browse br = new Browse();
        br.browser.showOpenDialog(null);
        ac.addMusicFromText(br.browser.getName(br.browser.getSelectedFile()));
        output.setListData(ac.showMusic(0));
    }//GEN-LAST:event_browseActionPerformed[/B]
    
    private void btnManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnManualActionPerformed
        SongEntry se = new SongEntry(new Start(), true);
        se.setVisible(true);
        ac.addMusic(se.nameEntry.getText(), se.artistEntry.getText(), se.albumEntry.getText());
        output.setListData(ac.showMusic(0));
    }//GEN-LAST:event_btnManualActionPerformed
    
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Start().setVisible(true);
                }
            });
        }//main
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton browse;
        private javax.swing.JButton btnEdit;
        private javax.swing.JButton btnImport;
        private javax.swing.JButton btnManual;
        private javax.swing.JButton btnPlaylist;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JLabel labelOutput;
        private javax.swing.JLabel labelPlaylists;
        private javax.swing.JList list;
        javax.swing.JList output;
        private org.jdesktop.beansbinding.BindingGroup bindingGroup;
        // End of variables declaration//GEN-END:variables
    
    }
    Java Code:
    package MusicMan;
    
    public class Browse extends javax.swing.JInternalFrame {
    
        public Browse() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
    
            browser = new javax.swing.JFileChooser();
    
            browser.setFileHidingEnabled(false);
            browser.setDragEnabled(true);
    
            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()
                    .addComponent(browser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(browser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(19, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>//GEN-END:initComponents
    
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        public javax.swing.JFileChooser browser;
        // End of variables declaration//GEN-END:variables
    
    }

  12. #12
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    The important stuff is in BOLD.

  13. #13
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    I used this command to get the file with the Current directory


    Java Code:
    filename=fileChooser.getSelectedFile();
    I hope that helps.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes it should works. But our friend says it not work. Seems something wrong with Browser implementation or else.

  15. #15
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Hmm...
    Quite strange then...

    I think I'll make a new test prog, just with the essential JFileChooser and see if it works...

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  17. #17
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    hmmm. . . I cant seem to undestant the code. im sorry. Im a little sick. The return value of the .getSelectedFile() is in file data type. If you want to use it as string you should convert it toString first. Sorry if i cant give much help.

    Java Code:
    else if(command.equalsIgnoreCase("Save As")){
                result = fileChooser.showSaveDialog(this);
                if (result == JFileChooser.CANCEL_OPTION) {
                    return;}
                filename=fileChooser.getSelectedFile();
                ext = fileChooser.getFileFilter();
                Save();
    }
    Here is a sample. i got the filename with the directory using getSelectedFile();
    and got the extention using the getfilterfile. *Note: You must declare a filter if you want to filter the extensions. Then call my Function.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  18. #18
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I used the getName method to convert it to a string...

    What is getFileFilter for? It already returns the file extension (.txt).

    I'm on my phone now, but later at my PC I'll try your code out. (and try a new one, so I can see where the problem might have been in my case...)

  19. #19
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I just had a look at the Java API.

    This is what it says about file filters:
    The file filter is used by the file chooser to filter out files from the user's view.
    I don't think that's what I need...

    Will still try your reccommendation later on.

  20. #20
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I realised what the problem was. The getName() method only used the filename, not the entire path. So I changed it to just use getSelected() and keep it as a File type.

Page 1 of 2 12 LastLast

Similar Threads

  1. i am a dumb dumb
    By goose_01ca in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 05:44 AM
  2. NetBeans 5.5
    By Ry4n in forum NetBeans
    Replies: 3
    Last Post: 01-26-2008, 07:09 AM
  3. NetBeans 5.5.1 Help.
    By padutch2 in forum New To Java
    Replies: 6
    Last Post: 12-03-2007, 04:01 AM
  4. netbeans 64 bit
    By caspermel in forum NetBeans
    Replies: 1
    Last Post: 06-26-2007, 11:29 PM
  5. About using netbeans
    By yuchuang in forum NetBeans
    Replies: 3
    Last Post: 05-27-2007, 09:06 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
  •