JComboBox实现键值绑定
来源:互联网 发布:热血战歌羽化降级数据 编辑:程序博客网 时间:2024/06/05 18:42
例子1(没有对数据库操作):
import java.awt.BorderLayout;
import java.awt.Component;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class ComboBoxItem extends JFrame implements ActionListener {
public ComboBoxItem() {
Vector model = new Vector();
model.addElement(new Item(1, "car"));
model.addElement(new Item(2, "plane"));
model.addElement(new Item(3, "train"));
model.addElement(new Item(4, "boat"));
JComboBox comboBox;
// Easiest approach is to just override toString() method
// of the Item class
comboBox = new JComboBox(model);
// comboBox.setDragEnabled(true);
comboBox.addActionListener(this);
getContentPane().add(comboBox, BorderLayout.NORTH);
// Most flexible approach is to create a custom render
// to diplay the Item data
comboBox = new JComboBox(model);
// comboBox.setDragEnabled(true);
comboBox.setRenderer(new ItemRenderer());
comboBox.addActionListener(this);
getContentPane().add(comboBox, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Item item = (Item) comboBox.getSelectedItem();
System.out.println(item.getId() + " : " + item.getDescription());
}
class ItemRenderer extends BasicComboBoxRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
Item item = (Item) value;
setText(item.getDescription().toUpperCase());
}
if (index == -1) {
Item item = (Item) value;
setText("" + item.getId());
}
return this;
}
}
class Item {
private int id;
private String description;
public Item(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public String toString() {
return description;
}
}
public static void main(String[] args) {
JFrame frame = new ComboBoxItem();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
例子2(对数据库操作):
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class ComboKeyValue extends JFrame implements ActionListener {
public ComboKeyValue() {
JComboBox comboBox;
Vector model = new Vector();
String locationid = "";
String locationname = "";
try {
Connection conn = getConnection();
Statement st = conn.createStatement();
String sqllocations = "select id, name from locations";
ResultSet rss = st.executeQuery(sqllocations);
while (rss.next()) {
locationid = rss.getString("id");
locationname = rss.getString("name");
KeyValue Itemlocation = new KeyValue(locationid, locationname);
model.addElement(Itemlocation);
}
} catch (SQLException e) {
e.printStackTrace();
}
comboBox = new JComboBox(model);
comboBox.addActionListener(this);
getContentPane().add(comboBox, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
KeyValue item = (KeyValue) comboBox.getSelectedItem();
System.out.println(item.getId() + " 对应 " + item.getName());
}
class KeyValue {
private String id;
private String name;
public KeyValue(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
}
public static Connection getConnection() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ce) {
System.out.print(ce.getMessage());
}
try {
String url = "jdbc:postgresql://localhost:5432/openbravopos";
String username = "postgres";
String password = "123";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void main(String[] args) {
JFrame frame = new ComboKeyValue();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
- JComboBox实现键值绑定
- Swing JComboBox实现键值
- swing jComboBox 绑定数据库
- jComboBox绑定数据库
- JComboBox实现联动效果
- WPF DevExpress ComboBoxEdit 实现键值绑定,输入字符自动匹配
- vue动态载入变量键值名,实现数据双向绑定
- 键值编码 || Cocoa绑定
- JComboBox
- JcomboBox
- JCombobox
- 用JComboBox实现三级联动
- java swing model实现JComboBox
- JCombobox组合框效果实现
- Spinner绑定键值对信息
- java JComboBox显示值与绑定至分开
- 实现JComboBox中添加自定义对象
- [Swing]JCombobox组合框效果实现
- oracle结构体系
- Openmeetings 1.8版本Windows安装详解
- Javascript实现复制到剪贴板
- URL的协议和Internet的服务
- Exchange 2010收发外网邮件
- JComboBox实现键值绑定
- 网页框架 分帧
- 每个游戏开发者都需要知道的游戏网络知识
- Gartner:企业将应用程序迁移到云上的五大方式
- 快速把web项目部署到weblogic上
- 20张图,让你看清2012移动互联网大方向
- 让程序检测出内存泄漏
- openfire数据库表结构
- NET版本的MapReduce--Qizmt