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

Similar Messages

  • 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() );
    }

  • JComboBox simple question

    I think is a very simple question, I ve got a JComBox with an array of Strings. Something like this:
    JPanel myPanel;
    JComBox box;
    String levels[]={"Level 1","Level 2","Level 3","Level 4","Level 5"};
    box = new JComboBox (levels);
    myPanel.add(box);
    Ok. My question is: How can I let just the level 1 enable at first, or how can I enable or disable just one level of the JCombox, like level 1 and 2 enable but level 3, 4 and 5 disable.
    Thanks a lot!! Hope someone helps me!!

    I don't think this is possible with a JComboBox. I believe it is with a JMenu and JMenuItems.
    You could only display valid options by adding/deleting them. Or you could handle it in the listener code. ie if they select an invalid option do nothing. This would require you keeping track of which options are valid elsewhere in the code.

  • 10g listener question

    Hi
    All,
    First question
    When I do some minor modification in tnsnames.ora. Do I Need to stop and start listener again.
    Second question
    And If I want to stop and start listener Do I need to stop all the databases before I can stop and start listener ? (9i and 10g)
    Thanks,
    Vishal

    When I do some minor modification in tnsnames.ora. Do I Need to stop and start listener again.No, tnsnames.ora is totally independant to the listener.
    And If I want to stop and start listener Do I need to stop all the databases before I can stop and start listener ? (9i and 10g)No, the listener is used only to connect new client to the server, once the connection is done, listener is not used anymore for that connection.
    Note you can reload instead of stop and start the listener.
    Micolas.

  • 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 Length question!

    When i use JComboBox,it's length is fixed.Perhaps some items is too long to display,so user can't understand it clearly.So i want find JComboBox that can expand automaticly,if item is long,it will be expanded.I tried several function JComboBox provided,but it's remain as usral.I don't know if some master-hand meets same question! Thanks a lot!

    If you want the List wider, look [url http://www2.gol.com/users/tame/swing/examples/SwingExamples.html]here
    If you want the whole combo wider, use
    myCombo.setMinimumSize(new Dimension(200,20));
    It's also possible to calculate the width of string in a certain font (to plug in above), but you can search for that.

  • 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.

  • JComboBox - 2 questions

    First, does implementing ListCellRenderer in a combo govern what is displayed when the combo popup is displayed or when it's not? If it isn't the popup, how do you take control of what is displayed when the combo's button is clicked and the popup displays? I'd like to display a two-column table in the popup area and I'm not quite where to start.
    Second, anyone have any idea how to stop the default drop down popup from displaying when a combo is clicked? Basically, in another case I'd like to install my own listener and display something unique when the combo is clicked and not have the drop down displayed.
    Any thoughts and help would be appreciated.

    ListCellRenderer only presents a user friendly view of what the value is usually is. For example if you add a JLabel to a combo box, the user sees something like this...
    javax.swing.JLabel[preferredwidth=preferredheight=text="Java Website"font=java.awt.Font[...
    ... which is the result of toString(). But if you use a renderer, you present the user with a label so the user only sees:
    Java Website
    Second point: you can do this roughly by rendering your cell as a JPanel with the layout as a GridLayout(2, 1). I haven't done this before but I think it might work. If it doesn't, you'll have to write a custom combobox. Which you will have to do for your third question.
    Stephen                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Retrieving values from a JComboBox - Design question.

    I would like some design guidance on a problem that I am hoping has been solved before. Here is my situation:
    I have a JComboBox that I populate with String values from a database table. The exact set of values to be loaded into the JComboBox varies according values specified elsewhere on the GUI.
    When I select an item from the JComboBox, I need to read the database to retrieve more information. The text is not sufficient for me to identify the data I need, so I need to get the table key from somewhere.
    Is there anyway I can associate my table key with the text value inside the JComboBox and retrieve it when the user selects a drop down value from the JComboBox?
    Many thanks in advance.

    when you load the data from the db, try to get ALL the information needed: item label+item value+description. put this data into a map (a hashmap) for example using a unique identifier. For example, use a numeric index. In this case, the item value should be the index that uniquely identifies your items.
    create a simple bean that encapsulates the item contents: index+value+label, description.
    Doing this will avoid the huge db access occurences.
    hth

  • JCombobox Renderer Question

    I am trying to use a custom renderer for a jcombobox in order to show combobox items in different font color. I get a DefaultListCellRenderer and update it according to my needs (setting font color). When I run the application, the combobox is populated properly. When I select one of the items (say "Red") from the combobox, the item gets selected, but the after the selection the font color of the item that is selected defaults to black (instead of red here). What should I do so that after selecting the item, the selected item is shown in the appropriated font color as indicated by custom renderer.
    Here is the code for the custom renderer.
        private class ColorCmbRenderer implements ListCellRenderer {
            protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
            private final Dimension preferredSize = new Dimension(50, 23);
            public Component getListCellRendererComponent(JList list, Object value,
                                                        int index, boolean isSelected,
                                                        boolean cellHasFocus) {
            JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
                                                        isSelected, cellHasFocus);
                switch (index) {
                    case 0: renderer.setText("Black");
                            renderer.setForeground(Color.BLACK);
                            break;
                    case 1: renderer.setText("Red");
                            renderer.setForeground(Color.RED);
                            break;
                    case 2: renderer.setText("Blue");
                            renderer.setForeground(Color.BLUE);
                            break;
                    case 3: renderer.setText("Green");
                            renderer.setForeground(Color.GREEN);
                            break;
                    default: break;
                renderer.setPreferredSize(preferredSize);
                return renderer;
        }Another thing that I wanted to know is whether each of the items in the jcombobox can be associated with individual Action objects by using something like setAction(new HTMLEditorKit.BoldAction()).
    regards,
    Nirvan.

    There are 3 foregrounds to be set.import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    public class ComboColors {
       JComboBox comboBox;
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                new ComboColors().makeUI();
       public void makeUI() {
          comboBox = new JComboBox(
                new Object[]{"BLACK", "RED", "BLUE", "GREEN"});
          comboBox.setRenderer(new BasicComboBoxRenderer() {
             private final Color[] colors = {
                Color.BLACK, Color.RED, Color.BLUE, Color.GREEN
             @Override
             public Component getListCellRendererComponent(JList list, Object value,
                   int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value,
                      index, isSelected, cellHasFocus);
                if (index == -1) {
                   index = comboBox.getSelectedIndex();
                setForeground(colors[index]);
                list.setSelectionForeground(colors[index]);
                comboBox.setForeground(colors[index]);
                return this;
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(comboBox);
          frame.add(new JButton("Dummy"), BorderLayout.SOUTH);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    }db

  • 11.2.0.3 upgrade to 12.1.0.2 - listener question

    I am currently doing a database upgrade from 11.2.0.3 to 12.1.0.2. The server has a number of other 11g database instances which have not yet been upgraded and the listener on teh server is 11g. Should i be creating a 12c listener for the upgrade i.e. having two listeners on the same server or what's the best way to tackle this scenario? thank you.

    You don't need the 12c listener for the upgrade. Create the 12c listener.ora file, but do not start the listener. Once the upgrade completes, shut down the 11g listener and start the 12c listener.
    Alternatively, install the 12c binaries, create the 12c listener.ora file, shutdown the 11g listener, start the 12c listener, then perform the upgrade.

  • Another RAC Listener question

    Hi all
    Would anyone be able to point me in the right direction as to trouble shoot the following. On one of my RAC nodes I am getting the following.
    When I run: srvctl status listener
    I get
    PRCR-1035 : Failed to look up CRS resource for ora.listener.type
    PRCR-1068 : Failed to query resources
    Cannot communicate with crsd
    When I run: lsnrctl services listener
    I get
    LSNRCTL for 64-bit Windows: Version 11.2.0.2.0 - Production on 13-JUN-2011 14:55
    :00
    Copyright (c) 1991, 2010, Oracle. All rights reserved.
    Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
    The listener supports no services
    The command completed successfully
    Thanks to anyone who can point me in the right direction.

    Hi there - please see below:
    Node2:
    CRS-601: Internal error
    RC: 11, File: clsCheckParser.cpp, Line: 907
    CRS-4000: Command Check failed, or completed with errors.
    CRS-4535: Cannot communicate with Cluster Ready Services
    CRS-4000: Command Status failed, or completed with errors.
    Node1:
    CRS-4537: Cluster Ready Services is online
    CRS-4529: Cluster Synchronization Services is online
    CRS-4533: Event Manager is online
    A Big list of reourceses appears for the status resource command for Node 1
    Thanks

  • ORA - Listener question!!

    Hi Friends,
    In the listener.ora parameter is set to ADMIN_RESTRICTIONS_LISTENER = ON
    To rotate the log without having to download the listener, we need these restrictions are in 'OFF'.
    Is it imperative for the proper functioning of SAP on Oracle ADMIN_RESTRICTIONS_LISTENER is in 'ON'?
    Thanks in advance.
    Martin Sello

    > In the listener.ora parameter is set to ADMIN_RESTRICTIONS_LISTENER = ON
    Ok....
    > To rotate the log without having to download the listener, we need these restrictions are in 'OFF'.
    What? "Download" the listener?
    Why would you need to have the ADMIN_RESTRICTIONS off in order to get a new listener log file?
    How about just restarting the listener?
    (I mean you are the DBA of this machine and you know the password, don't you ? )
    > Is it imperative for the proper functioning of SAP on Oracle ADMIN_RESTRICTIONS_LISTENER is in 'ON'?
    Nope, SAP does not know about this setting.
    In fact no Oracle client would ever know about it.
    It's a server side setting.
    It's just about the listener.
    cheers,
    Lars

  • JComboBox newbie question

    I cant seem to find out how to add items to my jcombobox using Forte.
    I have this :
    String[] dbId = { "579", "459", "789", "098", "297" };
    jComboBox1.addItem(dbId);
    Where do I put this code to fill the combo box with the String array?
    Steve

    Depends on what application you are building.
    If you are building a standalone application, put those codes in the constructor of your application class.
    If you are building an applet, put those codes in the init() method of your applet.

Maybe you are looking for

  • CS6 - Edit Clip in Adobe Audition Grayed Out

    I used to own the CS5.5 Master Collection Suite, and recently upgraded to CS6. Tonight, for the first time, I am using the new Premiere Pro CS6. Besides a few quirks and buginess, one frustrating issue is that the "Edit in Adobe Audition" option is g

  • Mac mini is freezing

    My mac mini is freezing with flashing question mark. Pl help

  • Variable for an infoobject when that infoobject is unavailable in the cube

    Hello I have a variable for 0FISCPER (V1), which gets populated based on another variable for 0FISCYEAR (V2). But the infoprovider on which the query is based doesnt have 0FISCYEAR infoobject. so I am unable to pass a value to the variable V2. is the

  • Functional Tables

    Hi gurus,    Can anyone tell me the functional tables used in developing a report to get the Customer/Customer group wise sales and quantity. Thanks Sandeep.

  • Sorting Order Not Held

    For some reason, when I choose to Sort Descending in a Library Folder of a Smart Album, iPhoto will re-order in the current session, but not "remember" the change when I quit iPhoto and re-open? Is that the way it works or should it remember?