如何在Java中为JComboBox添加分隔符? [英] How do I add a separator to a JComboBox in Java?

查看:304
本文介绍了如何在Java中为JComboBox添加分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JComboBox ,并希望在元素列表中有一个分隔符。我如何用Java做到这一点?

I have a JComboBox and would like to have a separator in the list of elements. How do I do this in Java?

一个示例场景,它可以派上用场进行字体系列选择;类似于Word和Excel中的font-family-selection-control。在这种情况下,我想在顶部显示最常用的字体,然后是分隔符,最后按字母顺序显示分隔符下面的所有字体系列。

A sample scenario where this would come in handy is when making a combobox for font-family-selection; similar to the font-family-selection-control in Word and Excel. In this case I would like to show the most-used-fonts at the top, then a separator and finally all font-families below the separator in alphabetical order.

可以有人帮我怎么做或者这在Java中不可能吗?

Can anyone help me with how to do this or is this not possible in Java?

推荐答案

有一个非常简短的教程,有一个例子它展示了如何在java2s上使用自定义ListCellRenderer
http:/ /www.java2s.com/Code/Java/Swing-Components/BlockComboBoxExample.htm

There is a pretty short tutorial with an example that shows how to use a custom ListCellRenderer on java2s http://www.java2s.com/Code/Java/Swing-Components/BlockComboBoxExample.htm

基本上它涉及在列表模型中插入已知占位符以及何时你在ListCellRenderer中检测到占位符,你返回一个'new JSeparator(JSeparator.HORIZONTAL)'的实例

Basically it involves inserting a known placeholder in your list model and when you detect the placeholder in the ListCellRenderer you return an instance of 'new JSeparator(JSeparator.HORIZONTAL)'

这篇关于如何在Java中为JComboBox添加分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆