用Java创建一个漂亮的listview

用Java创建一个漂亮的listview,java,swing,jlist,Java,Swing,Jlist,我是JavaSwing新手,我正在尝试创建一个显示目录内容的列表视图。我希望创建一个类似下图的视图: 我知道如何使用JList,但我不知道如何显示与文件类型匹配的图标。如您所见,从图像中,我们可以直观地区分pdf文件、文本文件和其他文件。我应该尝试使用JList还是其他UI组件?我也做过类似的事情;下面是我的输出示例 我为树使用了自定义渲染器;它生成缩进、图标和文本,您可以在显示器最左侧列的一个单元格中看到这些内容。以下是这方面的来源: package spacecheck.ui.rende

我是JavaSwing新手,我正在尝试创建一个显示目录内容的列表视图。我希望创建一个类似下图的视图:


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

我也做过类似的事情;下面是我的输出示例

我为树使用了自定义渲染器;它生成缩进、图标和文本,您可以在显示器最左侧列的一个单元格中看到这些内容。以下是这方面的来源:

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;
  }
}
package spacecheck.images;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
/**
*表示目录树中使用的图标;处理“扩展”和
*“未展开”目录以及表示不同
*水平。
*@author-rcook
*
*/
公共类树
{
未展开的公共静态int=1;
公共静态int=2;
@SuppressWarnings({“未使用”})
私有void say(String msg){System.out.println(msg);}
私有静态图像图标expandedIcon=null;
私有静态ImageIcon unexpandedIcon=null;
私有静态int-iconHeight=0;
私有静态int-iconWidth=0;
私有静态ArrayList CacheDexPandeDictions=new ArrayList();
私有静态ArrayList cachedUnexpandedIcons=新ArrayList();
静止的
{
expandedIcon=newImageIcon(TreeIcon.class.getResource(“images/Expanded.png”);
unexpandedIcon=newImageIcon(TreeIcon.class.getResource(“images/Unexpanded.png”);
iconHeight=unexpandedIcon.getIconHeight();
iconWidth=unexpandedIcon.getIconWidth();
}
公共树(颜色给定颜色){}
公共静态void main(字符串…参数)
{
JFrame=新JFrame(“图标测试”);
JLabel标签=新的JLabel(“背景测试”);
标签.背景(颜色.蓝色);
TreeIcon TreeIcon=新的TreeIcon(颜色为黑色);
ImageIcon icon=treeIcon.getIcon(2,false);
label.setIcon(图标);
框架。添加(标签);
frame.pack();
frame.setVisible(true);
}
/**
*返回展开或未展开级别的图标
*@param int相对于显示的其他级别的文件夹级别;
*从0开始,随深度增加而增加
*@param boolean表示是否扩展此级别。
*@return ImageIcon适用于扩展标志和级别。
*/
公共图像图标getIcon(整数级,布尔扩展)
{
ImageIcon结果=空;
如果(级别<0)
{System.out.println(“级别为“+level+”,设置为0”);
级别=0;
}
//根据是否展开设置图标列表。
ArrayList CacheDictions=CacheDuneExpandedicons;
如果(扩展){cachedIcons=cachedExpandedIcons;}
//如果缓存中已经有此图标,请将其返回。
if(cachedIcons.size()>=(级别+1)和&cachedIcons.get(级别)!=null)
{
结果=cachedIcons.get(级别);
}
其他的
{
//生成此图标并在返回之前将其存储在缓存中。
ImageIcon baseIcon=未扩展指令;
如果(已展开){baseIcon=expandedIcon;}
int iconH=iconHeight;
int iconW=iconWidth*(级别+1);
BuffereImage BuffereImage=新的BuffereImage(iconW、iconH、BuffereImage.TYPE_INT_ARGB);
Graphics g=buffereImage.getGraphics();
g、 drawImage(baseIcon.getImage(),iconWidth*级别,0,null);
结果=新图像图标(BuffereImage);
//我们创建了一个不在缓存列表中的图标;
//缓存列表在此插槽处可能有一个空值,或者可能还没有空值
//足够长,可以使用此插槽。请确保我们有足够的插槽
//在列表中,然后添加此图标。

对于(int i=cachedIcons.size();i我做了类似的事情;下面是我的输出示例

我为树使用了自定义渲染器;它生成缩进、图标和文本,您可以在显示最左侧列的一个单元格中看到这些内容。以下是相关的源代码:

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;
  }
}
package spacecheck.images;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
/**
*表示目录树中使用的图标;处理“展开”和
*“未展开”目录以及表示不同
*水平。
*@author-rcook
*
*/
公共类树
{
未展开的公共静态int=1;
公共静态int=2;
@SuppressWarnings({“未使用”})
私有void say(String msg){System.out.println(msg);}
私有静态图像图标expandedIcon=null;
私有静态ImageIcon unexpandedIcon=null;
私有静态int-iconHeight=0;
私有静态int-iconWidth=0;
私有静态ArrayList CacheDexPandeDictions=new ArrayList();
私有静态ArrayList cachedUnexpandedIcons=新ArrayList();
静止的
{
expandedIcon=newImageIcon(TreeIcon.class.getResource(“images/Expanded.png”);
unexpandedIcon=newImageIcon(TreeIcon.class.getResource(“images/Unexpanded.png”);
iconHeight=unexpandedIcon.getIconHeight();
iconWidth=unexpandedIcon.getIconWidth();
}