|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-22-2008, 10:14 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 06:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
After adding the JFileChooser component add an action event to it. Get the action text and do the process.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-23-2008, 11:13 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 191
|
|
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:
int RT = fileChooser.showOpenDialog(this);
3. Compare the return value to your desired value
Another Sample:
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, 04:05 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
Thanks, I will try those ideas.
|
|

05-24-2008, 07:31 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 07:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
getSelectedFile() gives the full path.
What's your browser class do? I can't see nay file chooser instance here in your code segment.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-25-2008, 10:21 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 12:39 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
Try to use getCurrentDirectory() to find the initial directory there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-25-2008, 02:35 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 05:25 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
Can I look at your code. I'm not sure what's going on there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-26-2008, 10:42 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
Sorry it's so much.
I wasn't sure what you wanted exactly...
Thanks again for your help.
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
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
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
}
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, 10:44 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
The important stuff is in BOLD.
|
|

05-26-2008, 10:45 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 191
|
|
I used this command to get the file with the Current directory
filename=fileChooser.getSelectedFile();
I hope that helps.
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

05-26-2008, 10:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
Yes it should works. But our friend says it not work. Seems something wrong with Browser implementation or else.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-26-2008, 10:54 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 11:02 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
|
|
|
Ya, that's better. Then you can compare two applications and find the difference.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-26-2008, 11:08 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 191
|
|
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.
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.
|
|

05-26-2008, 03:58 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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, 05:15 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
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.
|
|

05-26-2008, 10:29 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 28
|
|
|
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.
| | |