• Post Reply Bookmark Topic Watch Topic
  • New Topic

ComboBox and getSelectedItem !! URGENT

 
vinaya kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
here is a program which add new element to
a1
a2
a3
list will show a1 a2 a3
now add a4 ,don't press enter
change the element to a3.
Now i have a problem,
when i change this to a3, i want to get the value
that was there previously that is a4...
how to get this Value... Any pointers to this
thanks and regards
vinaya

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Main extends JFrame
{
public Main() {
getContentPane().setLayout(new BorderLayout());
final JComboBox combobox = new JComboBox();
final JList list = new JList(new DefaultListModel());

getContentPane().add(BorderLayout.NORTH, combobox);
getContentPane().add(BorderLayout.CENTER, list);

combobox.setEditable(true);
combobox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.DESELECTED) {

System.out.println("DESELECT"+combobox.getSelectedItem());

}
if (ie.getStateChange() == ItemEvent.SELECTED) {
System.out.println("SELECT"+combobox.getSelectedItem());
((DefaultListModel) list.getModel()).addElement(combobox.getSelectedItem());
combobox.insertItemAt(combobox.getSelectedItem(), 0);
}
}
});

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});

setSize(new Dimension(200, 200));
}

public static void main(String[] args) throws Exception {
Main main = new Main();
main.setVisible(true);
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!