JCombobox Listener is DEAF

Hi Forum
I am having a problem with my JCombobox Listener, It is not responding.
I have a Actionperformed method listening for two things
1 --JButton
2 --JCombobox
JCombox listen should work when I click "START RACE" button but it does not
Here is the code embeded in my friends server, pls be carefull, and working code will be appreciated
http://www.bespokesoft.com/cv/myRace.java
(**Note This code compile and runs, just the JCOMBOBOX listener to work) assignment due 14.30 uk time
Please help me, code will be appreciated!

make this your actionPerformed()
public void actionPerformed(ActionEvent evt)
  if(evt.getSource()==chooseBox) mule = (String)chooseBox.getSelectedItem();        
  else
    if (mule == null)
      messageField.setText("Please choose a horse");       
    else if (mule.equals("Choose a Horse"))
      messageField.setText("Please please choose a horse");
    else
      makeRace.runRace();
      makeRace.setResult(mule);
      potField.setText(" "+makeRace.getPot() );
      bankField.setText(" "+makeRace.getBank() );
      messageField.setText("Winner is horse number   "+makeRace.getHorseNumber()+" : "+makeRace.getHorse() );
}

Similar Messages

  • JComboBox listener problem

    Hi all,
    I have following problem, i use combobox and i need to write listener for selecting item.
    But both ActionListener and ItemListener are unusable for me, because i dont know how to differ between selecting item when combobox is poped up.
    I dont want to react on going thru items in popup, but only to FINAL select of button.
    Please Help.
    Mathew, HSIGP

         This works on non editable combo boxes
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class ComboBoxAction extends JFrame implements ActionListener
         JComboBox comboBox;
         public ComboBoxAction()
              comboBox = new JComboBox();
              comboBox.addActionListener( this );
              comboBox.addItem( "Item 1" );
              comboBox.addItem( "Item 2" );
              comboBox.addItem( "Item 3" );
              comboBox.addItem( "Item 4" );
              //  This prevents action events from being fired when the
              //  up/down arrow keys are used on the dropdown menu
              comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
              getContentPane().add( comboBox );
         public void actionPerformed(ActionEvent e)
              System.out.println( comboBox.getSelectedItem() );
              //  make sure popup is closed when 'isTableCellEditor' is used
              comboBox.hidePopup();
         public static void main(String[] args)
              final ComboBoxAction frame = new ComboBoxAction();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setVisible( true );
    }

  • JComboBox listener question

    Hi,
    I was wondering what kind of listener I would use to detect enter being pressed in a JComboBox.
    I tried an actionListener but this sends actionEvents at far too many times, I want to limit what I'm doing to only when the user presses enter in the JComboBox.
    It seems rather odd that a keyListener can't be used with a JComboBox, or at least according to the API specifications...
    All help is very appreciated!!

    The JComboBox itself just has the text field and then the down arrow, correct? Like if I am writing in that text field I am writing in the JComboBox? Basically all i need is a way to do something when the user presses 'enter', but not when all the other various things that trigger ordinary action events are fired. Using a keyListener to find out whether the key pressed was 'enter' seemed like the best way to do this, but if key events aren't even getting sent when the focus is in the text portion of the JComboBox that may not be the case. If there's anyway i can explain the problem better I'd be happy to do so.
    Thanks

  • JComboBox listener - which do i need?

    Hi,
    What listener do I need, so that I know when a new item is selected in the combo box? I only want to know when the item is selected. I know there is the itemListener but it fires an event for each item in the combo box until it gets the item that is being selected. I only want to be notified once.
    thanks

              String[] model = new String[] { "one", "two", "three" };
              final JComboBox comBox = new JComboBox( model );
              comBox.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        System.out.println( comBox.getSelectedItem() );
              });

  • Jcombobox listener

    I would like to set a listener on a jcombobox (either actionlistener or itemlistener). However, at the same time, I want to know that the combobox item was changed via the user's mouse or keyboard input, and not triggered via the application loading data from the database and changing the combo box. I know I can add the listener after the data has been loaded onto the page, but I was hoping that Swing would provide a mechanism to know what input method was used to change the combo box. Anybody have any ideas?
    It would be greatly appreciated. Thanks!

    That's certainly one of the work-arounds I had in mind, but the overall gui component that holds the JComboBox implements an interface that is inherited by several objects. In order to set a marker variable, I would have to implement the method in the super-class which does nothing for the other sub-classes and override it in the sub-class I'm interested in. Definitely do-able. But, I was hoping some Swing guru could tell me if I could get my hands on how the change item event was triggered. Sounds like it is not possible.
    Thank you.

  • Handle Mouse listener in JComboBox

    Hi there,
    How can I handle MouseListener in a JComboBox?
    I used addMouseListener to handle JComboBox as in any other Swing and AWT components. But the method seems like doesn't work with JComboBox.
    eg:
    public class AllMouseListener extends MouseAdapter implements MouseMotionListener{
    ......//includes all methods to handle mouseClicked, mousePressed, mouseDragged,...
    JComponent comp = null;
    if(.........){
    comp = new JComboBox( new String[] {"JComboBox","Add items..."} );
    comp.addMouseListener(this);
    comp.addMouseMotionListener(this);
    }//end class
    Can any one helps me on this? why doesn't it work? doesn't JComboBox listen to mouse event???
    Thanks and appreciate for any reply!
    Regards,

    hi , i also had this problem but i did one thing for this, then problem has solved :
    you have to definne UI for combo box like this:
    myCombo.setUI( new MuComboUI() );
    and in UI write this :
    class ComboUI extends BasicComboBoxUI {
    //..... define some UI here
    then after setting UI u can add myCombo.addMouseListener( new MyMouseListener() );
    then u can get mouse event i hope :)

  • Trying to add a listener to a JComboBox in a JTable

    Hi all.
    I have a little problem which I can't resolve.
    I made a program in which I create a table using a class which extends JTable (but this isn't the point with the problem, I think. It's only for rendering purposes)
    In this table I put various types of items (such as Strings, JButton, JComboBox). I have no problem with the visualization of them, but the problem is with the listener for the combo boxes. I created a listener of my own and registered it in the constructor of the combo box editor, but when I change a value it's never fired. Can someone please help me?
    Here's some code which can help understanding the problem
    Combo box editor:
    public class MyComboBoxEditor extends DefaultCellEditor{
    public MyComboBoxEditor(JTableX _tabellaDiAppartenenza) {
    super(new JComboBox());                  
    (JComboBox)super.editorComponent).addItemListener(new MyItemListener((JComboBox)super.editorComponent));    
    public Component getTableCellEditorComponent(JTable arg0, Object arg1, boolean arg2, int arg3, int arg4) {
    String elementi[]=new String[((JComboBox)arg1).getItemCount()];
    for(int i=0;i<((JComboBox)arg1).getItemCount();i++){
    elementi=(String)((JComboBox)arg1).getItemAt(i);
    super.editorComponent=new JComboBox(elementi);
    return super.getTableCellEditorComponent(arg0, arg1, arg2, arg3, arg4);
    }combo box listener:public class MyItemListener implements ItemListener{
    private JComboBox combo;
    public MyItemListener(JComboBox object){
    combo=object;
    public void itemStateChanged(ItemEvent arg0) {
    int changeEvent = arg0.getStateChange();
    if(changeEvent==ItemEvent.SELECTED){
    Object value=arg0.getItem();
    combo.setSelectedItem(value);
    }table:public class JTableX extends JTable{
    protected MyCellEditorModel myCellEditorModel;
    public JTableX(MyDataModel tm){
    super(tm);
    Object ge = defaultEditorsByColumnClass.get(Boolean.class);
    DefaultCellEditor dce = (DefaultCellEditor)ge;
    dce.setClickCountToStart(0);
    MyButtonEditor mbe = new MyButtonEditor(new JCheckBox("Bottone"), this);
    mbe.setClickCountToStart(0);
    MyComboBoxEditor mcbe = new MyComboBoxEditor(this);
    mcbe.setClickCountToStart(0);
    defaultEditorsByColumnClass.clear();
    Hashtable cellEditors = new Hashtable();
    cellEditors.put(JButton.class, mbe);
    cellEditors.put(JComboBox.class, mcbe);
    cellEditors.put(Boolean.class, dce);
    this.defaultRenderersByColumnClass.put(JButton.class, new MyButtonRenderer());
    this.defaultRenderersByColumnClass.put(JComboBox.class, new MyComboBoxRenderer());
    defaultEditorsByColumnClass.put(Object.class, new MyCellEditorModel1(cellEditors));
    public TableCellEditor getCellEditor(int row, int col){
    if(myCellEditorModel!=null) return myCellEditorModel;
    return super.getCellEditor(row,col);
    public TableCellRenderer getCellRenderer(int row, int column) {
    Object value = getValueAt(row,column);
    if (value !=null) {
    return getDefaultRenderer(value.getClass());
    return super.getCellRenderer(row,column);
    public void setCellEditor(MyCellEditorModel anEditor) {
    myCellEditorModel = anEditor;
    }Hoping the code is enough to understand the problem, I thanks all for watching and reading the post and for the future answeres.
    Bye all.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    super.editorComponent=new JComboBox(elementi);You create a new combo box each call to getTableCellEditComponent() (overriding the one of the constructor), but you aren't adding a ItemListener to it.

  • JComboBox - how to get popup or add listener

    Hi,
    Using a JComboBox I want to add a mouse listener so I can detect when a user press mouse on it. It seems that the popup may be getting the mouse event though. Is there a way to add a mouse listener to it so I can catch mousepressed event? perhaps a way to get the actual ComboPopup??
    ...hopefully it can be done without subclassing the combobox UI, or popup, or jcombobox itself.
    thanks

    It occurs to me that what you are probably asking is not 'how do I add a MouseListener
    to the popup menu', but rather, 'how do I add a MouseListener to the ComboBox itself'.
    Camickr suggestion is spot on if your combo box is editable. If it is not, you should add
    it to the renderer. However, I'm not sure if that will still catch mouse events on the arrow
    button which pops the list, and that doesn't seem to be accessible from the UI.
    Hmmm. Probably your best bet is to iterate through the combo box's child components
    and add the listener to each of those.
    : jay

  • JComboBox action listener

    I have registered an action listener on a JComboBox.. I change the selected value of this combobox from another component's actions also.. My question is how can i distinguish between an action event in which an item was selected using the combo box directly and an event in which the selected value in the combo box was set by different comopnent.. the actionEvent.getSource gives the combo box itself..
    Is using a mouselistener the only way out??

    You can't and its a bad design to attempt to do so.dont know about bad design but.. in my case its required. I have to make a file chooser type User interface for browsing an ftp site.. So the combo box needs to be updated when the user changes a directory by double clicking on the directory.. Could i do something else?? please suggest anything u can
    >
    Is using a mouselistener the only way out?? What about if the user uses the key board to select
    the item from the combo box? Would you also need to
    write your own KeyListener? Getting very ugly.good point.. didnt think about this....now i understand why they used action listener
    >
    As a workaround, you could add code to your "other"
    compnent:
    comboBox.removeActionListener(...);
    comboBox.setSelectedItem(...);
    comboBox.addActionListener(...);thanks, does this have a significant performance penalty.. I mean it does not look good does it??

  • JComboBox Lost Listener on Look and Feel change

    When the user change the Look And Feel on the Fly , any Listener that is done using
    myComboBox.getEditor().getEditorComponent().addXXXListener
    is lost
    I'm using Windows XP Service Pack 2, java 6 build 105
    Does someone have any inputs why this happen or how this can be "fixed" or maybe prevent. Do I'm doing something wrong?
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.*;
    import java.text.SimpleDateFormat;
    /* ComboBoxDemo2.java requires no other files. */
    public class ComboBoxDemo2 extends JPanel
                               implements ActionListener {
        static JFrame frame;
        JLabel result;
        String currentPattern;
        public ComboBoxDemo2() {
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
            String[] patternExamples = {
                     "dd MMMMM yyyy",
                     "dd.MM.yy",
                     "MM/dd/yy",
                     "yyyy.MM.dd G 'at' hh:mm:ss z",
                     "EEE, MMM d, ''yy",
                     "h:mm a",
                     "H:mm:ss:SSS",
                     "K:mm a,z",
                     "yyyy.MMMMM.dd GGG hh:mm aaa"
            currentPattern = patternExamples[0];
            //Set up the UI for selecting a pattern.
            JLabel patternLabel1 = new JLabel("Enter the pattern string or");
            JLabel patternLabel2 = new JLabel("select one from the list:");
            JComboBox patternList = new JComboBox(patternExamples);
            patternList.setEditable(true);
            patternList.addActionListener(this);
    //-------------------------------- XXX------------------------------------------
    //      This KeyListener it is lost when the user change the theme on the fly       
            patternList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()
            public void keyPressed(KeyEvent e)
             System.out.println(" Key pressed is "+e.getKeyCode());     
            //Create the UI for displaying result.
            JLabel resultLabel = new JLabel("Current Date/Time",
                                            JLabel.LEADING); //== LEFT
            result = new JLabel(" ");
            result.setForeground(Color.black);
            result.setBorder(BorderFactory.createCompoundBorder(
                 BorderFactory.createLineBorder(Color.black),
                 BorderFactory.createEmptyBorder(5,5,5,5)
            //Lay out everything.
            JPanel patternPanel = new JPanel();
            patternPanel.setLayout(new BoxLayout(patternPanel,
                                   BoxLayout.PAGE_AXIS));
            patternPanel.add(patternLabel1);
            patternPanel.add(patternLabel2);
            patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
            patternPanel.add(patternList);
            JPanel resultPanel = new JPanel(new GridLayout(0, 1));
            resultPanel.add(resultLabel);
            resultPanel.add(result);
            patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            add(patternPanel);
            add(Box.createRigidArea(new Dimension(0, 10)));
            add(resultPanel);
            setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            reformat();
        } //constructor
        public void actionPerformed(ActionEvent e) {
             System.out.println("Action Event");                
            JComboBox cb = (JComboBox)e.getSource();
            String newSelection = (String)cb.getSelectedItem();
            currentPattern = newSelection;
            reformat();
        /** Formats and displays today's date. */
        public void reformat() {
             try {
            Date today = new Date();
            SimpleDateFormat formatter =
               new SimpleDateFormat(currentPattern);
                String dateString = formatter.format(today);
                result.setForeground(Color.black);
                result.setText(dateString);
            }catch (IllegalArgumentException iae) {     
            System.out.println("Ilegal argument Exception");   
            catch (Exception e) {
            System.out.println("Argument Exception");                
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            final JFrame frame = new JFrame("ComboBoxDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Create and set up the content pane.
            JComponent newContentPane = new ComboBoxDemo2();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setLayout(new BorderLayout());      
            //frame.setContentPane(newContentPane,BorderLayout.CENTER);
            JMenuBar menuBar = new JMenuBar();
            JMenu theme = new JMenu("Theme");
            ButtonGroup bttnGroup = new ButtonGroup();
            JCheckBoxMenuItem metal = new JCheckBoxMenuItem("Metal");
            bttnGroup.add(metal);
            metal.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
              try{
              UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
              SwingUtilities.updateComponentTreeUI(frame);
              }catch(Exception a){a.printStackTrace();}
              theme.add(metal);
            JCheckBoxMenuItem system = new JCheckBoxMenuItem("System");
            bttnGroup.add(system);
            system.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
              try{
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              SwingUtilities.updateComponentTreeUI(frame);
              }catch(Exception a){a.printStackTrace();}
            theme.add(system);
            menuBar.add(theme);     
            frame.setJMenuBar(menuBar);
            JToolBar jtb = new JToolBar();
            jtb.add(newContentPane);
            frame.add(jtb, BorderLayout.PAGE_START);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }

    Thanks to both Rodney_McKay and jasper for their replys and ideas
    This code is working fine
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.*;
    import java.text.SimpleDateFormat;
    /* ComboBoxDemo2.java requires no other files. */
    public class ComboBoxDemo2 extends JPanel
                               implements ActionListener {
        static JFrame frame;
        JLabel result;
        String currentPattern;
        private JComboBox patternList = new JComboBox();
        public ComboBoxDemo2() {
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
            String[] patternExamples = {
                     "dd MMMMM yyyy",
                     "dd.MM.yy",
                     "MM/dd/yy",
                     "yyyy.MM.dd G 'at' hh:mm:ss z",
                     "EEE, MMM d, ''yy",
                     "h:mm a",
                     "H:mm:ss:SSS",
                     "K:mm a,z",
                     "yyyy.MMMMM.dd GGG hh:mm aaa"
            currentPattern = patternExamples[0];
            //Set up the UI for selecting a pattern.
            JLabel patternLabel1 = new JLabel("Enter the pattern string or");
            JLabel patternLabel2 = new JLabel("select one from the list:");
            patternList = new JComboBox(patternExamples){
            public void updateUI(){
            System.out.println("UPDATE UI");
            super.updateUI();     
            changeUIAddEditorListener();
            changeUIAddEditorListener();
            patternList.setEditable(true);
            patternList.addActionListener(this);
            //Create the UI for displaying result.
            JLabel resultLabel = new JLabel("Current Date/Time",
                                            JLabel.LEADING); //== LEFT
            result = new JLabel(" ");
            result.setForeground(Color.black);
            result.setBorder(BorderFactory.createCompoundBorder(
                 BorderFactory.createLineBorder(Color.black),
                 BorderFactory.createEmptyBorder(5,5,5,5)
            //Lay out everything.
            JPanel patternPanel = new JPanel();
            patternPanel.setLayout(new BoxLayout(patternPanel,
                                   BoxLayout.PAGE_AXIS));
            patternPanel.add(patternLabel1);
            patternPanel.add(patternLabel2);
            patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
            patternPanel.add(patternList);
            JPanel resultPanel = new JPanel(new GridLayout(0, 1));
            resultPanel.add(resultLabel);
            resultPanel.add(result);
            patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            add(patternPanel);
            add(Box.createRigidArea(new Dimension(0, 10)));
            add(resultPanel);
            setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            reformat();
        } //constructor
        public void actionPerformed(ActionEvent e) {
             System.out.println("Action Event");                
            JComboBox cb = (JComboBox)e.getSource();
            String newSelection = (String)cb.getSelectedItem();
            currentPattern = newSelection;
            reformat();
        /** Formats and displays today's date. */
        public void reformat() {
             try {
            Date today = new Date();
            SimpleDateFormat formatter =
               new SimpleDateFormat(currentPattern);
                String dateString = formatter.format(today);
                result.setForeground(Color.black);
                result.setText(dateString);
            }catch (IllegalArgumentException iae) {     
            System.out.println("Ilegal argument Exception");   
            catch (Exception e) {
            System.out.println("Argument Exception");                
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            final JFrame frame = new JFrame("ComboBoxDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Create and set up the content pane.
            JComponent newContentPane = new ComboBoxDemo2();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setLayout(new BorderLayout());      
            //frame.setContentPane(newContentPane,BorderLayout.CENTER);
            JMenuBar menuBar = new JMenuBar();
            JMenu theme = new JMenu("Theme");
            ButtonGroup bttnGroup = new ButtonGroup();
            JCheckBoxMenuItem metal = new JCheckBoxMenuItem("Metal");
            bttnGroup.add(metal);
            metal.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
              try{
              UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
              SwingUtilities.updateComponentTreeUI(frame);
              }catch(Exception a){a.printStackTrace();}
              theme.add(metal);
            JCheckBoxMenuItem system = new JCheckBoxMenuItem("System");
            bttnGroup.add(system);
            system.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
              try{
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              SwingUtilities.updateComponentTreeUI(frame);
              }catch(Exception a){a.printStackTrace();}
            theme.add(system);
            menuBar.add(theme);     
            frame.setJMenuBar(menuBar);
            JToolBar jtb = new JToolBar();
            jtb.add(newContentPane);
            frame.add(jtb, BorderLayout.PAGE_START);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
       public void changeUIAddEditorListener() {
       patternList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()
       public void keyPressed(KeyEvent e)
       System.out.println(" Key pressed is "+e.getKeyCode());     
    }

  • JComboBox keyboard listener

    I have an editable JComboBox component:
    JComboBox combo = new JComboBox();
    combo.setEditable(true);
    How to attach a listener to every character from keyboard?
    I have tried:
    combo.addKeyListener(new KeyAdapter() {
    but it doesn't seems to work!
    I'm not sure which listener to attach to the combobox...
    Tomecks

    I have found the solution of that problem:
    http://forum.java.sun.com/thread.jsp?thread=118366&forum=257&message=310063
    Tomecks

  • Notify JComboBox action listener ?

    Hi,
    i would like to notify the action listener of a JComboBox by myself (like doing a doClick for a JButton). What is the method that i could use for it ?

    JComboBox cbo1;
    cbo1.addItemListener(this);
    or you could use
    cbo1.addActionListener(this);
    for the first one have
    public void itemStateChanged(ItemEvent ie)
    if(ie.getSource()==cbo1)
    your code
    extend interface ItemListener
    for second one have
    public void actionPerformed(ActionEvent ae)
    if(ie.getSource()==cbo1)
    your code
    extend interface ActionListener
    Is this what u need ?

  • JComboBox property listener

    Hello, good evening. Iam trying to figure out how to add a property listener to a JComboBox. My purpose is, when I add something to the JComboBox it'll be visible, otherwise it hide. I tried one of the following code:
    cb_sedes.addPropertyChangeListener("getItemCount()",new Cbsedes_handler());
    Where cb_sedes is a JComboBox, "getItemCount()" is the property Iam interest in and Cbsedes_handler is just a class that handles the propertyListener.
    Any suggestion?
    Thank you.
    clopher.

    If I can remember my JavaBeans stuff properly you need to specify the name of the property not the method.
    So for getting events relating to changes in font you would provide a property of "font". For background colour changes "background" and so on...
    Are you sure this is what you want? Hiding items is not really dealt with by property changes...

  • JComboBox QUESTION - Listener Methods.

    Dear Everyone,
    At the moment I am working on a UI that utilises four JComboboxes. Each of the four JComboboxes contains an index of 10 numeric values, namely 0 - 9. I intend on using the concatenated values of all four boxes to represent a four digit year value, e.g.: 1583, 1902, 2050, 9504, etc.
    I need to write some code that listens to each of the four JComboboxes and places the user selected number (from the JComboboxes index) into a variable.
    Once the values of all four JComboboxes are held in four variables I then need to concatenate the variables so that they represent a single year, e.g.: 1583.
    Once I have the concatenated value I will then perform relative mathematical operations.
    If anybody has any suggestions as to how I can listen to the JComboboxes and then place the selected index values into variables they will be greatly appreciated.
    Thanks

    How about this:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class Jconbo extends JFrame
         Vector v       = new Vector();
         JComboBox jc1  = new     JComboBox(v);
         JComboBox jc2  = new     JComboBox(v);
         JComboBox jc3  = new     JComboBox(v);
         JComboBox jc4  = new     JComboBox(v);
         int       year = 0;
    public Jconbo()
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         for (int j=0; j < 10; j++) v.add(""+j);     
           setBounds(1,1,500,300);
         addJc(jc1);
         addJc(jc2);
         addJc(jc3);
         addJc(jc4);
         getContentPane().setLayout(new FlowLayout());
         setVisible(true);
    private void addJc(final JComboBox jc)
         getContentPane().add(jc);
         jc.setPreferredSize(new Dimension(100,24));
         jc.setSelectedIndex(0);
         jc.addItemListener( new ItemListener()
         {     public void itemStateChanged(ItemEvent e)
                   year = jc1.getSelectedIndex() * 1000 +
                           jc2.getSelectedIndex() * 100 +
                           jc3.getSelectedIndex() * 10 +
                           jc4.getSelectedIndex();           
                   System.out.println(""+year);
    public static void main (String[] args) 
         new Jconbo();
    Noah
    unformatted
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class Jconbo extends JFrame
         Vector v = new Vector();
         JComboBox jc1 = new     JComboBox(v);
         JComboBox jc2 = new     JComboBox(v);
         JComboBox jc3 = new     JComboBox(v);
         JComboBox jc4 = new     JComboBox(v);
         int year = 0;
    public Jconbo()
         addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         for (int j=0; j < 10; j++) v.add(""+j);     
         setBounds(1,1,500,300);
         addJc(jc1);
         addJc(jc2);
         addJc(jc3);
         addJc(jc4);
         getContentPane().setLayout(new FlowLayout());
         setVisible(true);
    private void addJc(final JComboBox jc)
         getContentPane().add(jc);
         jc.setPreferredSize(new Dimension(100,24));
         jc.setSelectedIndex(0);
         jc.addItemListener( new ItemListener()
         {     public void itemStateChanged(ItemEvent e)
                   year = jc1.getSelectedIndex() * 1000 +
                        jc2.getSelectedIndex() * 100 +
                        jc3.getSelectedIndex() * 10 +
                        jc4.getSelectedIndex();           
                   System.out.println(""+year);
    public static void main (String[] args)
         new Jconbo();
    }

  • How do I listen to a JComboBox selection?

    Hi
    What listener is to be added in order to get an action only when an item is selected?? The ActionListener and ItemListener dont seem to behave rationally. I would like to know if there's a way to catch an action when an item is selected from the combo box.
    Thanx in advance.
    regards,
    Giridhar

    Action listener should fo the trick, in what way is it not behaving rationally?
    Graeme

Maybe you are looking for