• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how to get information from JCombo box: This class does the GUI

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

public class PersonListGUI extends JFrame implements ItemListener
{
// instance variables
private String name[]={"Fred", "Bill", "Ethel"};
private JComboBox nameList = null;
private JFrame frame = null;
//private DescriptionPanel dp = new DescriptionPanel();
/**
* Constructor for objects of class PersonListGUI
*/
public PersonListGUI()
{
frame = new JFrame();
frame.setTitle("A List of Salespeople");
nameList = new JComboBox(name);
frame.getContentPane().add(nameList);
frame.setSize(300,70);
frame.setVisible(true);
System.out.println("hello");
}
public void actionPerformed(){
System.out.println("action performed");
}
public void itemStateChanged(ItemEvent e){
if (e.getSource() instanceof JComboBox){
String s = (String)e.getItem();
int k = nameList.getSelectedIndex();
System.out.println("k = " + k);
}
}
public int indexReturner(){
int k = nameList.getSelectedIndex();
return k;
}
}

It works fine. The box is displayed and I can change the display. This class is meant to make an instance of the box and get the returned data.

import javax.swing.*;
import java.awt.event.*;
public class Tester
{

public void Tester()
{
PersonListGUI pl = new PersonListGUI();
int k = pl.indexReturner();
System.out.println (k);
return ;
}
}


It works fine on the first pass. I get the index after the GUI is created (0), but how do I get information after each change to the selected item. It must have something to do with the listener that I am not implementing.

Thanks
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You've got this working. But you aren't doing anything outside of the if's scope with the selectedIndex other than printing it out.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe there methods getSelectedItem and getSelectedIndex that you can call on a JComboBox
 
reply
    Bookmark Topic Watch Topic
  • New Topic