如何以JList
水平方式加载?这是我的代码,我正在尝试显示JList
与提供的屏幕截图类似的内容。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class Test extends JFrame{
private JList toolsList;
private ArrayList<File> toolXmlList;
public Test()
{
toolXmlList = new ArrayList<File>();
toolXmlList = loadFiles();
setVisible(true);
setSize(300,300);
setTitle("Test Jlist");
createComponents();
}
public void createComponents()
{
toolsList = new JList();
toolsList.setModel(displayDefaltTools());
toolsList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
setLayout(new BorderLayout());
add(toolsList,BorderLayout.CENTER);
}
/**
* Creates a list model and add the tools to it
*
* @return DefaultListModel
*/
public DefaultListModel displayDefaltTools() {
DefaultListModel dlistModel = new DefaultListModel();
String presentation = "";
for (int i = 0; i < toolXmlList.size(); i++) {
//System.out.println(idSet.get(i));
presentation = presentation + toolXmlList.get(i).getName() ;
dlistModel.addElement(presentation);
presentation = "";
}
return dlistModel;
}
public ArrayList loadFiles()
{
ArrayList<File> xmlFiles = new ArrayList<File>();
File f = new File(".");
File [] folList = f.listFiles();
for(int i=0;i<folList.length;i++)
{
if(folList[i].getName().startsWith("Tool_Frag"))
{
File[] fileList=folList[i].listFiles();
for(int j=0;j<fileList.length;j++)
{
System.out.println(fileList[j].getName());
xmlFiles.add(fileList[j]);
}
}
}
return xmlFiles;
}
public static void main(String[] args)
{
new Test();
}
}
我正在尝试以这种方式获取 jlist,项目并排显示