我正在从 JList
中的数据库中获取数据,我在这里使用
{
...
...
DefaultListModel m=new DefaultListModel();
String sqllist="select * from cnotezdetails cn where cn.case_count_no="+c+"";
System.out.println(sqllist);
pst=conn.prepareStatement(sqllist);
rs= pst.executeQuery();
while(rs.next()){
int id=rs.getInt(1);
String date=rs.getString("nextDate");
String status=rs.getString("status");
String notes=rs.getString("notes");
System.out.println(date);
m.addElement(id+date+notes);
}
jList1.setModel(m);
}
tmp=(integer)JList1.getSelectedValue();
这里我只需要选择 id 值,但 getSelected
id、Date、notes。但是我只需要选择 id 值来在数据库中进行比较
即,
id Date notes
1 25/08/2014 note1
2 26/08/2014 note2
这里我只需要获取 id 值,即 id=1,2 等。
我怎样才能做到这一点?
最佳答案
就像@MadProgrammer 建议的那样。使用简单的 POJO 来包装值。您可以覆盖 toString()
以获得所需的输出。选择项目时,仅从 pojo 中获取值。
类似于:
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class PojoListDemo {
public PojoListDemo() {
JFrame frame = new JFrame();
frame.add(new JScrollPane(getPojoList()));
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JList getPojoList() {
JList list = new JList(getListModel());
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
Pojo pojo = (Pojo) ((JList) e.getSource())
.getSelectedValue();
System.out.println("Selected id: " + pojo.getId());
}
}
});
return list;
}
private ListModel getListModel() {
DefaultListModel<Pojo> model = new DefaultListModel<Pojo>();
model.addElement(new Pojo(1, new Date(), "Hello World"));
model.addElement(new Pojo(2, new Date(), "Hello Stack Overflow"));
model.addElement(new Pojo(3, new Date(), "Hello Grandma"));
return model;
}
class Pojo {
private int id;
private Date date;
private String description;
public Pojo(int id, Date date, String description) {
this.id = id;
this.date = date;
this.description = description;
}
public int getId() {
return id;
}
public Date getDate() {
return date;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return MessageFormat.format("{0} - {1} - {2}",
getId(),
new SimpleDateFormat("dd MMM yyyy").format(getDate()),
getDescription());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PojoListDemo();
}
});
}
}
更新
如果您知道如何使用 jdbc,则从数据库数据创建列表是微不足道的。只需将每个结果行属性添加到 pojo 中的相应属性即可。有点像
private ListModel getListModel() {
DefaultListModel<Pojo> model = new DefaultListModel<Pojo>();
...
// access your database and get a ResultSet
...
while(rs.next()) {
int id = rs.getInt("id");
Date date = rs.getDate("date");
String description = rs.getString("description");
model.addElement(new Pojo(id, date, description));
}
// close resources
return model;
}
关于java - 如果 java swings Gui 中的单行中存在多列,如何在 jList 中获取 id。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25483867/