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 7955139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:38:39+00:00 2026-06-04T03:38:39+00:00

How can I get a selection from a JComboBox to correlate to a number

  • 0

How can I get a selection from a JComboBox to correlate to a number of array selections?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;

class ContactsReader extends JFrame
{
public JPanel mainPanel;
public JPanel buttonPanel;
public JPanel displayPanel;

public JLabel titleLabel;
public JLabel nameLabel;
public JLabel ageLabel;
public JLabel emailLabel;
public JLabel cellPhoneLabel;
public JLabel comboBoxLabel;

public JButton exitButton;

public JTextField nameTextField;
public JTextField ageTextField;
public JTextField emailTextField;
public JTextField cellPhoneTextField;

public JComboBox<String> contactBox;

public String[] getContactNames;
public String[] displayContactNames;

public File contactFile;
public Scanner inputFile;

public String selection;

public ContactsReader()
{
    super("Contacts Reader");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    buildPanel();

    add(mainPanel, BorderLayout.CENTER);

    pack();
    setVisible(true);

} 

public void buildPanel()
{
    titleLabel = new JLabel("Please enter contact information");
    mainPanel = new JPanel(new BorderLayout());

    buttonPanel();
    displayPanel();

    mainPanel.add(titleLabel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    mainPanel.add(displayPanel, BorderLayout.CENTER);
}

public void buttonPanel()
{
    //create submit and exit buttons
    exitButton = new JButton("Exit");
    exitButton.addActionListener(new exitButtonListener());

    buttonPanel = new JPanel(); //create button panel

    buttonPanel.add(exitButton);    //add exit button to panel
}

public void displayPanel()
{
    String nameHolder;

    int count = 0;

    nameLabel = new JLabel("Name");
    ageLabel = new JLabel("Age)");
    emailLabel = new JLabel("Email");
    cellPhoneLabel = new JLabel("Cell Phone #");
    comboBoxLabel = new JLabel("Select a Conact");

    nameTextField = new JTextField(10);
    nameTextField.setEditable(false);
    ageTextField = new JTextField(10);
    ageTextField.setEditable(false);
    emailTextField = new JTextField(10);
    emailTextField.setEditable(false);
    cellPhoneTextField = new JTextField(10);
    cellPhoneTextField.setEditable(false);

    try{
        contactFile = new File("ContactData.txt");
        inputFile = new Scanner(contactFile);   
    }
    catch (Exception event){}

    while (inputFile.hasNext())
    {
        nameHolder = inputFile.nextLine();
        count++;
    }
    inputFile.close();

    String getContactNames[] = new String[count];
    String displayContactNames[] = new String[count/4];

    try{
        contactFile = new File("ContactData.txt");
        inputFile = new Scanner(contactFile);   
    }
    catch (Exception event){}

    for (int i = 0; i < count; i++)
    {
        if (inputFile.hasNext())
        {
            nameHolder = inputFile.nextLine();
            getContactNames[i] = nameHolder;

            if (i % 4 == 0)
            {
                displayContactNames[i/4] = getContactNames[i];
            }

        }
    }

    inputFile.close();

    contactBox = new JComboBox<String>(displayContactNames);
    contactBox.setEditable(false);
    contactBox.addActionListener(new contactBoxListener());

    displayPanel = new JPanel(new GridLayout(10,1));

    displayPanel.add(comboBoxLabel);
    displayPanel.add(contactBox);
    displayPanel.add(nameLabel);
    displayPanel.add(nameTextField);
    displayPanel.add(ageLabel);
    displayPanel.add(ageTextField);
    displayPanel.add(emailLabel);
    displayPanel.add(emailTextField);
    displayPanel.add(cellPhoneLabel);
    displayPanel.add(cellPhoneTextField);

}       

private class exitButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0); //set exit button to exit even when pressed
    }
}

private class contactBoxListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        //get selection from dropdown menu
        selection = (String) contactBox.getSelectedItem();
    }
}

public static void main(String[] args)
{

    new ContactsReader();   //create instance of Contact Reader
}

}

I want the selection to send the name, age, email, and cell phone # to the corresponding text fields. I can figure out to get a selection but don’t know how to make it choose the correct array selections and send it to the text fields.

  • 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-06-04T03:38:41+00:00Added an answer on June 4, 2026 at 3:38 am

    Don’t have the JComboBox hold just Strings, but rather have it hold objects of a custom class that contain all the information that you will need when it’s selected. Then use the object selected to populate your JTextFields.

    For instance, consider creating a class, say called Contact,

    public class MyContact {
      String name;
      Date dateOfBirth; // in place of age
      String email;
      String cellPhone;
    
      //...
    }
    

    And then create a JComboBox<MyContact>

    When an item is selected, call the corresponding getXXX() getter method to extract the information to fill the JTextField. You will want to give the JComboBox a custom CellRenderer so that it displays the contacts nicely.

    For example:

    import java.awt.Component;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyComboEg extends JPanel {
       private static final MyData[] data = {
             new MyData("Monday", 1, false),
             new MyData("Tuesday", 2, false),
             new MyData("Wednesday", 3, false),
             new MyData("Thursday", 4, false),
             new MyData("Friday", 5, false),
             new MyData("Saturday", 6, true),
             new MyData("Sunday", 7, true),
       };   
       private JComboBox<MyData> myCombo = new JComboBox<MyData>(data);
       private JTextField textField = new JTextField(10);
       private JTextField valueField = new JTextField(10);
       private JTextField weekendField = new JTextField(10);
    
       public MyComboEg() {
          add(myCombo);
          add(new JLabel("text:"));
          add(textField);
          add(new JLabel("value:"));
          add(valueField);
          add(new JLabel("weekend:"));
          add(weekendField);
    
          myCombo.setRenderer(new DefaultListCellRenderer(){
             @Override
             public Component getListCellRendererComponent(JList list,
                   Object value, int index, boolean isSelected, boolean cellHasFocus) {
                String text = value == null ? "" : ((MyData)value).getText();
                return super.getListCellRendererComponent(list, text, index, isSelected,
                      cellHasFocus);
             }
          });
          myCombo.setSelectedIndex(-1);
    
          myCombo.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                // MyData myData = (MyData) myCombo.getSelectedItem();
                MyData myData = myCombo.getSelectedItem();
                textField.setText(myData.getText());
                valueField.setText(String.valueOf(myData.getValue()));
                weekendField.setText(String.valueOf(myData.isWeekend()));
             }
          });
       }
    
       private static void createAndShowGui() {
          MyComboEg mainPanel = new MyComboEg();
    
          JFrame frame = new JFrame("MyComboEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class MyData {
       private String text;
       private int value;
       private boolean weekend;
    
       MyData(String text, int value, boolean weekend) {
          this.text = text;
          this.value = value;
          this.weekend = weekend;
       }
       public String getText() {
          return text;
       }
       public int getValue() {
          return value;
       }
       public boolean isWeekend() {
          return weekend;
       }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HashMap selections = new HashMap<Integer, Float>(); How can i get the Integer key of
I'm currently using the ColorPickerDialog.java provided by Google. I can get it to load
I'm trying to get the class name from an selection of images when clicked.
I'm trying to get a list of values to display upon a selection from
I am trying to find a way to get a random selection from a
How can i get section cell text on custom cell button click. is this
How can I get the adjacent sibling of a dynamic selector? This is what
how can i get all songs selected when i click playselected button without selecting
How can I get the variable My.Application.Info.Version.ToString to populate in the comments section? Dim
when selecting a column that can contains a NULL value I get an exception

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.