Get my JList to print my toString

I got the code below and it works but right now it just prints out my stuff to the conslole. How can I change so the output comes in my JList instead? Also How do I clean the JList before any output is done?
package p3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*; 
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;


public class LopareGUI extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private List<Lopare> lopareLista = new ArrayList<Lopare>();
	private JList output; 
	private JScrollPane scrollPane; 
	private JMenuBar menuBar; 
	private JMenu menu; 
	private JMenuItem menuItem;      


	public JMenuBar createMenuBar() {         

		menuBar = new JMenuBar(); 

		menu = new JMenu("Menu"); 
		menu.setMnemonic(KeyEvent.VK_M); 
		menu.getAccessibleContext().setAccessibleDescription("Control the program"); 
		menuBar.add(menu); 

		menuItem = new JMenuItem("Add New", KeyEvent.VK_A);         
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); 
		menuItem.getAccessibleContext().setAccessibleDescription("Add a New Runner to the program"); 
		menu.add(menuItem); 
		menuItem.addActionListener( new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				AddDialog dialog = new AddDialog(LopareGUI.this, lopareLista);        
				dialog.setVisible(true);   
			}
		}) ;

		menuItem = new JMenuItem("Show Runner", KeyEvent.VK_S);         
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK)); 
		menuItem.getAccessibleContext().setAccessibleDescription("Show Runner"); 
		menu.add(menuItem); 
		menuItem.addActionListener( new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				ShowDialog showdialog = new ShowDialog(LopareGUI.this);        
				showdialog.setVisible(true); 
				if( showdialog.getSelection() == ShowDialog.OPTION_ALL )
				{
					Lopare.showAll(lopareLista);
				}
				
				else if ( showdialog.getSelection() == ShowDialog.OPTION_WINNER )
				{
					Lopare.showWinner(lopareLista);
				}
				
				else if( showdialog.getSelection() == ShowDialog.OPTION_ELITE )
				{
					Elite elite = new Elite();
					elite.showElite(lopareLista);
				}
				
				else if( showdialog.getSelection() == ShowDialog.OPTION_MOTION )
				{					
					Motion motion = new Motion();
					motion.showMotion(lopareLista);
				}
			}
		}) ;

		menuItem = new JMenuItem("Exit", KeyEvent.VK_E);         
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); 
		menuItem.getAccessibleContext().setAccessibleDescription("Exit program"); 
		menu.add(menuItem); 
		menuItem.addActionListener( new ActionListener(){
			public void actionPerformed( ActionEvent ae ){
				int option = JOptionPane.showConfirmDialog( LopareGUI.this, "Are you sure you want to close?" ) ;

				if( option == JOptionPane.YES_OPTION ){
					dispose() ;
				}
			}
		}) ;

		menu = new JMenu("Help"); 
		menu.setMnemonic(KeyEvent.VK_N); 
		menu.getAccessibleContext().setAccessibleDescription("This menu does nothing yet"); 
		menuBar.add(menu); 

		return menuBar; 
	} 



	public Container createContentPane() { 

		JPanel contentPane = new JPanel(new BorderLayout()); 
		contentPane.setOpaque(true); 
		output = new JList(); 
		scrollPane = new JScrollPane(output);
		contentPane.add(scrollPane, BorderLayout.CENTER); 
		return contentPane; 
	} 




	private void createAndShowGUI() { 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setJMenuBar(createMenuBar()); 
		setContentPane(createContentPane()); 
		setSize(450, 260); 
		setVisible(true); 
	} 



	public static void main(String[] args) { 

		javax.swing.SwingUtilities.invokeLater(new Runnable() { 
			public void run()  
			{ 
				new LopareGUI().createAndShowGUI(); 
			} 
		}); 
	} 
}


-----------------------------

package p3;

import java.util.Collections;
import java.util.List;



public abstract class Lopare implements Comparable<Object> {

	//privat deklaration
	private String namn;
	private Integer tid;
	private Integer startnummer;


	/**
	 * getNamn
	 * @return
	 */
	public String getNamn() {
		return namn;
	}

	/**
	 * setNamn
	 * @param namn
	 */
	public void setNamn(String namn) {
		this.namn = namn;
	}


	/**
	 * getTid
	 * @return
	 */
	public Integer getTid() {
		return tid;
	}


	/**
	 * setTid
	 * @param tid
	 */
	public void setTid(Integer tid) {
		this.tid = tid;
	}


	/**
	 * getStartnummer
	 * @return
	 */
	public Integer getStartnummer() {
		return startnummer;
	}


	/**
	 * setStartnummer
	 * @param startnummer
	 */
	public void setStartnummer(Integer startnummer) {
		this.startnummer = startnummer;
	}


	/**
	 * showAll
	 * @param lista
	 */
	public static void showAll(List<Lopare> lista){
		for(int i = 0; i < lista.size(); i++)
			System.out.println(lista.get(i).toString());
	}


	/**
	 * showWinner
	 * @param lista
	 */
	public static void showWinner(List<Lopare> lista){

		Collections.sort( lista ) ;

		for(int i = 0; i < lista.size(); i++)
		{
			if(lista.get(i) instanceof Elite)
			{
				System.out.println(lista.get(i));
				break;
			}
		}
	}	



	/**
	 * toString
	 */
	@Override
	public String toString() {
		return "\nNamn: " +getNamn() +"\nTid: " +getTid() +" minuter\nStartnummer: " +getStartnummer(); 
	}


	/**
	 * addMotion
	 * @param motion
	 */
	public abstract void addMotion(Motion motion);

	/**
	 * addElite
	 * @param elite
	 */
	public abstract void addElite(Elite elite);

	/**
	 * showMotion
	 * @param lista
	 */
	public abstract void showMotion(List<Lopare> lista);

	/**
	 * showElite
	 * @param lista
	 */
	public abstract void showElite(List<Lopare> lista);		


	public int compareTo(Object anotherLopare) throws ClassCastException {
		if (!(anotherLopare instanceof Lopare))
			throw new ClassCastException("A Lopare object expected.");
		int anothertid = ((Lopare) anotherLopare).getTid();  
		return this.tid - anothertid;    
	}


}

Open in new window

MickeysAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>       private JList output;

instead of keeping a reference to the List you should keep a reference to the model used by the list

      private DefaultListModel outputModel;


to change the list then just requires you to update the model

eg. to clear

outputModel.clear();
0
 
CEHJCommented:
If you want to keep them separate you can just do
output.setModel(new DefaultComboBoxModel(new Vector(lopareLista)));

Open in new window

0
 
objectsConnect With a Mentor Commented:
>             output = new JList();

that would become

Jlist             output = new JList(outputModel);
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
objectsCommented:
some good background reading on using lists here
http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
0
 
MickeysAuthor Commented:
I have read the page and I have also added what you have told me.

Then how to I get my toString to come into my LIst instead of showing in the console?
0
 
objectsConnect With a Mentor Commented:
outputModel.addElement(whatever);
0
 
MickeysAuthor Commented:
well
this is what I do
else if ( showdialog.getSelection() == ShowDialog.OPTION_WINNER )
                        {
Lopare.showWinner(lopareLista);
                        }

this is what I try to do and wont work
else if ( showdialog.getSelection() == ShowDialog.OPTION_WINNER )
                        {
                              outputModel.addElement(Lopare.showWinner(lopareLista));
                        }
0
 
objectsConnect With a Mentor Commented:
change your showWinner() method to instead *return* the winner

      public static Lopare getWinner(List lista){
0
 
MickeysAuthor Commented:
Then it raises two new questions.

How do I split the output. Everthing will come out like in a row. afsafdsafasdasdfasfdasfdasfas

and also if I print alist all will come on a single row.......I wanted each object on each line




if( showdialog.getSelection() == ShowDialog.OPTION_ALL )
				{
					outputModel.clear();
					outputModel.addElement(lopareLista);
				}
				
				else if ( showdialog.getSelection() == ShowDialog.OPTION_WINNER )
				{
					if(Lopare.showWinner(lopareLista) != null)
					{
						outputModel.clear();
						outputModel.addElement(Lopare.showWinner(lopareLista));
					}
				}

Open in new window

0
 
objectsCommented:
you just use addElement to add a single element
to add a list of objects you would loop and add them one at a time

0
 
MickeysAuthor Commented:
Ok.
But take for example showElite. (see code below).

We are asked not to lift out anything from elite class and just have gui in the gui class. As I see it I must lift out the print thing into my gui Or else I cant make it work? Or do I save it to an elite lista and return elite list and print that out?
if( showdialog.getSelection() == ShowDialog.OPTION_ALL )
				{
					outputModel.clear();
					for(int i = 0; i < lopareLista.size(); i++) 
					{    
						outputModel.addElement(lopareLista.get(i));
					}					
				}
				
				else if ( showdialog.getSelection() == ShowDialog.OPTION_WINNER )
				{
					if(Lopare.showWinner(lopareLista) != null)
					{
						outputModel.clear();
						outputModel.addElement(Lopare.showWinner(lopareLista));						
					}
					
					else
						JOptionPane.showMessageDialog(null, "No winner");
				}
				
				else if( showdialog.getSelection() == ShowDialog.OPTION_ELITE )
				{
					Elite elite = new Elite();
					elite.showElite(lopareLista);
				}
				
				else if( showdialog.getSelection() == ShowDialog.OPTION_MOTION )
				{					
					Motion motion = new Motion();
					motion.showMotion(lopareLista);
				}


----------------------------------

/**
	 * showElite
	 */
	@Override
	public void showElite(List<Lopare> lista) {
		for(int i = 0; i < lista.size(); i++)
		{				
			if(lista.get(i) instanceof Elite)
			{
				System.out.println(lista.get(i).toString());
			} //end if				
		} //end for		
	}

Open in new window

0
 
objectsConnect With a Mentor Commented:
yes it should return a list of Elite
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.