My program needs to tell whether two Students were selected, and if so I need to output to the screen their averages. I got the first specification which works for only one selection (just shows grade) but I have no idea to tell whether they selected two from the list...
Thanks!
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
// This demonstrates a Single Choice JList. The choice is displayed in a label
public class Grades1 extends JFrame implements ListSelectionListener{
private JList list;
private JLabel jlab;
private BufferedImage image;
private int w,h;
private GradeManager g;
private ArrayList<Student> students;
public Grades1(){
g=new GradeManager();
students=g.getList();
ArrayList<String> names=new ArrayList<String>();
for(int i=0; i<students.size(); i++){
names.add(students.get(i).getName());
}
DefaultListModel myModel = new DefaultListModel();
setTitle("Student's Grades");
setSize(250,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
jlab = new JLabel("Make Selection: ");
jlab.setForeground(Color.RED);
jlab.setBounds(54, 230, 200, 10);
list = new JList(myModel);
//add student names to JList
for(String s: names)
myModel.addElement(s);
list.addListSelectionListener(this); // register for listening
JScrollPane jscp = new JScrollPane(list);
jscp.setBounds(35, 10, 170, 150);
jscp.setPreferredSize(new Dimension(170,150));
Panel mainPanel = new Panel("pic.jpg");
mainPanel.setBounds(0, 0, 250, 300);
mainPanel.setLayout(null);
add(jscp);
add(jlab);
add(mainPanel);
setVisible(true);
}
public void valueChanged(ListSelectionEvent le){
int index = list.getSelectedIndex();
if(index != -1)
jlab.setText(students.get(index).getName() + "'s Grade is: " + students.get(index).getGrade());
}
//public void valueChanged(ListSelectionEvent le, )
class Panel extends JPanel{
public Panel(String fname){
this.setSize(1000000000,30);
try {
image = ImageIO.read(new File(fname));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println("Could not read in the pic");
System.exit(0);
}
}
public Dimension getPreferredSize() {
return new Dimension(w,h);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
}
public static void main(String[] args){
Grades1 d = new Grades1();
}
}