Java 获取 CheckedComboBox 的值
Java Getting the values of CheckedComboBox
我按照此 tutorial 创建了一个 checkedcombobox 控件,但我不知道如何从控件中获取选定的值,并将其显示在控制台中。这是我的代码片段:
CheckableItem[] sizes = {
new CheckableItem("2T/3T",false),
new CheckableItem("4T",false),
new CheckableItem("5T",false),
new CheckableItem("4",false),
new CheckableItem("6",false),
new CheckableItem("8",false),
new CheckableItem("10",false)
};
CheckedComboBox<CheckableItem> cmbSizes = new CheckedComboBox<CheckableItem>(new DefaultComboBoxModel<CheckableItem>(sizes));
请注意,完整的控件class可以在提供的link中下载。
这是 CheckedComboBox 的 class 代码:
class CheckedComboBox<E extends CheckableItem> extends JComboBox<E>{
private boolean keepOpen;
private transient ActionListener listener;
protected CheckedComboBox(){
super();
}
protected CheckedComboBox(ComboBoxModel<E> aModel) {
super(aModel);
}
protected CheckedComboBox(E[] m){
super(m);
}
@Override public Dimension getPreferredSize(){
return new Dimension(100,20);
}
@Override public void updateUI(){
setRenderer(null);
removeActionListener(listener);
super.updateUI();
listener = e->{
if(e.getModifiers() == InputEvent.BUTTON1_MASK){
updateItem(getSelectedIndex());
keepOpen = true;
}
};
setRenderer(new CheckBoxCellRenderer());
addActionListener(listener);
getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0),"checkbox-select");
getActionMap().put("checkbox-select",new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Accessible a = getAccessibleContext().getAccessibleChild(0);
if(a instanceof BasicComboPopup){
BasicComboPopup pop = (BasicComboPopup) a;
updateItem(pop.getList().getSelectedIndex());
}
}
});
}
private void updateItem(int index) {
if (isPopupVisible()) {
E item = getItemAt(index);
item.selected ^= true;
removeItemAt(index);
insertItemAt(item, index);
setSelectedItem(item);
}
}
@Override public void setPopupVisible(boolean v) {
if (keepOpen) {
keepOpen = false;
} else {
super.setPopupVisible(v);
}
}
}
class CheckBoxCellRenderer<E extends CheckableItem> implements ListCellRenderer<E> {
private final JLabel label = new JLabel(" ");
private final JCheckBox check = new JCheckBox(" ");
@Override public Component getListCellRendererComponent(JList list, CheckableItem value, int index, boolean isSelected, boolean cellHasFocus) {
if (index < 0) {
label.setText(getCheckedItemString(list.getModel()));
return label;
} else {
check.setText(Objects.toString(value, ""));
check.setSelected(value.selected);
if (isSelected) {
check.setBackground(list.getSelectionBackground());
check.setForeground(list.getSelectionForeground());
} else {
check.setBackground(list.getBackground());
check.setForeground(list.getForeground());
}
return check;
}
}
private static String getCheckedItemString(ListModel model) {
List<String> sl = new ArrayList<>();
for (int i = 0; i < model.getSize(); i++) {
Object o = model.getElementAt(i);
if (o instanceof CheckableItem && ((CheckableItem) o).selected) {
sl.add(o.toString());
}
}
return sl.stream().sorted().collect(Collectors.joining(", "));
}
}
class CheckableItem {
public final String text;
public boolean selected;
protected CheckableItem(String text, boolean selected) {
this.text = text;
this.selected = selected;
}
@Override public String toString() {
return text;
}
}
我认为这就是您要找的,如果不是请分享您正在尝试解决的确切问题
if (index < 0) {
label.setText(getCheckedItemString(list.getModel()));
System.out.println(getCheckedItemString(list.getModel()));
return label;
}
我试过 japura 插件,它解决了我的问题。这是到目前为止的代码:
List<String> sizes = new ArrayList<>();
sizes.add("2T/3T");
sizes.add("4T");
sizes.add("5T");
sizes.add("4");
sizes.add("6");
sizes.add("8");
sizes.add("10");
CheckComboBox cmbSizes = new CheckComboBox();
ListCheckModel model = cmbSizes.getModel();
cmbSizes.setTextFor(CheckComboBox.NONE, "*Please select size*");
//cmbSizes.setTextFor(CheckComboBox.MULTIPLE,String.valueOf(model.getCheckeds()));
for(String size:sizes){
model.addElement(size);
}
model.addListCheckListener(new ListCheckListener() {
@Override
public void removeCheck(ListEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void addCheck(ListEvent arg0) {
System.out.println(String.valueOf(model.getCheckeds()));
}
});
我按照此 tutorial 创建了一个 checkedcombobox 控件,但我不知道如何从控件中获取选定的值,并将其显示在控制台中。这是我的代码片段:
CheckableItem[] sizes = {
new CheckableItem("2T/3T",false),
new CheckableItem("4T",false),
new CheckableItem("5T",false),
new CheckableItem("4",false),
new CheckableItem("6",false),
new CheckableItem("8",false),
new CheckableItem("10",false)
};
CheckedComboBox<CheckableItem> cmbSizes = new CheckedComboBox<CheckableItem>(new DefaultComboBoxModel<CheckableItem>(sizes));
请注意,完整的控件class可以在提供的link中下载。
这是 CheckedComboBox 的 class 代码:
class CheckedComboBox<E extends CheckableItem> extends JComboBox<E>{
private boolean keepOpen;
private transient ActionListener listener;
protected CheckedComboBox(){
super();
}
protected CheckedComboBox(ComboBoxModel<E> aModel) {
super(aModel);
}
protected CheckedComboBox(E[] m){
super(m);
}
@Override public Dimension getPreferredSize(){
return new Dimension(100,20);
}
@Override public void updateUI(){
setRenderer(null);
removeActionListener(listener);
super.updateUI();
listener = e->{
if(e.getModifiers() == InputEvent.BUTTON1_MASK){
updateItem(getSelectedIndex());
keepOpen = true;
}
};
setRenderer(new CheckBoxCellRenderer());
addActionListener(listener);
getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0),"checkbox-select");
getActionMap().put("checkbox-select",new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Accessible a = getAccessibleContext().getAccessibleChild(0);
if(a instanceof BasicComboPopup){
BasicComboPopup pop = (BasicComboPopup) a;
updateItem(pop.getList().getSelectedIndex());
}
}
});
}
private void updateItem(int index) {
if (isPopupVisible()) {
E item = getItemAt(index);
item.selected ^= true;
removeItemAt(index);
insertItemAt(item, index);
setSelectedItem(item);
}
}
@Override public void setPopupVisible(boolean v) {
if (keepOpen) {
keepOpen = false;
} else {
super.setPopupVisible(v);
}
}
}
class CheckBoxCellRenderer<E extends CheckableItem> implements ListCellRenderer<E> {
private final JLabel label = new JLabel(" ");
private final JCheckBox check = new JCheckBox(" ");
@Override public Component getListCellRendererComponent(JList list, CheckableItem value, int index, boolean isSelected, boolean cellHasFocus) {
if (index < 0) {
label.setText(getCheckedItemString(list.getModel()));
return label;
} else {
check.setText(Objects.toString(value, ""));
check.setSelected(value.selected);
if (isSelected) {
check.setBackground(list.getSelectionBackground());
check.setForeground(list.getSelectionForeground());
} else {
check.setBackground(list.getBackground());
check.setForeground(list.getForeground());
}
return check;
}
}
private static String getCheckedItemString(ListModel model) {
List<String> sl = new ArrayList<>();
for (int i = 0; i < model.getSize(); i++) {
Object o = model.getElementAt(i);
if (o instanceof CheckableItem && ((CheckableItem) o).selected) {
sl.add(o.toString());
}
}
return sl.stream().sorted().collect(Collectors.joining(", "));
}
}
class CheckableItem {
public final String text;
public boolean selected;
protected CheckableItem(String text, boolean selected) {
this.text = text;
this.selected = selected;
}
@Override public String toString() {
return text;
}
}
我认为这就是您要找的,如果不是请分享您正在尝试解决的确切问题
if (index < 0) {
label.setText(getCheckedItemString(list.getModel()));
System.out.println(getCheckedItemString(list.getModel()));
return label;
}
我试过 japura 插件,它解决了我的问题。这是到目前为止的代码:
List<String> sizes = new ArrayList<>();
sizes.add("2T/3T");
sizes.add("4T");
sizes.add("5T");
sizes.add("4");
sizes.add("6");
sizes.add("8");
sizes.add("10");
CheckComboBox cmbSizes = new CheckComboBox();
ListCheckModel model = cmbSizes.getModel();
cmbSizes.setTextFor(CheckComboBox.NONE, "*Please select size*");
//cmbSizes.setTextFor(CheckComboBox.MULTIPLE,String.valueOf(model.getCheckeds()));
for(String size:sizes){
model.addElement(size);
}
model.addListCheckListener(new ListCheckListener() {
@Override
public void removeCheck(ListEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void addCheck(ListEvent arg0) {
System.out.println(String.valueOf(model.getCheckeds()));
}
});