forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
57 lines
1.6 KiB
6 years ago
|
package com.fanruan.api.design.work.component;
|
||
|
|
||
|
import com.fanruan.api.design.ui.component.UIComboBox;
|
||
|
import com.fr.design.gui.icombobox.UIComboBoxRenderer;
|
||
|
|
||
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
|
||
|
/**
|
||
|
* @author richie
|
||
|
* @version 10.0
|
||
|
* Created by richie on 2019/11/1
|
||
|
* 用于选择和展示整数的下拉框
|
||
|
*/
|
||
|
public class IntComboBox extends UIComboBox {
|
||
|
|
||
|
public IntComboBox() {
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
public IntComboBox(Integer[] data) {
|
||
|
ComboBoxModel<Integer> model = new DefaultComboBoxModel<>(data);
|
||
|
setModel(model);
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
private void init() {
|
||
|
this.setRenderer(new UIComboBoxRenderer() {
|
||
|
public Component getListCellRendererComponent(JList list,
|
||
|
Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||
|
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||
|
if (value instanceof Integer) {
|
||
|
this.setText(" " + value + " ");
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public int getSelectedInt() {
|
||
|
if (this.getSelectedItem() == null) {
|
||
|
return -1;
|
||
|
}
|
||
|
return (Integer) this.getSelectedItem();
|
||
|
}
|
||
|
|
||
|
public void setSelectedInt(int selectedInt) {
|
||
|
int index = -1;
|
||
|
for (int i = 0; i < this.getItemCount(); i++) {
|
||
|
if ((Integer) this.getItemAt(i) == selectedInt) {
|
||
|
index = i;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
this.setSelectedIndex(index);
|
||
|
}
|
||
|
}
|