I have a Combo box/Dropdown and when a user clicks an option I want the combo box to set the text onto a jTextField.

For example:

if(carsJComboBox.getSelectedIndex() == 0)
{
    currentCarsJTextField.setText("250 Million");
}

if(carsJComboBox.getSelectedIndex() == 1)
{
    currentCarsJTextField.setText("500 Million");
}

The problem is that when the program runs, it automatically sets 250 Million to the JTextField. I understand why it does that because 250 Million is the first drop down option, but the JTextField never updates. When I choose the option below 250 Million, which is 500 Miliion and click on it, it doesn't set the JTextField text to 500 Million.

Also, is there a way I could add the two numbers. For example: I choose 250 Million, then choose 500 Million and the JTextField sets the text to 750 Million?

This might be a bit confusing but thank you.

Are you sure that code is being executed? Maybe there's a problem with your listener. Try printing the selected index in those if tests to confirm.

You can have a "total" variable that you add 250 or 500 (etc) to in your listener the set the text field to
total + " Million"

The code is being executed. I don't think I gave enough information. Here:

These numbers in this array are the options in the combo box:

private String[] newnumbers = {"Add 250 Million","Add 500 Million","Add 1 Billion","Add 1.5 Billion","Add 2 Billion"};

What I want to do is when the user clicks "Add 250 Million", it pastes "250 Million" on the JTextField. Then when the users clicks "Add 500 Million" it pastes "750 Million" on the JTextField because 250 + 500 = 750.

I don't think I can have a variable because the numbers in the array are strings not ints.

int cumulatedMillions = 0; // declared outside listener method

// inside listener method...
if(carsJComboBox.getSelectedIndex() == 0)
{
    cumulatedMillions += 250;
}
if(carsJComboBox.getSelectedIndex() == 1)
{
    cumulatedMillions += 500;
}
currentCarsJTextField.setText(cumulatedMillions + " Million");  
// needs to be more clever for billions

// or could define array {250, 500 ...} and use getSelectedIndex()
// to index into it, thus avoiding all those if tests
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.