我创建了一个JList
,其中包含目录中的文件列表。
这是JList
。
JList MList;
String ListData[]
// Create a new listbox control
List = new JList(ListData);
我还创建了一个方法来读取目录中的文本文件列表:
public String ReadDirectory() {
String path = "C://Documents and Settings/myfileTxt";
String files = null;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT")) {
System.out.println(files);
}
}
}
return files;
}
问题是我想要JList
中此方法的结果(文本文件列表)。
如何将File
对象放入JList
?
答案 0 :(得分:9)
不要将字符串放入JList
,使用File
个对象并设置渲染器。由于默认渲染器返回的组件是JLabel
,因此可以轻松设置图标。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.Border;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileFilter;
/**
This code uses a JList in two forms (layout orientation vertical & horizontal wrap) to
display a File[]. The renderer displays the file icon obtained from FileSystemView.
*/
class FileList {
public Component getGui(File[] all, boolean vertical) {
// put File objects in the list..
JList fileList = new JList(all);
// ..then use a renderer
fileList.setCellRenderer(new FileRenderer(!vertical));
if (!vertical) {
fileList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
fileList.setVisibleRowCount(-1);
} else {
fileList.setVisibleRowCount(9);
}
return new JScrollPane(fileList);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
File f = new File(System.getProperty("user.home"));
FileList fl = new FileList();
Component c1 = fl.getGui(f.listFiles(new TextFileFilter()),true);
//f = new File(System.getProperty("user.home"));
Component c2 = fl.getGui(f.listFiles(new TextFileFilter()),false);
JFrame frame = new JFrame("File List");
JPanel gui = new JPanel(new BorderLayout());
gui.add(c1,BorderLayout.WEST);
gui.add(c2,BorderLayout.CENTER);
c2.setPreferredSize(new Dimension(375,100));
gui.setBorder(new EmptyBorder(3,3,3,3));
frame.setContentPane(gui);
frame.pack();
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class TextFileFilter implements FileFilter {
public boolean accept(File file) {
// implement the logic to select files here..
String name = file.getName().toLowerCase();
//return name.endsWith(".java") || name.endsWith(".class");
return name.length()<20;
}
}
class FileRenderer extends DefaultListCellRenderer {
private boolean pad;
private Border padBorder = new EmptyBorder(3,3,3,3);
FileRenderer(boolean pad) {
this.pad = pad;
}
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(
list,value,index,isSelected,cellHasFocus);
JLabel l = (JLabel)c;
File f = (File)value;
l.setText(f.getName());
l.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
if (pad) {
l.setBorder(padBorder);
}
return l;
}
}
答案 1 :(得分:3)
@Andrew Thompson
晚上03:36在我的TimeZone和06:00我必须醒来,但在某个地方,我失去了(不是我)JLabel,天哪,它可能是
import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class FilesInTheJList {
public FilesInTheJList() {
JList displayList = new JList(new File("C:\\").listFiles());
displayList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
displayList.setCellRenderer(new MyCellRenderer());
displayList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
displayList.setName("displayList");
JFrame f = new JFrame("Files In the JList");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(500, 300));
displayList.setVisibleRowCount(-1);
f.add(new JScrollPane(displayList));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FilesInTheJList fITJL = new FilesInTheJList();
}
});
}
private static class MyCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof File) {
File file = (File) value;
setText(file.getName());
setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
}
}
import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class FilesInTheJList {
private static final int COLUMNS = 4;
private Dimension size;
public FilesInTheJList() {
final JList list = new JList(new File("C:\\").listFiles()) {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredScrollableViewportSize() {
if (size != null) {
return new Dimension(size);
}
return super.getPreferredScrollableViewportSize();
}
};
list.setFixedCellHeight(50);
list.setFixedCellWidth(150);
size = list.getPreferredScrollableViewportSize();
size.width *= COLUMNS;
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(new MyCellRenderer());
list.setVisibleRowCount(0);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JFrame f = new JFrame("Files In the JList");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(list));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FilesInTheJList fITJL = new FilesInTheJList();
}
});
}
private static class MyCellRenderer extends JLabel implements ListCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof File) {
File file = (File) value;
setText(file.getName());
setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setPreferredSize(new Dimension(250, 25));
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
}
}
答案 2 :(得分:2)
我不太明白你在做什么 - 看起来你想要将JList的内容过滤为只有.txt
扩展名的文件。在这种情况下,您可能希望实现FilenameFilter
,这将导致list()
返回String[]
中仅与您指定的文件名匹配的文件名列表。
例如,
public String[] ReadDirectory() {
String path = "C://Documents and Settings/myfileTxt";
String files = null;
File folder = new File(path);
FilenameFilter txtFilter = new TextFileFilter();
String[] listOfFiles = folder.listFiles(txtFilter);
return listOfFiles;
}
public class TextFileFilter extends FilenameFilter
{
public boolean accept(File dir, String name)
{
if(name != null && name.endsWith(".txt"))
{
return true;
}
return false;
}
}
答案 3 :(得分:1)
创建字符串List<String> fnames = new ArrayList<String>();
列表,用文件名(fnames.add(files);
)填充,然后使用list.setListData(fnames.toArray());
将其设置为JList。我想这应该有用。
答案 4 :(得分:1)
只需像这样编辑你的代码,
public ArrayList<String> ReadDirectory() {
String path = "C://Documents and Settings/myfileTxt";
ArrayList<String> files = new ArrayList<String>();
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files.add(listOfFiles[i].getName());
if (files.endsWith(".txt") || files.endsWith(".TXT")) {
System.out.println(files);
}
}
}
return files;
}
将返回ArrayList对象,然后您可以将该ArrayList对象转换为普通数组。
像这样的东西,让我们说你的变量名是,
ArrayList<String> listData = new ArrayList<String>();
只需将其转换为普通数组,
listData.toArray();