Java/Swing 中的分段列表?

发布于 2024-07-22 05:19:53 字数 399 浏览 4 评论 0原文

我在 JList 中有一个项目列表供用户选择。 由于它有很多项目(例如,州中的城市),我想将列表分为几个部分。 不过,章节标题不应该是可选择的。 因此,对于我的城市/州示例,这可能如下所示:

  • 州 1
    • 城市1
    • 城市 2
    • 城市 3
  • 州 2
    • 城市 4
    • 城市 5
    • 城市 6

通过将 JList 嵌入自定义 ListCellRenderer 中,自己编写此代码并不困难,但我想知道是否那里已经有这样的课程了。

I have a list of items in a JList for the user to select. Since it's a lot of items (say, cities in states), I want to divide the list into sections. The section headings should not be selectable, though. So for my cities/states example, this might look like this:

  • State 1
    • City 1
    • City 2
    • City 3
  • State 2
    • City 4
    • City 5
    • City 6

It wouldn't be so difficult to write this myself by embedding JLists in a custom ListCellRenderer, but I'm wondering if there already is a class like that out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九厘米的零° 2024-07-29 05:19:54

您可以使用 Apple 所谓的 SourceList。 您可以在 iTunes 和 Mac OS X 的 Finder 中看到它们的运行情况。 这是对您所描述的问题的优雅解决方案。

用于执行此操作的跨平台开源 Java Swing 组件如下:
http://explodingpixels.wordpress.com/2008/09 /08/mac-widgets-for-java/

You could use what Apple calls a SourceList. You see them in action in iTunes and in Mac OS X's Finder. It is an elegant solution to the problem you describe.

A cross-platform, open source Java Swing component for doing this is here:
http://explodingpixels.wordpress.com/2008/09/08/mac-widgets-for-java/

も星光 2024-07-29 05:19:53

JIDE 提供了一个组件,可以让您做到这一点。 它称为 GroupList:

替代文字
(来源:jidesoft.com)

There's a component available with JIDE that let's you do exactly this. It's called GroupList:

alt text
(source: jidesoft.com)

谁与争疯 2024-07-29 05:19:53

也许通过使用 JTree ? 你所描述的是一棵有两层的树。

Maybe by using JTree ? What you describe is a Tree with two levels.

半窗疏影 2024-07-29 05:19:53

我看到这个问题已经得到解答,但我注意到罗伯特评论说他希望有一个开源解决方案。 我建议使用 Glazed Lists 的分隔符列表,您可以在此处找到该 API:

http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/SeparatorList.html

以下是一些示例代码,将生成按首字母分组的项目列表:

alt text http://img300.imageshack.us /img300/8977/separatorlist.png

public class SeparatorListTest {

private static Comparator<String> createComparator() {
    return new Comparator<String>() {
        public int compare(String stringOne, String stringTwo) {
            return stringOne.substring(0,1).compareTo(stringTwo.substring(0,1));
        }
    };
}

private static ListCellRenderer createListCellRenderer() {
    return new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (value instanceof SeparatorList.Separator) {
                SeparatorList.Separator separator = (SeparatorList.Separator) value;
                label.setText(separator.getGroup().get(0).toString().substring(0,1));
                label.setFont(label.getFont().deriveFont(Font.BOLD));
                label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
            } else {
                label.setFont(label.getFont().deriveFont(Font.PLAIN));
                label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
            }

            return label;
        }
    };
}

public static void main(String[] args) {
    EventList<String> rawList = GlazedLists.eventListOf(
            "apple", "appricot", "acorn", "blueberry", "coconut", "chesnut", "grape");
    SeparatorList<String> separatorList = 
            new SeparatorList<String>(rawList, createComparator(), 1, 1000);

    JList list = new JList(new EventListModel<String>(separatorList));
    list.setCellRenderer(createListCellRenderer());
    JScrollPane scrollPane = new JScrollPane(list);
    scrollPane.setBorder(null);

    JFrame frame = new JFrame();
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

I see this question is already answered, but I noticed that Robert commented that he was hoping for an open source solution. I'd recommend using Glazed Lists' Separator list, the API for which you can be found here:

http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/SeparatorList.html

Here's some sample code that will produce a list of items grouped by their first letter:

alt text http://img300.imageshack.us/img300/8977/separatorlist.png

public class SeparatorListTest {

private static Comparator<String> createComparator() {
    return new Comparator<String>() {
        public int compare(String stringOne, String stringTwo) {
            return stringOne.substring(0,1).compareTo(stringTwo.substring(0,1));
        }
    };
}

private static ListCellRenderer createListCellRenderer() {
    return new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (value instanceof SeparatorList.Separator) {
                SeparatorList.Separator separator = (SeparatorList.Separator) value;
                label.setText(separator.getGroup().get(0).toString().substring(0,1));
                label.setFont(label.getFont().deriveFont(Font.BOLD));
                label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
            } else {
                label.setFont(label.getFont().deriveFont(Font.PLAIN));
                label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
            }

            return label;
        }
    };
}

public static void main(String[] args) {
    EventList<String> rawList = GlazedLists.eventListOf(
            "apple", "appricot", "acorn", "blueberry", "coconut", "chesnut", "grape");
    SeparatorList<String> separatorList = 
            new SeparatorList<String>(rawList, createComparator(), 1, 1000);

    JList list = new JList(new EventListModel<String>(separatorList));
    list.setCellRenderer(createListCellRenderer());
    JScrollPane scrollPane = new JScrollPane(list);
    scrollPane.setBorder(null);

    JFrame frame = new JFrame();
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文