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: Source from a JComboBox

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Source from a JComboBox

    So I have a JComboBox and a button. I want the user to select an option from the combo box, then click the delete button which will call the action listener:

    ActionListener DeleteVert = new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JComboBox cb = (JComboBox) e.getSource();
    		        String Selection = (String)cb.getSelectedItem();
    		        System.out.println(Selection);
    			}
            };
     
            Delete.addActionListener(DeleteVert);

    When I try to click the button it throws an exception on the line:
    JComboBox cb = (JComboBox) e.getSource();

    The exception says something about an unknown source. Anyone got any ideas whats wrong?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Source from a JComboBox

    Perhaps the source was not a JComboBox? What does the error message say?

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Source from a JComboBox

    I changed it so the action listener is set to the combo box instead of the button and it prints when I click on one of the options in the drop down box, but I wanted it so that you select the option and then press the button for it to run.

    Any idea how I would do that instead?

Similar Threads

  1. JComboBox
    By ikocijan in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 16th, 2012, 06:35 AM
  2. JComboBox
    By SV25 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 20th, 2011, 08:47 AM
  3. [SOLVED] Having Problem with my JComboBox
    By dazzl3r in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 9th, 2010, 12:03 AM
  4. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM
  5. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM

Tags for this Thread