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.
Related
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!
I would like to create an array that holds the information (in this case the ID) and then transfer this data into a drop down list.
The below code is where the data will be inputted and where I created the array list:
Person cons_save = new Person();
cons_save.setPersonfirstname(this.jTextField1.getText());
cons_save.setPersonlastname(this.jTextField2.getText());
cons_save.setPersonID(this.jTextField3.getText());
this.jTextField1.setText("");
this.jTextField2.setText("");
this.jTextField3.setText("");
cons_save.savecons();
ArrayList<String> idList = new ArrayList<String>();
idList.add(cons_save.PersonID);
I need that every time that I input the ID and save, the id will transfer to the array and go to a combo box in another field.
The code I am trying to input in the combo box is the following:
Object[] idList = Person.getidList();
JComboBox box = new JComboBox (idList);
But this keeps issuing multiple errors. I have got this code from other questions similar to mine but it is not working
the task which I do is quite simple, but I faced one problem with JList component.
What i need is. I fetch data from DataBase, load it to String array, pack array into ArrayList (just because I duno how many records I have), return it from method as ArrayList.
Now at receiver side. I have a trouble, I cant fetch String array from ArrayList so that I can pass to JList.
Here is the code.
ArrayList<Object> fetch = new ArrayList<Object>();
public String[] data =new String[10];
listModel = new DefaultListModel();
myList = new JList(listModel);
//myList = new JList(data);
// So it works with simple array of strings.
fetch=DAO.loadPasswords();
//At this point it asks me to cast an object to smth which late cause null pointer exception.
myList = new JList(fetch.get(0));
And here is loadPasswords();
public static ArrayList<Object> loadPasswords(){
dbConnect();
boolean unswer = false;
String[] result=null;
String query1 = "select * from tblPasswordsStorage where "
+ "_user_id = ?";
String userId=Personal_Organizer.userProfile.getUserID();
ArrayList<Object> params = new ArrayList<Object>();
params.add(userId);
executeQueryP(query1, params);
ArrayList<Object> fetched=null;
try {
if (rs.next()) {
unswer = true;
//Personal_Organizer.userProfile.setUserID(rs.getString(1));
result[0]=rs.getString(1);
result[1]=rs.getString(2);
result[2]=rs.getString(3);
result[3]=rs.getString(4);
result[4]=rs.getString(5);
result[5]=rs.getString(6);
result[6]=rs.getString(7);
result[7]=rs.getString(8);
result[8]=rs.getString(9);
result[9]=rs.getString(10);
fetched.add(result);
}
if (unswer) {
while (rs.next()) {
Tools.print(rs.getString(1));
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,
"SQL Server connection issue.\n"
+ "Please, check Server address, user name and password.",
"Output",
JOptionPane.PLAIN_MESSAGE);
}
dbClose();
return fetched;
}
I tried to use multidimensional array but still JList require single dimensional array.
Either use a DefaultListModel and load it with your Strings using a simple for or for-each loop that loops through your ArrayList, adding each String item in the ArrayList into the model, or create your own ListModel class, one that extends AbstractListModel and that uses the ArrayList as its data nucleus.
Edit, since your data is held in an ArrayList of arrays, then perhaps you don't want to display it within an ArrayList after all, but rather in a JTable. Here your best bet would be to create your own TableModel, one based off of AbstractTableModel and that uses your ArrayList as its data nucleus. Also, perhaps better than loading your data into an array would be to create a custom class to hold each row of data from the ResultSet.
You state:
My problem is to simply load something to JList. The idea is to display application/website name in the list, then when you click on it, you get full data which contains in array. That's why I put fetch.get(0)...because. I need at least something.
Then
Create a custom class to hold each row of data.
Create items of this class from each row of the ResultSet
Fill your DefaultListModel with items of this class
Give your JList a custom ListCellRenderer, one that display's the application/website name
Then you can display the name and still have each list item hold all the pertinent information that it needs.
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.
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!