Ahmed Faraz
Ahmed Faraz

Reputation: 118

List of Values in Java Swing Form

How can I display an LoV in a Java Swing form as shown in Oracle Forms?

I have data concerning user identifiers and user names. And I want to display the user names in LoV, but if a user selects a name corresponding to a user, its identifier should be returned.

EDIT 1: Here is the form code that I've used to display 'Lov'

import db.DBHelper;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class LovForm extends JDialog implements ActionListener {

    private Connection conn;
    private DBHelper db;
    private JList list;
    private DefaultListModel model;
    private UserDetail userDetail;
    private JButton btnOk;

    public LovForm(Connection c, UserDetail u) {
        this.conn = c;
        this.userDetail = u;
        initComponents();
    }

    private void initComponents() {
        db = new DBHelper(this.conn);
        btnOk = new JButton("Ok");
        btnOk.addActionListener(this);
        Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        model = new DefaultListModel();
        try {
            ResultSet rs = db.getAllAppUsers();
            if (rs != null) {
                while (rs.next()) {
                    String name = rs.getString("NAME");
                    model.addElement(name);
                }
                list = new JList(model);
            }
        } catch (Exception e) {
            list = new JList();
        }

        list.setPreferredSize(new Dimension(150, 250));
        JScrollPane listScroller = new JScrollPane(list);
        JLabel lbl = new JLabel("Select an application user");
        c.add(lbl);
        c.add(listScroller);
        c.add(btnOk);

        this.setTitle("List of Users");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(new Dimension(200, 250));
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setModal(true);    
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e
    ) {
        if (e.getSource() == btnOk) {
            String selectedItem = (String) list.getSelectedValue();
            userDetail.setUserName(selectedItem);
            this.dispose();
        }
    }
}

Upvotes: 1

Views: 1645

Answers (2)

Marius Manastireanu
Marius Manastireanu

Reputation: 2586

I would add a wrapper class for the User, which contains its name and its id.

Do not forget to override the toString() method of the User (that method will be used when rendering the list).

While populating the list, create User objects, and therefore you will have access to both its name and its id.

See code bellow:

    private void initComponents() {
    // your code....

    try {
        ResultSet rs = db.getAllAppUsers();
        if (rs != null) {
            while (rs.next()) {
                String name = rs.getString("NAME");
                int id = rs.getInt("ID"); 
                model.addElement(new User(name, id));
            }
            list = new JList(model);
        }
    } catch (Exception e) {
        list = new JList();
    }

    // your code...
}

@Override
public void actionPerformed(ActionEvent e
) {
    if (e.getSource() == btnOk) {
        User selectedItem = (User) list.getSelectedValue();
        userDetail.setUserName(selectedItem.getName());
        int id = selectedItem.getId();
        this.dispose();
    }
}

public class User {
   private String name;
   private Integer id;

   public User(String name, Integer id) {
       this.name = name;
       this.id = id;
   }

   public String getName() {
       return name;
   }

   public Integer getId() {
      return id;
   }

   @Override
   public String toString() {
      return name;
   }
}

Upvotes: 3

camickr
camickr

Reputation: 324147

You could use a JComboBox.

You would create a custom Object to store both pieces of data. Then you would create a custom renderer to display whatever data you want in the combo box. Your processing code would then use the other property.

Check out Combo Box With Custom Renderer for an example of the rendering code you would use. This renderer is more complete than most rendering code you will find in the forum because it will still support selection of the item from the combo box using the keyboard.

I see you updated your question to show you are using a JList. Well the answer is still the same. You need a custom renderer for your JList. You can base your renderer off the example from the above link except you would extend the DefaultListCellRenderer.

Upvotes: 3

Related Questions