ArrayList and JOptionPane.showInputDialog() - java

I am trying to learn Java and have come a long way from PHP, I tried to apply the same mentality when creating my code. But as many of you have already known, it doesn't work that easily.
So with that said, I have a question. I want to create a drop down list from items in an ArrayList. I am fond of the idea of using the JOptionPane.showInputDialog() method to attempt this.
This is what I have currently, but I get an error telling me that no suitable method found for showInputDialog
ArrayList<String> projectList = new ArrayList<String>();
while(results.next())
projectList.add(results.getString("project"));
String inputDialog = (String)JOptionPane.showInputDialog(this, "Choose project to open", "Open Project", JOptionPane.PLAIN_MESSAGE, null, projectList, "--");
I know that the issue is that when I pass the ArrayList as the array object I get thrown this error. But if I did something like
Object[] projectList = {"one", "two"};
then it works as intended, I then tried to maybe do this and pass in projects as my array object.
Object[] projects = {projectList.toString()};
This somewhat works, but then the output looked like "one, two" in drop down list as 1 line item.

You can convert ArrayList to Object[] if needed using List.toArray(), ie:
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
Object[] result = list.toArray();

projectList is an ArrayList, convert it to array and then pass it to the method JOptionPane.showInputDialog(). Please check List.toArray() method.
String [] projects = projectList.toArray(new String[projectList.size()]);

Try following
Object[] projectListArray =new Object[projectList.size()];
projectList.toArray(projectListArray);
String inputDialog = (String)JOptionPane.showInputDialog(this, "Choose project to open", "Open Project", JOptionPane.PLAIN_MESSAGE, null, projectListArray , "--");
Hope it helps.

Related

How to add ArrayList in Netbeans default comboboxmodel? [duplicate]

Currently have an ArrayList called SundayList which is loaded as soon as the frame AddStudent is loaded (bit of GUI)
The code automatically generated by Netbeans is:
comboboxSunday = new javax.swing.JComboBox();
comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item1", "Item2" }));
How do I load the combobox items with my own array?
The array includes items such as:
Activity1
Activity2
Activity3
Activity4
From my previous search, people mentioned about using a toString() and toArray(), and I'm not familiar with either methods, and how they help in loading the list into the combobox as I'm quite new to java..
You could create your own ComboBoxModel that takes a List as the main parameter, but that's a little more involved
comboboxSunday.setModel(new DefaultComboBoxModel());
for (Object item : listOfItems) {
comboboxSunday.addItem(item);
}
Assuming your array looks something like this:
String[] SundayList = { "Activity1", "Activity2", "Activity3", "Activity4" };
You can do this:
javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList);
If your array isn't a string array. try:
javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList.toString());
Hope this helps!

Java: Add to array by JOptionPane

I have this array:
String[] countriesList = {
"Japan",
"Sverige",
"Tyskland",
"Spanien",
"Syrien",
"Litauen",
};
I want to be able to add another thing to the array, in this case this [6]th position. Is it possible to do this by JOPtionPane? This is what I've done this far, however nothing happens nor does any errors occur.
String addland = JOptionPane.showInputDialog("Vilket land vill du lägga till?").trim();
countriesList[6] = addland;
Arrays start their counting from 0, so you could use countriesList[5] = addland;
You may use a dynamic list to perform your task.
They are better in every situation and should be superior to simple Arrays
Try to use this
List<String> countriesList = new ArrayList<>(
Arrays.asList("Japan", "Sverige", "Tyskland", "Spanien", "Syrien", "Litauen"));
String addland = JOptionPane.showInputDialog("Vilket land vill du lägga till?").trim();
countriesList.set(5,addland);
System.out.println(countriesList);
Output, after entering asdadsad:
[Japan, Sverige, Tyskland, Spanien, Syrien, asdadsad]
To add a land to the existing list use countriesList.add(addland); instead of countriesList.set(5,addland);

Android: Compare strings in array list and combine them

I'm wanting to compare the strings in an array list taken from my database and join them together..
Here is the code which collects data from my database..
public List<String> getData2List() {
String[] columns = new String[]{ KEY_ROWID, KEY_DATE, KEY_NAME, KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "1", null, null, null, null);
List<String> results = new ArrayList<String>();
int iCM = c.getColumnIndex(KEY_DATE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(iCM));
}
return results;
}
and here is the code to place them in the list..
Database info = new Database(this);
info.open();
List<String> dates = info.getData2List();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dates));
info.close();
This all works fine but if there are more than one entry which is the same I end up with a list of the same thing (if this makes sense!?).
Example:
if the list came out like {"01/01/13", "02/01/13", "01/01/13", "03/02/13", "01/01/13"}
I'm trying to make the out come like {"01/01/13", "02/01/13", "03/02/13"}
so that all entry of the same value have been compiled into one.
Any help or ideas is much appreciated.
Just use an Set instead of a List.
An HashSet will provide you unique strings, a List instead can contain same occurrence.
Why don't you just use HashSet<String> ?
Example:
List<String> dates = info.getData2List();
Set<String> uniqueDates = new HashSet<String>(dates);
You can use HashSet instead of ArrayList.
because in your case you will have following advantages
joining two set will be more easier useing addAll method
There is no chance to have duplicate values
You need a Set collection class, basically what "Set" does is, storing only unique values, and if you try to set a new value that already exist in the collection it will just ignore, make sure you are overriding equals, and hashCode method as well in case you are trying to store your own object, look at the documentation for more info about how it works..
http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
Regards!

Loading an ArrayList into a JCombobox using netbeans

Currently have an ArrayList called SundayList which is loaded as soon as the frame AddStudent is loaded (bit of GUI)
The code automatically generated by Netbeans is:
comboboxSunday = new javax.swing.JComboBox();
comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item1", "Item2" }));
How do I load the combobox items with my own array?
The array includes items such as:
Activity1
Activity2
Activity3
Activity4
From my previous search, people mentioned about using a toString() and toArray(), and I'm not familiar with either methods, and how they help in loading the list into the combobox as I'm quite new to java..
You could create your own ComboBoxModel that takes a List as the main parameter, but that's a little more involved
comboboxSunday.setModel(new DefaultComboBoxModel());
for (Object item : listOfItems) {
comboboxSunday.addItem(item);
}
Assuming your array looks something like this:
String[] SundayList = { "Activity1", "Activity2", "Activity3", "Activity4" };
You can do this:
javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList);
If your array isn't a string array. try:
javax.swing.JComboBox sundayCombo = new javax.swing.JComboBox(SundayList.toString());
Hope this helps!

How to populate an Object[] with items

I'm trying to use a Swing Dialog so that the user can choose an item from a list of options with poolTeams being the name of that list. Like this:
String team = (String)JOptionPane.showInputDialog(frame, "Please choose a team:\n",
"Choose Team", JOptionPane.PLAIN_MESSAGE, null, poolTeams, "");
According to the documentation, poolTeams needs to be of type Object[] so I can't use ArrayLists or anything like that.
The problem is; the items in poolTeams will vary so I can't just populate it like
Object[] poolTeams = {"a", "b", "c"};
Is there a way I can make the program populate it automatically? If not, is there a different way I can offer the user a list in the dialog box?
Thanks!
List<Object> options = new ArrayList<Object>();
options.add(...);
options.add(...);
options.add(...);
Object [] selections = options.toArray()
You can very easily turn an ArrayList to an array using the toArray() method. Build up your ArrayList, then turn it into an array when you need it.

Categories