Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: do actions in ComboBox

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do actions in ComboBox

    heloo!
    I have designed a frame with a combo box,i want after choosing an item in combo box and submitting it by pressing OK,do some actions according to the selected item such as opening a new frame... .how can i do this?
    can u give me some sample codes for that?will be gratfull if help me about it!


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: do actions in ComboBox

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
            if(evt.getStateChange()== evt.SELECTED && evt.getItem().toString().equals("Item 1"))
            {
                JOptionPane.showMessageDialog(null,"nmkbsdmf");
            }
        }

    I think it may help you ... if you change used listener interface.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: do actions in ComboBox

    You kind of answered your question as you asked it...

    You said in your question, "according to the selected item".

    The last two words that I quoted above are your answer. Anytime you want to know which item in your combobox is currently selected, you have two ways of finding out:

    JComboBox1.getSelectedIndex(); // Which returns and int value, with 0 being the first item in the combobox.
    JComboBox1.getSelectedItem(); // Which returns a String value that is the text of the item.

    Either one of these will get you where you need to be. Just put the following in your JButtonActionPerformed event:

    if ( JComboBox1.getSelectedItem().equals("What you are looking for") ) {
         // Take some action for this item.
    } else if ( JComboBox1.getSelectedItem().equals("Another string of text") ) {
         // Take some different action.
    } // ...etc...etc...etc

    Hope this helps.

    Cheers,

    Sean C.
    PekinSOFT Systems

Similar Threads

  1. Java program to display 2D Array in ComboBox
    By crazydeo in forum AWT / Java Swing
    Replies: 1
    Last Post: May 29th, 2008, 09:13 AM