How to get id in jList if multiple columns exist on the same row in java swings Gui.?

I am retrieving data from a database in a JList , here I am using

  { ... ... 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(); 

Here I need to select only the id value, but this is getSelected id, Date, notes. But I only need to select the id value for comparison in the database i.e..,

 id Date notes 1 25/08/2014 note1 2 26/08/2014 note2 

Here I need to get only id value, i.e. id = 1,2 etc.

How can i achieve this?

+5
source share
1 answer

Just try @MadProgrammer. Use a simple POJO to round values. You can override toString() to get the desired result. When an item is selected, just get the value from pojo.

Sort of:

 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(); } }); } } 

UPDATE

Creating a list from db data is trivial if you know how to use jdbc. Just add each row of the attriubutes result to the corresponding attributes in pojo. Sort of

 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; } 
+1
source

Source: https://habr.com/ru/post/1201066/


All Articles