So I am trying to make a program that adds a subject to a jList from a textfield and after that, I want to add the grade for that subject from a textfield. Is it possible to store (in an array) that value to the item selected on the jList? so that I can access it for getting the average of all the grades of the subjects entered.
int x[] = jList1.getSelectedIndices();
for(int i = 0; i < jList1.getModel().getSize(); i++){
grade[x[i]] = Double.parseDouble(jTextField2.getText());
jList1.getSelectedValue();
}
You are supposed to manipulate data in the model that jList is based on.
listModel = new DefaultListModel();
listModel.addElement("Jane Doe");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
list = new JList(listModel);
https://docs.oracle.com/javase/tutorial/uiswing/components/list.html#creating
Jlist is a (view) component that displays a list of objects and allows the user to select one or more items. A separate model, ListModel, maintains the contents of the list.
https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html
What is the exact problem with your implementation?
EDIT
Double grade = Double.parseDouble(jTextField2.getText());
for(int i = 0; i < jList1.getModel().getSize(); i++){
String before = jList1.getModel().getElementAt(i);
String after=before+"_"+String.valueOf(grade);
jList1.getModel().setElementAt(after,i);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.