1,先看效果
?
上面是文本框,紧挨着下面是对应的下拉框,点击下拉框时,下拉框中的item的内容自动填充到上面的文本框中.
实现代码:
?
public static JComboBox<String> comboBoxSelectedHandle(JComboBox<String> comboBox,final JTextField ipTextField){
if(ValueWidget.isNullOrEmpty(comboBox)){
comboBox = new JComboBox<String>();
}
comboBox.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JComboBox<String> target=(JComboBox<String>)e.getSource();
String selectedPort=(String)target.getSelectedItem();
if(!ValueWidget.isNullOrEmpty(selectedPort)){
ipTextField.setText(selectedPort);
}
// System.out.println(e.getSource());
}
});
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox<String> target=(JComboBox<String>)e.getSource();
String selectedPort=(String)target.getSelectedItem();
if(!ValueWidget.isNullOrEmpty(selectedPort)){
ipTextField.setText(selectedPort);
}
}
});
return comboBox;
}
?
2,项目
采用Java swing编写,使用maven构建
源代码见附件
?
?
?
?
?
原文:http://hw1287789687.iteye.com/blog/2217653