[英]How to display a File[] array in a JList
I'm trying to display a list of files that I get of a folder in a JList, this is my code but when I run the project and select the desired folder I obtain the names of the files in the Output console but I can't show that File[] array in the JList. 我正在尝试显示我在JList中的文件夹中获得的文件列表,这是我的代码,但是当我运行项目并选择所需的文件夹时,我在输出控制台中获得了文件名,但是我可以t在JList中显示File []数组。
private void jButtonOpenActionPerformed(java.awt.event.ActionEvent evt) {
// Gets the path of the folder selected
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setCurrentDirectory(new File("C:\\...\\ProjectColorCT"));
int show_fileC = fileChooser.showOpenDialog(this);
String pathFolder = null;
if (show_fileC != JFileChooser.CANCEL_OPTION ){
File folderName = fileChooser.getSelectedFile();
pathFolder = folderName.getAbsolutePath();
jTextPathFolder.setText(pathFolder);
}
System.out.println(pathFolder);
// Gets all the names of the files inside the folder selected previously
File folderCortes = new File(pathFolder);
File[] archivos = folderCortes.listFiles();
for (File fichero : archivos) {
System.out.println(fichero.getName());
}
// Create the model for the JList
DefaultListModel model = new DefaultListModel();
// Add all the elements of the array "archivos" in the model
for (int i=0 ; i<archivos.length ; i++){
model.addElement(archivos[i].getName());
}
// Add the JList to the JScrollPane
jCortesList = new JList(model);
jScrollCortes = new JScrollPane(jCortesList);
// Print for testing
for (int i=0 ; i<archivos.length ; i++){
jCortesList.setSelectedIndex(i);
System.out.println(jCortesList.getSelectedValue());
}
}
I'm adding a DefaultListModel();
我要添加一个
DefaultListModel();
and after I assign that model to the JList
, finally I assign that JList
to the JScrollPane
, but it doesn't show me the list in the interface. 在将该模型分配给
JList
,最后我将该JList
分配给JScrollPane
,但是它没有在接口中向我显示列表。
Based on your available, out-of-context code, the obvious answer would be, you've not actually added jScrollCortes
to the UI... 根据您可用的上下文外代码,显而易见的答案是,您实际上并未将
jScrollCortes
添加到UI中...
private void jButtonOpenActionPerformed(java.awt.event.ActionEvent evt) {
//...
// Add the JList to the JScrollPane
jCortesList = new JList(model);
jScrollCortes = new JScrollPane(jCortesList);
add(jScrollCortes);
revalidate();
repaint();
//...
}
This may or may not work depending on how the classes, UI and/or layout is setup. 根据类,UI和/或布局的设置方式,这可能有效也可能无效。
A better solution would be to have the JList
and JScrollPane
already created an displayed on the UI, then all you'd need to do is apply the new ListModel
to the already existing JList
更好的解决方案是让
JList
和JScrollPane
已经在UI上创建了一个显示,然后您要做的就是将新的ListModel
应用于已经存在的JList
When I add the JList from the Swing Controls Palette the JScrollPane creates by default and I changed it the name to jScrollCortes
当我从Swing控件面板添加JList时,JScrollPane默认创建,并将其名称更改为jScrollCortes
Since you already have an instance of JList
(wrapped in a JScrollPane
), then you should simply only need to change the model... 由于您已经有一个
JList
实例(包装在JScrollPane
),因此您只需要更改模型即可。
DefaultListModel model = new DefaultListModel();
//...
// Add the JList to the JScrollPane
jCortesList.setModel(model);
//jCortesList = new JList(model);
//jScrollCortes = new JScrollPane(jCortesList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.