主方法代码:
package com.aynu.layout;
import com.aynu.model.NumberComboBoxListModel;
import com.aynu.model.NumberListModel;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.util.List;
public class ListModelTest {
JFrame jf = new JFrame("测试ListModel");
JList<BigDecimal> jList = new JList<>(new NumberListModel(new BigDecimal(1),new BigDecimal(21),new BigDecimal(2)));
JComboBox<BigDecimal> jComboBox = new JComboBox<>(new NumberComboBoxListModel(new BigDecimal(0.1),new BigDecimal(1.2),new BigDecimal(0.1)));
JLabel label = new JLabel("您选择的值是:");
JTextField jTextField = new JTextField(15);
public void init(){
//组装视图
//组装顶部
jList.setVisibleRowCount(4);
jList.setSelectionInterval(2,4);
jList.setFixedCellWidth(90);
jList.setFixedCellHeight(30);
//注册监听器
jList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
List<BigDecimal> selectedValuesList = jList.getSelectedValuesList();
jTextField.setText("");
for (BigDecimal item : selectedValuesList){
jTextField.setText(jTextField.getText()+item.toString()+",");
}
}
});
jComboBox.setMaximumRowCount(4);
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = jComboBox.getSelectedItem();
jTextField.setText(selectedItem.toString());
}
});
Box hBox = Box.createHorizontalBox();
hBox.add(new JScrollPane(jList));
JPanel tempPanel = new JPanel();
tempPanel.add(jComboBox);
hBox.add(tempPanel);
jf.add(hBox);
//组装底部
JPanel bottomPanel = new JPanel();
bottomPanel.add(label);
bottomPanel.add(jTextField);
jf.add(bottomPanel, BorderLayout.SOUTH);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new ListModelTest().init();
}
}
package com.aynu.model;
import javax.swing.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class NumberListModel extends AbstractListModel<BigDecimal> {
BigDecimal start;
BigDecimal end;
BigDecimal step;
public NumberListModel(BigDecimal start, BigDecimal end, BigDecimal step) {
this.start = start;
this.end = end;
this.step = step;
}
@Override
public int getSize() {
int floor = (int) Math.floor(end.subtract(start).divide(step, 2, RoundingMode.HALF_DOWN).doubleValue());
return floor + 1;
}
@Override
public BigDecimal getElementAt(int index) {
BigDecimal ele = new BigDecimal(index).multiply(step).add(start).setScale(2, RoundingMode.HALF_DOWN);
return ele;
}
}
package com.aynu.model;
import javax.swing.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class NumberComboBoxListModel extends NumberListModel implements ComboBoxModel<BigDecimal> {
//当前选中条目的索引
private int selectedId;
public NumberComboBoxListModel(BigDecimal start, BigDecimal end, BigDecimal step) {
super(start, end, step);
}
@Override
public void setSelectedItem(Object anItem) {
if (anItem instanceof BigDecimal) {
BigDecimal curr = (BigDecimal) anItem;
selectedId = curr.subtract(super.start).divide(super.step, 2, RoundingMode.HALF_DOWN).intValue();
}
}
@Override
public Object getSelectedItem() {
BigDecimal item = new BigDecimal(selectedId).multiply(super.step).add(super.start).setScale(1, RoundingMode.HALF_DOWN);
return item;
}
}