2014-12-27 73 views
0

我是Java swing新手,我试图创建一个显示目录内容的列表视图。我期望创建如下图像的视图: enter image description here在Java中创建一个漂亮的列表视图

我知道如何使用JList,但我不知道如何显示与文件类型匹配的图标。正如您所看到的,从图片中,我们可以直观地区分pdf文件和文本文件等。我应该尝试使用JList还是其他UI组件?

+2

您正在尝试创建一个树型表,Swing本身没有的东西。考虑使用SwingX的JXTreeTable组件。请看看[这里](http://stackoverflow.com/a/7123302/522444)。 – 2014-12-27 14:28:51

+0

如何为不同类型的文件使用正确的图标 – lenhhoxung 2014-12-27 14:39:05

+1

更多创意[here](http://stackoverflow.com/q/18440232/230513)。 – trashgod 2014-12-27 23:24:19

回答

3

我做了类似的事情;这里是我的输出的一个例子。

enter image description here

我用的树定制呈现;它会在显示屏最左边一列的一个单元格中生成缩进,图标和文本。这里的来源是:

package spacecheck.ui.renderer; 

import java.awt.Component; 

import javax.swing.ImageIcon; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.TableCellRenderer; 

import spacecheck.filedata.FileCollection; 
import spacecheck.images.TreeIcon; 

/** 
* renders the name of a collection for the tree display. 
* @author rcook 
* 
*/ 
public class CollectionNameRenderer extends DefaultTableCellRenderer // which implements JLabel 
// implements TableCellRenderer 
{ 
    private static final long serialVersionUID = 1L; 
    @SuppressWarnings({"unused"}) 
    private void say(String msg) { System.out.println(msg); } 

    private static TreeIcon tIcon = null; 

    /** 
    * set the value of the CollectionName for the JTable; includes using 
    * indent so that the correct icon can be obtained (icons are different widths 
    * to implement different indent levels). 
    */ 
    public void setValue(Object value) 
    { 
    FileCollection fc = (FileCollection)value; 
    boolean expanded = fc.isExpanded(); 
    int level = fc.getDisplayLevel(); 
// boolean isSelected = table. 
    ImageIcon icon = tIcon.getIcon(level, expanded); 
    if (icon != null) { setIcon(icon); } 
    setText(value.toString()); 
    } 

    /** 
    * get the renderer component for a collection name. 
    */ 
    public Component getTableCellRendererComponent 
     (JTable table, Object value, boolean isSelected, boolean hasFocus, 
      int rowIndex, int colIndex) 
    { 
    if (tIcon == null) { tIcon = new TreeIcon(table.getBackground()); } 
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, rowIndex, colIndex); 
    } 
} 

该类使用另一个名为TreeIcon;它实现文件夹图标的缩进,如图所示,并根据文件夹的展开/未展开状态选择图标。下面是类:

package spacecheck.images; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.util.ArrayList; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/** 
* represents an icon used in the directory tree; handles 'expanded' and 
* 'unexpanded' directories as well as indentation representing different 
* levels. 
* @author rcook 
* 
*/ 
public class TreeIcon 
{ 
    public static int UNEXPANDED = 1; 
    public static int EXPANDED = 2; 

    @SuppressWarnings({"unused"}) 
    private void say (String msg) { System.out.println(msg); } 

    private static ImageIcon expandedIcon = null; 
    private static ImageIcon unexpandedIcon = null; 
    private static int  iconHeight = 0; 
    private static int  iconWidth = 0; 

    private static ArrayList<ImageIcon> cachedExpandedIcons = new ArrayList<ImageIcon>(); 
    private static ArrayList<ImageIcon> cachedUnexpandedIcons = new ArrayList<ImageIcon>(); 

    static 
    { 
    expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.png")); 
    unexpandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Unexpanded.png")); 
    iconHeight = unexpandedIcon.getIconHeight(); 
    iconWidth = unexpandedIcon.getIconWidth(); 
    } 

    public TreeIcon(Color givenColor) { } 

    public static void main(String ... arguments) 
    { 
    JFrame frame = new JFrame("icon test"); 
    JLabel label = new JLabel("background test"); 
    label.setBackground(Color.blue); 
    TreeIcon treeIcon = new TreeIcon(Color.black); 
    ImageIcon icon = treeIcon.getIcon(2, false); 
    label.setIcon(icon); 
    frame.add(label); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    /** 
    * return the icon for an expanded or unexpanded level 
    * @param int level of folder relative to other levels displayed; 
    * starts at 0 and increases with depth 
    * @param boolean indicates whether this level is expanded or not. 
    * @return ImageIcon appropriate for expansion flag and level. 
    */ 
    public ImageIcon getIcon(int level, boolean expanded) 
    { 
    ImageIcon result = null; 

    if (level < 0) 
    { System.out.println("level is " + level + ", setting to 0"); 
     level = 0; 
    } 

    // set our list of icons depending on whether we are expanded. 
    ArrayList<ImageIcon> cachedIcons = cachedUnexpandedIcons; 
    if (expanded) { cachedIcons = cachedExpandedIcons; } 

    // if we already have this icon in our cache, return it. 
    if (cachedIcons.size() >= (level+1) && cachedIcons.get(level) != null) 
    { 
     result = cachedIcons.get(level); 
    } 
    else 
    { 
     // generate this icon and store it in the cache before returning it. 
     ImageIcon baseIcon = unexpandedIcon; 
     if (expanded) { baseIcon = expandedIcon; } 
     int iconH = iconHeight; 
     int iconW = iconWidth*(level+1); 

     BufferedImage bufferedImage = new BufferedImage(iconW,iconH,BufferedImage.TYPE_INT_ARGB); 
     Graphics g = bufferedImage.getGraphics(); 

     g.drawImage(baseIcon.getImage(), iconWidth*level, 0, null); 

     result = new ImageIcon(bufferedImage); 

     // we've created an icon that was not in the cached list; 
     // the cached list may have a null at this slot, or it may not yet be 
     // long enough to have this slot. Ensure that we have enough slots 
     // in the list, and then add this icon. 
     for (int i=cachedIcons.size(); i<=level; i++) 
     { 
     cachedIcons.add(null); 
     } 
//  if (cachedIcons.size() < level + 1) { cachedIcons.add(result); } 
//          else { 
     cachedIcons.set(level, result); 
//  } 
//  say("adding icon, level = " + level + (expanded ? " " : " un") + "expanded, width = " + iconW); 
    } 

    return result; 
    } 
} 

要选择不同类型的文件的图标,你可以有你的渲染和图标选取器看文件的扩展名(或其他),以确定哪个图标拿出地图来使用。

希望有帮助!

+0

非常酷,我要检查它 – lenhhoxung 2014-12-27 16:26:59