Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:40:23+00:00 2026-05-30T11:40:23+00:00

What method is used to return the selection chosen by the user? JPanel ageSelection

  • 0

What method is used to return the selection chosen by the user?

JPanel ageSelection = new JPanel();
JLabel age = new JLabel("Age:");

ArrayList<Integer> ageList = new ArrayList<Integer>();

for (int i = 1; i <= 100; ++i) {
    ageList.add(i);
}

DefaultComboBoxModel<Integer> modelAge = new DefaultComboBoxModel<Integer>();
for (Integer i : ageList) {
    modelAge.addElement(i);
}

JComboBox<Integer> ageEntries = new JComboBox<Integer>();
ageEntries.setModel(modelAge);

ageEntries.addActionListener(new putInTextListener());

ageSelection.add(age);
ageSelection.add(ageEntries);


class putInTextListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        ageEntries.getSelectedItem();
    }
}

When the last line is added (ageEntries.getSelectedItem();), I get an error:

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

Any ideas?

Edited Code:

class putInAgeListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {

        Object myAge = ageEntries.getSelectedItem();

        String myAgeData = myAge.toString();

        int i = Integer.parseInt(myAgeData);

        System.out.print(i);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:40:24+00:00Added an answer on May 30, 2026 at 11:40 am

    1) this statement is empty and probably you want to get Integer / Object / String value from currently selected Item

    Integer / Object / String myWhatever = ageEntries.getSelectedItem();
    

    2) better would be use ItemListener for JComboBox, rather than ActionListener, notice ItemListener fired events SELECTED/DESELECTED, always twice

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboBoxListeners {
    
        private JFrame f;
        private JComboBox flyFromCombo;
        private JComboBox flyToCombo;
        private JLabel tripLabel = new JLabel();
        private Object[] itemsFrom;
        private Object[] itemsTo;
    
        public ComboBoxListeners() {
            itemsFrom = new Object[]{"-", "First - From", "Second - From", "Third - From"};
            itemsTo = new Object[]{"-", "First - To", "Second - To", "Third - To"};
            //flyFromCombo.setPrototypeDisplayValue("################################################");
            flyFromCombo = new JComboBox(itemsFrom);
            flyFromCombo.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if ((e.getStateChange() == ItemEvent.SELECTED)) {
                        String str = flyFromCombo.getSelectedItem().toString();
                        String str1 = flyToCombo.getSelectedItem().toString();
                        setLabelText(str, str1);
                    }
                }
            });
            flyToCombo = new JComboBox(itemsTo);
            flyToCombo.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if ((e.getStateChange() == ItemEvent.SELECTED)) {
                        String str = flyFromCombo.getSelectedItem().toString();
                        String str1 = flyToCombo.getSelectedItem().toString();
                        setLabelText(str, str1);
                    }
                }
            });
            tripLabel.setPreferredSize(new Dimension(400, 30));
            f = new JFrame("ComboBox ItemListeners");
            f.setLayout(new GridLayout(0, 1, 15, 15));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(flyFromCombo);
            f.add(flyToCombo);
            f.add(tripLabel);
            f.setLocation(150, 150);
            f.pack();
            f.setVisible(true);
        }
    
        private void setLabelText(String str1, String str2) {
            String textForLabel = "";
            String helpStringFirst = str1.trim();
            if (helpStringFirst != null && helpStringFirst.length() > 0) {
                if (!helpStringFirst.equals("-")) {
                    textForLabel = "Flight No57. from :   " + helpStringFirst;
                } else {
                    textForLabel = "Flight from Un-Know :   ";
                }
            }
            String helpStringSecond = str2.trim();
            if (helpStringSecond != null && helpStringSecond.length() > 0) {
                if (!helpStringSecond.equals("-")) {
                    textForLabel = textForLabel + "   --> to :   " + helpStringSecond;
                } else {
                    textForLabel += "   to :   Un-Know    ";
                }
            }
            final String pushTextForLabel = textForLabel;
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    tripLabel.setText(pushTextForLabel);
                }
            });
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
                }
            });
        }
    }
    

    EDIT

    I haven’t (and don’t want too) JDK7,

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class ComboBoxListeners {
    
        private JFrame f;
        private JComboBox flyFromCombo;
        private JLabel tripLabel = new JLabel();
    
        public ComboBoxListeners() {
            ArrayList<Integer> ageList = new ArrayList<Integer>();
            for (int i = 1; i <= 100; ++i) {
                ageList.add(i);
            }
            DefaultComboBoxModel modelAge = new DefaultComboBoxModel();
            for (Integer i : ageList) {
                modelAge.addElement(i);
            }
            flyFromCombo = new JComboBox(modelAge);
            flyFromCombo.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if ((e.getStateChange() == ItemEvent.SELECTED)) {
                        String str = flyFromCombo.getSelectedItem().toString();
                        tripLabel.setText("Selected Age From JComboBox is :   " + str);
                    }
                }
            });
            tripLabel.setPreferredSize(new Dimension(400, 30));
            f = new JFrame("ComboBox ItemListeners");
            f.setLayout(new GridLayout(0, 1, 15, 15));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(flyFromCombo);
            f.add(tripLabel);
            f.setLocation(150, 150);
            f.pack();
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method (used to generate friendly error messages in unit tests):
[Update: I've found the API reference. The method used is below] <?php wp_delete_post( $postid,
For instance in C# or Java, you always have a main() method used to
Can the #save method be used to update a record? I know that I
In Java, flush() method is used in streams. But I don't understand what are
In .NET, the GetHashCode method is used in a lot of places throughout the
In unit testing, the setup method is used to create the objects needed for
The case: There is a .net application calling unmanaged C code. Used method for
I used the method $(#dvTheatres a).hover(function (){ $(this).css(text-decoration, underline); },function(){ $(this).css(text-decoration, none); } );
I used sprintf method to format data to a string which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.