How would I make a code/name component?

How would I make a code/name component? I want to either type a code into a component or choose the code from a table. Either way I want the component to display both the code and its corresponding name.

For example, in a bank field, I'd like to type "1234" as a bank code and have the component display both "1234" and "First Bank of Duke". Alternatively, I'd like to choose "1234" from a table, and again have the component display both the code and the name."

I've looked at combo boxes and lists, but both looked too crude to do this.

Thanks,

Matt

[585 byte] By [MattCaa] at [2007-10-2 10:23:26]
# 1

is this what you're trying to do?

run the program, then click the "1234" in the list

import javax.swing.*;

import java.awt.*;

import javax.swing.event.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(200,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

DefaultListModel lm = new DefaultListModel();

final JList list = new JList(lm);

JScrollPane sp = new JScrollPane(list);

sp.setPreferredSize(new Dimension(200,100));

lm.addElement(new Bank("1234","First Bank of Duke"));

JPanel panel1 = new JPanel();

panel1.add(sp);

JPanel panel2 = new JPanel(new GridLayout(1,2));

final JLabel label1 = new JLabel(" ",JLabel.CENTER);

final JLabel label2 = new JLabel(" ",JLabel.CENTER);

panel2.add(label1);

panel2.add(label2);

getContentPane().add(panel1,BorderLayout.CENTER);

getContentPane().add(panel2,BorderLayout.SOUTH);

pack();

list.addListSelectionListener(new ListSelectionListener(){

public void valueChanged(ListSelectionEvent lse){

if(lse.getValueIsAdjusting() == false)

{

Bank bank = (Bank)list.getSelectedValue();

label1.setText(bank.code);

label2.setText(bank.name);

}}});

}

public static void main(String[] args){new Testing().setVisible(true);}

}

class Bank

{

String code;

String name;

public Bank(String c, String n){code = c; name = n;}

public String toString(){return code;}

}

Michael_Dunna at 2007-7-13 1:56:24 > top of Java-index,Desktop,Core GUI APIs...
# 2
You sure made it simple!Thanks!Matt
MattCaa at 2007-7-13 1:56:24 > top of Java-index,Desktop,Core GUI APIs...