Swing中可多选下拉框的简单实现

实现可多选下拉框需要写三个类:
MyComboBox.java --- 继承自JComboBox
CheckListCellRenderer.java --- 继承自JCheckBox,且实现ListCellRenderer
CheckValue.java --- 设置JCheckBox的类

此处也是比较简单的实现,具体为以下为代码:


####MyComboBox.java####

public class MyComboBox extends JComboBox implements ActionListener {
public MyComboBox() {
addItem(new CheckValue(false, "Select All"));
this.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
itemSelected();
}
});
}

private void itemSelected() {
if (getSelectedItem() instanceof CheckValue) {
if (getSelectedIndex() == 0) {
selectedAllItem();
} else {
CheckValue jcb = (CheckValue) getSelectedItem();
jcb.bolValue = (!jcb.bolValue);
setSelectedIndex(getSelectedIndex());
}
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
/*选中后依然保持当前弹出状态*/
showPopup();
}
});
}
}
private void selectedAllItem() {
boolean bl = false;
for (int i = 0; i < getItemCount(); i++) {
CheckValue jcb = (CheckValue) getItemAt(i);
if (i == 0) {
bl = !jcb.bolValue;
}
jcb.bolValue = (bl);
}
setSelectedIndex(0);
}
/*获取选取的对象*/
public Vector getComboVc() {
Vector vc = new Vector();
for (int i = 1; i < getItemCount(); i++) {
CheckValue jcb = (CheckValue) getItemAt(i);
if (jcb.bolValue) {
vc.add(jcb.value);
}
}
return vc;
}
}

###CheckListCellRenderer.java###

public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer,
Serializable {
protected static Border noFocusBorder;
/**
* Constructs a default renderer object for an item
* in a list.
*/
public CheckListCellRenderer() {
super();
if (noFocusBorder == null) {
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
}
setOpaque(true);
setBorder(noFocusBorder);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setComponentOrientation(list.getComponentOrientation());
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
if (value instanceof CheckValue) {
CheckValue ckValue = (CheckValue) value;
this.setText(ckValue.value == null ? "" : ckValue.value);
this.setSelected(ckValue.bolValue);
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setBorder((cellHasFocus) ?
UIManager.getBorder("List.focusCellHighlightBorder") :
noFocusBorder);
return this;
}
}

###CheckValue.java###

public class CheckValue {
public boolean bolValue = false;
public String value = null;
public CheckValue() {
}
public CheckValue(boolean bolValue, String value) {
this.bolValue = bolValue;
this.value = value;
}
}


这三个类放在一个包里,或者简单起见放在一个java文件也可,注意以下inner class和friend class的区别即可。
使用方法也很简单:

for (int i = 0; i < 10; i++) {
CheckValue cValue = new CheckValue();
cValue.value = "测试_" + i;
if (i % 3 == 0) {
cValue.bolValue = true;
}
jComboBox1.addItem(cValue);
}
jComboBox1.setRenderer(new CheckListCellRenderer());
jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 12));



以上是不完整的测试代码,MyComboBox实现的功能是可以多选List项,且占用空间比较小,比传统的JList控件使用也方便,JList一般都是使用Ctrl或Shift键来多选,使用起来不是那么一目了然,MyComboBox还可以实现全选和全部不选的功能,当然这只是非常简单的实现,可以扩展的地方还很多,可以实现多种颜色Item等。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap 提供了一个多框组件,可以通过添加 `multiple` 属性来实现。 示例代码: ```html <select multiple class="form-control"> <option>项1</option> <option>项2</option> <option>项3</option> <option>项4</option> <option>项5</option> </select> ``` 你可以在 `<select>` 元素上添加 `form-control` 类来应用 Bootstrap 的样式。 如果需要在显示更多信息,可以使用 `data-*` 属性来添加自定义数据。例如: ```html <select multiple class="form-control"> <option data-subtext="描述1">项1</option> <option data-subtext="描述2">项2</option> <option data-subtext="描述3">项3</option> </select> ``` 在这个例子,`data-subtext` 属性用于添加描述信息。这个属性可以在 JavaScript 通过 `dataset` 属性来获取。 另外,如果需要对多框进行进一步的自定义,可以使用 Bootstrap 的 JavaScript 插件。例如,可以使用 `selectpicker` 插件来添加搜索框和样式定制。示例代码: ```html <select multiple class="form-control selectpicker" data-live-search="true" data-style="btn-primary"> <option>项1</option> <option>项2</option> <option>项3</option> <option>项4</option> <option>项5</option> </select> ``` 在这个例子,我们添加了 `selectpicker` 类来启用插件,并使用 `data-live-search` 属性来添加搜索框。另外,使用 `data-style` 属性来设置样式。你需要在页面引入 `bootstrap-select` 插件的 JavaScript 和 CSS 文件才能使用这个插件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值