[英]How to load a JList in horizontal manner?
How to load a JList
in horizontal fashion?? 如何以横向方式加载
JList
? Here is my code,I am trying to display the JList
similar to the screen shot provided. 这是我的代码,我试图显示类似于提供的屏幕截图的
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();
}
}
I am trying to get a jlist in this manner,items displayed one next to another 我正在尝试以这种方式获取jlist,项目彼此相邻显示
You will have to do two things: 您将必须做两件事:
Set the LayoutOrientation
to JList.HORIZONTAL_WRAP
or JList.VERTICAL_WRAP
as per the documentation . 设置
LayoutOrientation
到JList.HORIZONTAL_WRAP
或JList.VERTICAL_WRAP
为每个文件 。
Make the list wide enough that it can display more than one element per row. 使列表足够宽,使其每行可以显示多个元素。 Use
setVisibleRowCount()
for this. 为此使用
setVisibleRowCount()
。
Calling setPreferredSize()
also works but can cause trouble when you use layout managers. 调用
setPreferredSize()
也可以,但是在使用布局管理器时可能会引起麻烦。
Alternatively, consider using a JTable
if you must make sure a certain number of rows/columns (like all elements in a single line). 另外,如果必须确保一定数量的行/列(如单行中的所有元素),请考虑使用
JTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.