Win a copy of Penetration Testing Basics this week in the Security forum!
  • 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
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

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.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.DESELECTED) {


if (ie.getStateChange() == ItemEvent.SELECTED) {
((DefaultListModel) list.getModel()).addElement(combobox.getSelectedItem());
combobox.insertItemAt(combobox.getSelectedItem(), 0);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {

setSize(new Dimension(200, 200));

public static void main(String[] args) throws Exception {
Main main = new Main();
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic