如何以水平方式加载JList? [英] How to load a JList in horizontal manner?
问题描述
如何以水平方式加载JList
?这是我的代码,我正在尝试显示与提供的屏幕截图类似的JList
.
How to load a JList
in horizontal fashion?? Here is my code,I am trying to display the JList
similar to the screen shot provided.
/*
* 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,项目一个接一个地显示
I am trying to get a jlist in this manner,items displayed one next to another
推荐答案
您将必须做两件事:
-
将
LayoutOrientation
设置为JList.HORIZONTAL_WRAP
或JList.VERTICAL_WRAP
作为使列表足够宽,以使每行可以显示多个元素.为此使用setVisibleRowCount()
.Make the list wide enough that it can display more than one element per row. Use
setVisibleRowCount()
for this.调用
setPreferredSize()
也可以,但是在使用布局管理器时可能会引起麻烦.Calling
setPreferredSize()
also works but can cause trouble when you use layout managers.或者,如果必须确保一定数量的行/列(如单行中的所有元素),请考虑使用
JTable
.Alternatively, consider using a
JTable
if you must make sure a certain number of rows/columns (like all elements in a single line).这篇关于如何以水平方式加载JList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!