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();
	}
}

Recommended Answers

Use JList.getSelectedIndices() to get all of the selected indices.
Edit: Cross-posted. Use html in your label …

Jump to Post

Yes sorry, I managed to cross-post while you were posting that you'd figured it out.

Did you catch my edit about using HTML to create multiple lines in your label? You can use <br> to separate them.

Jump to Post

All 5 Replies

Never mind I got it...turns out there's a getSelectedIndices(); method!
int[] indices=list.getSelectedIndices();

Yayy and I figured it out all by myself :D

Now I'm trying to figure out how to make the JLabel that I'm changing skip down to the second line because it keeps going off the screen. \n isn't working, any help there?

Use JList.getSelectedIndices() to get all of the selected indices.
Edit: Cross-posted. Use html in your label to generate multiple lines.

commented: Thanks for replying :) +1

Thanks! I figured it out but thanks for your help and replying, it means a lot. :)

Yes sorry, I managed to cross-post while you were posting that you'd figured it out.

Did you catch my edit about using HTML to create multiple lines in your label? You can use <br> to separate them.

Ya thanks, now I'm just working on changing the font of my Jlabel.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.