JComboBox events as TableCellEditor

I'm using a subclass of JComboBox as a TableCellEditor, and it isn't firing the same events as it does outside the table. For instance, when the combobox has editable focus, I can use the arrow keys to move the selection up and down the menu, and outside the Table, everytime I move the selection in this way, it sets the appropriate SelectedItem and SelectedIndex properties of the ComboBox to match my selection. But in the table, it doesn't set these properties just by chaning the selection in this way. It also doesn't fire an ItemChangeEvent like it does outside the table.
Anyone know why this is happening and how I can work around it?
Thanks.

well, the keyinputhandler was a class in JClass's table extension!
there was a traverse() method which we had to stick a requestFocus() in to make sure that all the right listeners were invoked from a 'move' event....
not entirely sure where you'd stick it in normal swing JTables....!
(apols!)

Similar Messages

  • JComboBox event notification when select first entry in pop-up menu JDK 6

    I recently began testing a Java GUI, I originally developed with JDK 1.5.0_11, with JDK 1.6.0_3. I have used the JComboBox in several applications developed using JDK 1.3.x and 1.4.x over the years. In every one of these earlier JDKs the JComboBox widgets all behaved the same. When you select the JComboBox widget, the pop-up menu appears and you can select any of the items from the menu list. Having made the selection with either a mouse click or a key press, an event notification is sent. Typically, an ActionEvent as well as either a mouse click event or a keypressed event may be sent. When testing with 1.6.0_x versions of the JDK and JRE, I can't account for any event notification being sent when selecting the first item at the top of the pop-up menu as the initial selection. If I select some other item on the list and then the first item, it works as it is supposed to work, but only after selecting another item first.
    I've placed the JComboBox in a JDialog and a JPanel. There are NO AWT widgets in any containers of the application. The same behavior seems to exist regardless of what other containing Swing component I place the JComboBox in. I'm running these applications on Windows XP, service pack 2. The system is an AMD 64 bit dual processor with 4Gb of RAM. The essential code follows:
    private JComboBox getJcboAlias()
        /* Note here that I am using a defaultComboModel as I have always done */
       jcboAliases = new JComboBox(urls);
       *jcboAliases.setEditable(false);*                               // Note here that the JComboBox is NOT editable
       jcboAliases.addActionListener(new ActionListener()   // ActionListener only receives a notification if the second or
       {                                                                            // another item in the pop-menu list is selected...but never the
          public void actionPerformed(ActionEvent ae)           // first item in the list
             String s = (String) jcboAliases.getSelectedItem();
             for (int i = 0; i < connections.size(); i++)
                conAlias = (String[]) connections.get(i);
                if (s.equals(conAlias))
    isSelected = true;
    break;
    jlblName.setVisible(true);
    jlblAlias.setVisible(true);
    jlblUID.setVisible(true);
    jlblPWD.setVisible(true);
    jtxtName.setText(conAlias[0]);
    jtxtName.setVisible(true);
    jtxtAddress.setText(conAlias[1]);
    jtxtAddress.setVisible(true);
    jtxtUID.setText(conAlias[2]);
    jtxtUID.setVisible(true);
    jtxtPWD.setVisible(true);
    jtxtPWD.setText("");
    jtxtPWD.requestFocus();
    return jcboAliases;
    }    I'm using a non-editable JComboBox because there is a pop-up menu with this and not with the JList widget.  JComboBox behaves more like the JList MicroSoft counterpart.  Another code snippet follows in which the JComboBox is editable.  The same errant behavior occurs when selecting the first item from the pop-up menu: private JComboBox getJcboPCMember()
    jcboPCMember = new JComboBox(PCViewerCustom.getCurrentMembers());
    jcboPCMember.setBounds(250, 103, 180, 20);
    jcboPCMember.setToolTipText("PATHCHECK(ER) member name - Example: CKPPTHCK");
    jcboPCMember.setSelectedIndex(Integer.valueOf(PCViewerCustom.getCurrentHost(3)));
    jcboPCMember.setEditable(true); // Note here that this JComboBox IS editable
    jcboPCMember.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent ae)
    if (ae.getActionCommand().equals("comboBoxEdited"))
    boolean copy = false;
    for (int i = 0; i < jcboPCMember.getItemCount(); i++)
    if (jcboPCMember.getSelectedItem().equals(jcboPCMember.getItemAt(i)))
    copy = true;
    break;
    if (!copy)
    if (jcboPCMember.getItemCount() > 0)
    if (jcboPCMember.getItemAt(0).equals("No Entries"))
    jcboPCMember.removeItem("");
    jcboPCMember.removeItem("No Entries");
    jcboPCMember.insertItemAt((String) jcboPCMember.getSelectedItem(), 0);
    else
    jcboPCMember.removeItem("");
    jcboPCMember.addItem((String) jcboPCMember.getSelectedItem());
    enableJbtnOK();
    else
    if (jcboPCMember.getSelectedIndex() >= 0)
    PCViewerCustom.setCurrentHost(3, Integer.toString(jcboPCMember.getSelectedIndex()));
    enableJbtnOK();
    else
    JOptionPane.showMessageDialog(null, "Name not selected", "Error", JOptionPane.ERROR_MESSAGE);
    return jcboPCMember;
         I am able to add a new entry to the JComboBox, however, I am still unable to select the first item from the pop-up menu and fire any kind of notification event.  I have not seen this behavior before in any earlier versions of the JDK and it's playing havoc with my ability to deploy the application into a JDK or JRE V6 environment, without adding a bunch of additional code to pre-select the first item on the list, which pretty much defeats the purpose of the JComboBox.  I'll be the first to admit I've done something wrong, but i've built a number of test scenarios now on two systems runing Win XP SP2 and I get the same errant behavior.  I've also added in event listeners for a MouseListerner, a KeyListener and an ItemListener.  Still no event notification on this first item in the list.  Again, however, if I select one of the other items from the pop-up menu list and then select the first item, all is well.  Imagine selling that method of operation to a user....  It occurs to me that this must be a bug in the V6 JComboBox.  I wanted to post this here first, however, in order to determine if this is, in fact, a bug - in other words, am I the only one seeing this, or is this a more widespread issue?  Any assistance in making this determination is greatly appreciated.
    David Baker
    Edited by: Galstuk on Nov 27, 2007 12:21 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    here is the other file as well:
    Harold Timmis
    [email protected]
    Orlando,Fl
    *Kudos always welcome
    Attachments:
    VI2.vi ‏5 KB

  • JComboBox event confusion: not enough generated? :-(

    Hi,
    I am slightly confused about the events generated by the combobox.
    I have a combobox and and actionlistener registered on it. If I programmatically use setSelectedIndex on the combobox, and event is generated and the actionlistener is triggered. If I do a setSelectedIndex on the same combobox within the code of the action listener, no event is generated, though the index does get updated. Though I am happy it doesn't generate that event, I don't understand why it doesn't. Anyone?
    Rene'

    Instead of using ActionListener you should use ItemListener for combo box. This is an example
    comboBox.addItemListener(new java.awt.event.ItemListener() {
         public void itemStateChanged(ItemEvent e) {
         try {
         JComboBox box = (JComboBox) e.getSource();
         System.out.println(box.getSelectedItem());
         } catch (ArrayIndexOutOfBoundsException exp) {}
    });Note: Item Listener only listens when you change something in Combo Box. If you click on a combo box, and selects the same value, item listeners doesn't call.
    Hope this helps.

  • JComboBox Event? How do I know when a JComboBox has been clicked?

    What event is fired when a JComboBox is pressed?
    I can tell when an item has been selected but I want
    to know when the combobox was clicked originally.

    Two (untested) ways of doing this:
    1. Subclass JCombobox and fire an event when the box is opened. To find out when its opened, you'll probably have to look at the source for JCombobox and override the method.
    2. use the model as a clue. Chances are the underlying combo box model will have its getValueAt() method called when the user opens the drop down box. Cant be sure that thats the only time itll happen tho,,
    DS

  • JComboBox event problem

    Hi ,
    I have JComboBox in my application. I added action listener for this. When I set the values programmatically I don't want the code in the actionperformed to be executed. I can do it indirectly by using boolean flag for this, but I want to know is there any other (neat) way we can achieve this.
    Thanks

    You can always check to see if there is a selected index...
    myComboBox.addActionListener
         new ActionListener()
              public void actionPerformed(ActionEvent e)
                   if (((JComboBox)e.getSource()).getSelectedIndex() != -1)
                        //perform your operations here.
    );Another way to do it would be to write your own custom action listener and consume() any event that you don't want.
    Hope that helps.

  • JComboBox Event Handling

    Editable JComboBox (combo1.setEditable(true)) is not adhering to KeyEvents such as keyTyped() and keyReleased() events.
    How can I make a "Editable" JComboBox to adhere KeyEvents.

    a comboBox is made up of many components.
    when editable there is a JTextField
    you need to get the textfield to add the keylistener
    combo.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()
    other times you may need to cast it
    ((JTextField)combo.getEditor().getEditorComponent()).someMethod()

  • JComboBox events

    Is it possible to have the event handling for a JComboBox in another class?
    I'm using MVC architecture and so would like my View's JCBs to be handled by the Controller, but I can't get the Controller to recognise different JCBs with getSource()....
    Is there something like
    if (e.theView.getSource()==combo1)
    do this;
    else if (e.theView.getSource()==combo2)
    do this;
    }

    OOps. Realized what I was doing wrong.

  • JComboBox event ItemChanged is called repeatedly

    Hi all,
    I've got an JApplet with 2 JComboBoxes. The first both of them are added to ItemListener.
    When a entry of the first ComboBox is selected I delete
    the contents of the second one and add other contents.
    (I have to do so)
    The problem :
    The itemStateChanged event occurs of Combo2 occurs
    after itemStateChanged of Combo1 and vice versa multiple times.
    How can I avoid this. When I select an item from combo1
    only this event should occure. When I select an item from combo2 only this event should occure.
    public void itemStateChanged(ItemEvent ie) {
          String sName = (String)ie.getItem();
          if(ie.getSource() == ((CQdbl_Beanstandung)m_appletParent).jchoBereich) {
             if(ie.getStateChange() == ItemEvent.SELECTED) {
                //department BML-V1  and BML-V2 have the same masks
                if(sName.compareTo("BML-V2") == 0) {
                   ((CQdbl_Beanstandung)m_appletParent).setBmlv2();
                } else  if(sName.compareTo("BML-V1/WE") == 0) {
                   ((CQdbl_Beanstandung)m_appletParent).setBmlv1we();
                } else  if(sName.compareTo("BML-V1/WA") == 0) {
                   ((CQdbl_Beanstandung)m_appletParent).setBmlv1wa();
                //get new form contents
                ((CQdbl_Beanstandung)m_appletParent).refreshForm();
          } else if(ie.getSource() == ((CQdbl_Beanstandung)m_appletParent).jchoVerursacher) {
             if(ie.getStateChange() == ItemEvent.SELECTED) {
                ((CQdbl_Beanstandung)m_appletParent).setKst();

    make a variable itemStateThatOughtToBe
    in your ItemListenrer first ask if the current itemstate is istemStateThatOughtToBe. if return else execute your normal code.
    If you don't want the itemlistener to become active with this particualr change first set ItemStateTahtOughttoBe and then change the item

  • JComboBox Event help

    sir
    i have three JComboBoxes .
    firstcombobox will be loaded on start up .
    basing on the First combobox item selected the second combo box is filled up.
    basing on the second the third one will be filled up.
    based on the third combobox the text feild beside it should be locked or unlocked
    i have the problem with the comboboxes events in which events i have to write all the three please help me

    in the following link there is a code example it will help u and u have to make combobox model to all stuff
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=688505

  • JCombobox event

    Hi everybody,
    I JComboBox component on my JFrame and i set it as editable.
    Well, i want that when i type a character from my keyboard the list of comboBox has to be automatically fill of some data.
    How can i get manage the the event from my keyboard?
    Cheers.
    Stefano

    An ActionEvent is fired when you press Enter in a JComboBox.

  • Multiple JComboBox Event Handling Problem

    Hi, guys
    I am a newbie in Java. I need to create three JComboBox, say date, month and year and a JButton "submit". After I select the entries from the three JComboBox, I click the "search" button to display something in terms of the information I selected, but I have no idea how to write the actionPerformed( ActionEvent evt) to deal with the problem. Can anyone give me some hint?
    Any help would be appreciated.

    If you are asking how to write a basic event handler for when your button is pressed, RTM.
    http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

  • Remove events from TableCellEditor

    Hi,
    I created a table with some columns. I am using InputField as TableCellEditor. On execution of application, when I click on any of the inputfields (cell), an event is triggered.
    Can anybody suggest the way to remove that event?
    Thanks and Regards,
    Vaibhav

    Vaibhav,
    On Table UI control set compatabilityMode=NW04s
    If you can't see such property then you probably use old NW SP
    Valery Silaev
    SaM Solutions
    http://www.sam-solutions.net

  • JComboBox - Event Name...

    Hello.
    I'd like to know Which event I must Use to detect the change of selected item...
    (I'd like to change Label, according the selected item in the Combo)
    Thank you.

    You can use any action event handler. I have made one such handler whose code is as follows:
    private void combobox_selected(java.awt.event.ActionEvent evt) {                                  
    // TODO add your handling code here:
            int a = jComboBox1.getSelectedIndex();
            if(a==0)
            jLabel1.setText("Item 1");
            else if(a==1)
            jLabel1.setText("Item 2");
            else if(a==2)
            jLabel1.setText("Item 3");
            else
            jLabel1.setText("Item 4");
        } 

  • How to separate events fired from multiple JComboBox instances

    Hi everybody,
    I have built a form with 2 JComboBox instances: each for separated options.
    String[] items = {"Red", "Blue"};
            JComboBox comboBox1 = new JComboBox(items); // For water color
            JComboBox comboBox2 = new JComboBox(items); // For mountain color
            comboBox1.addActionListener(aListener);
            comboBox2.addActionListener(aListener);
            contentArea.add(comboBox1);
            contentArea.add(comboBox2);How can I distinguish which event is fired from comboBox1 and which event comes from comboBox2. I tried to implement actionPerformed method as follows:
    public void actionPerformed(ActionEvent event) {
            Object eventSource = event.getSource();
            if (eventSource instanceof JComboBox) {
                JComboBox cb = (JComboBox)event.getSource();           
                System.out.println("From JCom "+cb);
                if (cb == comboBox1) {
                    System.out.println("From comboBox1");
        }but it does not work. Java Compiler said: cannot find symbol comboBox1.
    How can I resolve this problem?
    Thanks in advance

    Move the declarations for the comboboxes into class scope so they can be seen from within the "actionPerformed" method.
    class SomeClass
        // instance variable declarations
        // these variables have class scope
        JComboBox comboBox1;
        JComboBox comboBox2;
        void aMethod()
            String[] items = {"Red", "Blue"};
            comboBox1 = new JComboBox(items); // For water color
            comboBox2 = new JComboBox(items); // For mountain color
        }Or you could use the "getActionCommand" and "setActionCommand" (JComboBox) methods to send along a string with the event.
    Another option is to use the Component methods "setName" and "getName" to identify the sender of the event. Or you could use the JComponent "putClientProperty" and "getClientProperty" methods.

  • AutoComplete JComboBox As JTable cell editor

    Hello, when I try to use AutoComplete JComboBox as my JTable cell editor, I facing the following problem
    1) Exception thrown when show pop up. - Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    2) Unable to capture enter key event.
    Here is my complete working code. With the same JComboBox class, I face no problem in adding it at JFrame. But when using it as JTable cell editor, I will have the mentioned problem.
    Any advice? Thanks
    import javax.swing.*;
    import javax.swing.JTable.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    * @author  yccheok
    public class NewJFrame extends javax.swing.JFrame {
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
                    /* Combo Box Added In JFrame. Work as expected. */
                    final JComboBox comboBox = new JComboBox();
                    comboBox.addItem("Snowboarding");
                    comboBox.addItem("Rowing");
                    comboBox.addItem("Chasing toddlers");   
                    comboBox.setEditable(true);
                    comboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
                       public void keyReleased(KeyEvent e) {
                           if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                               System.out.println("is enter");
                               return;
                           System.out.println("typed");
                           comboBox.setSelectedIndex(0);
                           comboBox.showPopup();
                    getContentPane().add(comboBox, java.awt.BorderLayout.SOUTH);
        public JTable getMyTable() {
            return new JTable() {
                 Combo Box Added In JTable as cell editor. Didn't work as expected:
                 1. Exception thrown when show pop up.
                 2. Unable to capture enter key event.
                public TableCellEditor getCellEditor(int row, int column) {
                    final JComboBox comboBox = new JComboBox();
                    comboBox.addItem("Snowboarding");
                    comboBox.addItem("Rowing");
                    comboBox.addItem("Chasing toddlers");   
                    comboBox.setEditable(true);
                    comboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
                       public void keyReleased(KeyEvent e) {
                           if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                               System.out.println("is enter");
                               return;
                           System.out.println("typed");
                           comboBox.setSelectedIndex(0);
                           comboBox.showPopup();
                    return new DefaultCellEditor(comboBox);
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
        private void initComponents() {
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = getMyTable();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
            jScrollPane1.setViewportView(jTable1);
            getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
            pack();
        }// </editor-fold>                       
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
        // Variables declaration - do not modify                    
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration                  
    }

    You need to create a custom CellEditor which will prevent these problems from occurring. The explanation behind the problem and source code for the new editor can be found at Thomas Bierhance's site http://www.orbital-computer.de/JComboBox/. The description of the problem and the workaround are at the bottom of the page.

Maybe you are looking for