You are viewing our Forum Archives. To view or take place in current topics click here.
Java, Integer to array to combobox
Posted:

Java, Integer to array to comboboxPosted:

5FDP_Jekyll
  • Supporter
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
I am currently trying to add a GUI to a program I had made. Currently, I am going a bit different with getting the data, as before I had the user implement all the required input. Now I am trying to make it to allow the user to select the xp required rather than inputting it themselves.

So because of this I am having quite the amount of issues. So my question is, how can I go about reading integers from a file, from those integers creating an array, then after putting them in an array, putting them all into a combobox for selection by the user?

If needed I will post the code I have currently been fiddling with to try to get it work. However, if I could get a generic code that I could use for this, I'm sure I'd be able to make it work in the program.
#2. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Something like this? [ Register or Signin to view external links. ] (excluding combobox)
#3. Posted:
5FDP_Jekyll
  • 2 Million
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Perhaps I should have said that I mainly needed help with taking an array of type int to show all of its elements in the comboxbox lol. I was able to easily figure out how to take an int and put it into an array, just having trouble with the comboxbox reading from the array.

Keep getting errors with that part.

This is what I was currently attempting to do:
Scanner reader = new Scanner(new File("levels.txt"));
         ArrayList<Integer> exprequired = new ArrayList<Integer>();
         while (reader.hasNext()){
            exprequired.add(reader.nextInt());
            reader.nextLine();
        reader.close();
         }
      JComboBox<Integer> reqexp = new JComboBox<>(exprequired);


However, that just keeps giving me an error on the JComboBox of "cannot infer type arguments for JComboBox<>"
#4. Posted:
tortuga
  • Wizard
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Read the Javadoc for JComboBox [ Register or Signin to view external links. ]

Look at the constructor summary. None of them take a List as their input. There is one for just an array though. You're currently working with ArrayLists - not arrays. You have to convert your ArrayList to an array.
#5. Posted:
5FDP_Jekyll
  • TTG Addict
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
tortuga wrote Read the Javadoc for JComboBox [ Register or Signin to view external links. ]

Look at the constructor summary. None of them take a List as their input. There is one for just an array though. You're currently working with ArrayLists - not arrays. You have to convert your ArrayList to an array.
Thanks, that completely makes sense now, lol. Hopefully I can finish this program now.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.