2014-12-27 69 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