1

Purpose: JComboBox to list down ages that a user can select

I realize that I need an array of integers. What part of the Math functions in Java will allow me to easily do that? The list of numbers will be from 1-100 in sequential order.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Maydayfluffy
  • 427
  • 1
  • 7
  • 16

4 Answers4

4

I don't quite understand why you need the Math functions.

This would work:

List<Integer> age = new ArrayList<Integer>();
for (int i = 1; i <= 100; ++i) {
    age.add(i);
}
JComboBox ageComboBox = new JComboBox(age.toArray());
Robin
  • 36,233
  • 5
  • 47
  • 99
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • Putting that in, I get an error. Syntax error on token "int", Dimensions expected after this token. What does this mean? – Maydayfluffy Feb 19 '12 at 00:01
  • @Maydayfluffy on which line did you get the error, maybe you're using the code incorrectly? – Adel Boutros Feb 19 '12 at 01:29
  • The problem with this code is List. It should be List . Same for ArrayList. I edited the answer to remove the compile errors – Robin Feb 19 '12 at 12:00
3

You don't need any math functions. Look up JComboBox in the java docs and you'll find a .addItem function. It can take a String (e.g. "1") or a Number (e.g. new Integer(1)). Just iterate in a for-loop and add the items you need.

Tony
  • 1,401
  • 9
  • 11
2

I suspect a JSpinner using a SpinnerNumberModel would be a better component for selecting an integer based age or Y.O.B. See How to Use Spinners in the tutorial for more info.

3 spinners

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

maybe you have look at AutoComplete ComboBox / JTextField

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319