Results 1 to 20 of 24
Thread: Dumb Netbeans Q
- 05-22-2008, 09:14 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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.
- 05-23-2008, 05:26 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
After adding the JFileChooser component add an action event to it. Get the action text and do the process.
- 05-23-2008, 10:13 AM #3
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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:
3. Compare the return value to your desired valueJava Code:int RT = fileChooser.showOpenDialog(this);
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.
- 05-23-2008, 03:05 PM #4
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Thanks, I will try those ideas.
- 05-24-2008, 06:31 PM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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.
- 05-25-2008, 06:03 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
getSelectedFile() gives the full path.
What's your browser class do? I can't see nay file chooser instance here in your code segment.
- 05-25-2008, 09:21 AM #7
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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
- 05-25-2008, 11:39 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Try to use getCurrentDirectory() to find the initial directory there.
- 05-25-2008, 01:35 PM #9
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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...
- 05-26-2008, 04:25 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can I look at your code. I'm not sure what's going on there.
- 05-26-2008, 09:42 AM #11
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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 }
- 05-26-2008, 09:44 AM #12
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
The important stuff is in BOLD.
- 05-26-2008, 09:45 AM #13
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
I used this command to get the file with the Current directory
I hope that helps.Java Code:filename=fileChooser.getSelectedFile();
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 05-26-2008, 09:49 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes it should works. But our friend says it not work. Seems something wrong with Browser implementation or else.
- 05-26-2008, 09:54 AM #15
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Hmm...
Quite strange then...
I think I'll make a new test prog, just with the essential JFileChooser and see if it works...
- 05-26-2008, 10:02 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, that's better. Then you can compare two applications and find the difference.
- 05-26-2008, 10:08 AM #17
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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.
Here is a sample. i got the filename with the directory using getSelectedFile();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(); }
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.
- 05-26-2008, 02:58 PM #18
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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...)
- 05-26-2008, 04:15 PM #19
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
I just had a look at the Java API.
This is what it says about file filters:
I don't think that's what I need...The file filter is used by the file chooser to filter out files from the user's view.
Will still try your reccommendation later on.
- 05-26-2008, 09:29 PM #20
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Similar Threads
-
i am a dumb dumb
By goose_01ca in forum New To JavaReplies: 4Last Post: 04-25-2008, 04:44 AM -
NetBeans 5.5
By Ry4n in forum NetBeansReplies: 3Last Post: 01-26-2008, 06:09 AM -
NetBeans 5.5.1 Help.
By padutch2 in forum New To JavaReplies: 6Last Post: 12-03-2007, 03:01 AM -
netbeans 64 bit
By caspermel in forum NetBeansReplies: 1Last Post: 06-26-2007, 10:29 PM -
About using netbeans
By yuchuang in forum NetBeansReplies: 3Last Post: 05-27-2007, 08:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks