I have 2 classes one of which is a GUI.
My first class is called MusicSearch.java and it has this:
public static void directoryMusicLocator(File dir) {
try
{
String[] filetype = new String[] { "mp3" }; // only search mp3 files
System.out.println("Getting all .mp3 files in " + dir.getCanonicalPath() + " including those in subdirectories");
List<File> files = (List<File>) FileUtils.listFiles(dir, filetype, true);
for (File file : files)
{
System.out.println(file.getAbsolutePath()); // get the file's absolute path
}
System.out.println("\nFinished Searching.");
}
catch (IOException e)
{
e.printStackTrace();
}
}
What this does is that it searches for Mp3 files on a directory for example C:\\Music
For the GUI, well I created it using netbeans' JFrame Designer and a screenshot of this can be seen on the image below. (I can't embed images at the moment so I can only provide a link to the image.)
http://i.stack.imgur.com/7pLPL.jpg
On the JTextField next to the "Enter Location", the user enters a location for example C:\\Music. When the user clicks the button "Find MP3's" the method directoryMusicLocator is called. Below is the Action Listener for the Find MP3's button:
private void findMP3ButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String fLocation = dirToSearch.getText(); // this gets the input from the textfield
File dir = new File(fLocation); // converts the location to path
MusicSearch.directoryMusicLocator(dir);
}
An example of the output can be seen below when it is ran and user entered C:\\Music on the textfield:
C:\Music\Feint - Times Like These (Fracture Design Remix).mp3
C:\Music\Ficci - Climax (FREE).mp3
C:\Music\Ficci - Making Me Blue (FREE).mp3
Now what I want is the output to display on the JTextArea but I don't know how. Can someone tell me how.
Thanks
You just have to do the following changes in your directoryMusicLocator
and findMP3ButtonActionPerformed
methods. Instead of directly printing you just need to store the content in a StringBuilder
and return that so that you can show it in the JTextArea
.
public static String directoryMusicLocator(File dir) {
try
{
String[] filetype = new String[] { "mp3" }; // only search mp3 files
StringBuilder outString = new StringBuilder("Getting all .mp3 files in " + dir.getCanonicalPath() + " including those in subdirectories\n");
List<File> files = (List<File>) FileUtils.listFiles(dir, filetype, true);
for (File file : files)
{
outString.append(file.getAbsolutePath()+"\n"); // get the file's absolute path
}
outString.append("\nFinished Searching.");
return outString.toString()
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
private void findMP3ButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String fLocation = dirToSearch.getText(); // this gets the input from the textfield
File dir = new File(fLocation); // converts the location to path
String output = MusicSearch.directoryMusicLocator(dir);
// Replace <jTextArea> with your JTextArea field name
<jTextArea>.setText(output);
}
JTextArea textArea = new JTextArea(); // instantiate the JTextArea
textArea.append("text"); // append to the existing text on JTextArea
textArea.setText("text"); // set the current text with the given one
hope this helps :)
Rather than calling System.out.println
, which sends the output to the standard output steam, you should send it to the text area. But you can't do that without a reference to the JTextArea
that you're working with, so you need to change the signature of directoryMusicLocator
to include a JTextArea
like so:
public static void directoryMusicLocator(File dir, JTextArea outputTextArea) {
Then, you'll want to change System.out.println(newText)
to outputTextArea.append(newText + System.getProperty("line.separator"))
. (Replace "newText" with the parameters that you're sending to the JTextArea.
Finally, change the call-sites for directoryMusicLocator(File)
- pass a reference to the JTextArea
: MusicSearch.directoryMusicLocator(dir, jTextArea1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.