Reputation:
I am currently working on a small project, where a Jframe has a Jlist, which displays an arraylist of files, which have been gathered by scanning through a directory.
The whole process works like this, a seperate class, calls a method in the Jframe class called "main". This "main" method scans through a directory(folderAndFiles) and lists all the file names into a regular text file(file.txt). Aftwerwards the seperate class then displays the Jframe, which then has another scanner, which reads the new text file(file.txt) and turns all the names within that file into an arraylist(filList). Within the initialization function of the Jframe, I am then setting a DefaultListModel to add the arraylist items to the Jlist.
Everything sort of works, but there are some slight hiccups. The first thing is the fact that the Jlist displays the things from the arraylist horizontaly meaning it will be like this [File1.txt, File2.txt, File3.txt, etc...]
and not vertically, where the filenames are displayed underneath each other. Then the second issue, which probably would be fixed if the first issue is fixed, the array list is too long and on the Jlist after listing some of the files it ends up saying "line is to long, please switch to wrapped mode to see whole line..."
I am not completely sure whether I am using the arraylist right and why it won't list the items within the Jlist vertically and properly.
The following is the whole Jframe, where the Jlist is placed and the function is happening.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.DefaultListModel;
public class DisplayTest extends javax.swing.JFrame {
static String username = System.getProperty("user.name");
static File file = new File("file.txt");
static ArrayList<String> filList = new ArrayList<String>();
public DisplayTest(){
initComponents();
DefaultListModel DLM = new DefaultListModel();
DLM.addElement(filList);
jList1.setModel(DLM);
System.out.println(filList);
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DisplayTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JList<String> jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public static void main() throws IOException {
try {
listFilesForFolderMac(folderAndFiles);
} finally {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
filList.add(line);
}
}
public static void listFilesForFolderMac(final File folder) throws IOException {
PrintWriter writToDoc = new PrintWriter(new FileWriter("file.txt",true));
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolderMac(fileEntry);
} else {
writToDoc.println(fileEntry.getName());
}
}
writToDoc.close();
}
final static File folderAndFiles = new File("/Users/" + username + "/Documents");
}
The following is how I call the Jframe and function, from the seperate class
public static void main(String[] args) throws IOException {
try {
DisplayTest.main();
} finally {
DisplayTest dTest = new DisplayTest();
dTest.setVisible(true);
}
}
Upvotes: 0
Views: 170
Reputation: 324118
I am not completely sure whether I am using the arraylist right
A JList
doesn't know how to display the items in an ArrayList
, so you can't just add the ArrayList
to the ListModel
.
So you need to iterate through the ArrayList
and add each item to the ListModel
.
Or even easier, just get rid of the ArrayList
and read the data from the file directly into the ListModel
.
Your code is poorly structured. Any time you see an overuse of static variables and methods you know you have a design problem.
I would start by reading the section from the Swing tutorial on How to Use Lists. There are working examples there that will show you a better program structure. You can start with working code and modify it for your requirements.
Upvotes: 3