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 2 of 2

Thread: Problem getting selected item as string from JComboBox

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem getting selected item as string from JComboBox

    Hey there I'm trying to implement my own custom JComboBox class as here:

    package guitesting;
     
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;
     
     
    public class ComboBoxPotSelection extends JComboBox implements ActionListener{
        private String selection;
     
        public ComboBoxPotSelection(String[] pots){
            this.addItem(pots);
            this.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent CmbBoxEvent){
                    JComboBox combo = (JComboBox)CmbBoxEvent.getSource();
                    selection = (String)combo.getSelectedItem();
                }
            });
            this.setEditable(false);
            this.setSelectedIndex(0);
            this.setVisible(true);
        }
     
        public String retStrVal(){
            return selection;
        }
    }

    But when i try to run the implementation Im getting:

    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
    at guitesting.ComboBoxPotSelection$1.actionPerformed( ComboBoxPotSelection.java:17)

    I cant understand why I cant cast my selected item to a string??


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Problem getting selected item as string from JComboBox

    The item you're adding is a String array, so that's what you get back.

    Read the API for JComboBox and follow the link to the Swing tutorial on How to Use Combo Boxes, where you will find a number of code samples.

    db

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. Making item sortable (implementing Comparable)
    By Asido in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 19th, 2010, 12:44 PM
  3. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM