Hi!

I have a problem with getting an arraylist to work within a JList. I have posted 5 classes; a GUI class - GrafikInl6, a class with most of the methods - Functionality, and then an abstract baseclass Runner6 with the two subclasses EliteRunner6 and Jogger6.

My problem is the method - showWinnerEliteRunners()in GrafikInl6. When pushing the button "Show eliterunner winner" the eliterunner with the best time should be shown. But this do not happen. I see that the arraylist doesn't seem to work - the arraylist do always have the size = 0. Even though I keep on adding new runners to the JList. But I can't figure out how to get the arraylist to start increase it's value.

Could some friendly soul maybe help me out here?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;


public class GrafikInl6 extends JFrame implements ActionListener
{	private ArrayList<Runner6> runners;
	private Funktionality funkJ;
	private Funktionality funkE;
	private Container contJ;
	private Container contE;
	private JList joggerList;
	private JList eliteRunnerList;
	private JTextField nameJField;
	private JTextField startNrJField;
	private JTextField timeJField;
	private JTextField tshirtJField;
	private JTextField nameEField;
	private JTextField startNrEField;
	private JTextField timeEField;
	private JTextField clubEField;
	private JTextField licenseEField;
	
	public static void main(String[] args)
	{
		GrafikInl6 frame = new GrafikInl6();
		frame.setVisible(true)	;
	}
	
	public GrafikInl6()
	{
		initiateInstanceVariables();
		buildFrame();
	}
	
	private void initiateInstanceVariables()
	{ this.runners = new ArrayList<Runner6>(20);
		funkJ = new Funktionality();
		funkE = new Funktionality();
		contJ = getContentPane();
		contE = getContentPane();
		nameJField = new JTextField();
		nameJField.setBorder(BorderFactory.createTitledBorder("Name"));
		startNrJField = new JTextField();
		startNrJField.setBorder(BorderFactory.createTitledBorder("Startnumber"));
		timeJField = new JTextField();
		timeJField.setBorder(BorderFactory.createTitledBorder("Time"));
		tshirtJField = new JTextField();
		tshirtJField.setBorder(BorderFactory.createTitledBorder("T-shirt registration"));
		joggerList = new JList(this.runners.toArray());
		joggerList.setBorder(BorderFactory.createTitledBorder("Registered jogger"));
	
		nameEField = new JTextField();
		nameEField.setBorder(BorderFactory.createTitledBorder("Name"));
		startNrEField = new JTextField();
		startNrEField.setBorder(BorderFactory.createTitledBorder("Startnumber"));
		timeEField = new JTextField();
		timeEField.setBorder(BorderFactory.createTitledBorder("Time"));
		clubEField = new JTextField();
		clubEField.setBorder(BorderFactory.createTitledBorder("Club"));
		licenseEField = new JTextField();
		licenseEField.setBorder(BorderFactory.createTitledBorder("Licensnumber"));
		eliteRunnerList = new JList(this.runners.toArray());
		eliteRunnerList.setBorder(BorderFactory.createTitledBorder("Registered eliterunner"));
	}
	
	private void buildFrame()
	{
		setTitle("Runners cup");
		setSize(1000,400);
		contJ.setLayout(new GridLayout(1,2));
		contE.setLayout(new GridLayout(1,2));
		JPanel leftPanel = new JPanel(new GridLayout(7,1));
		leftPanel.add(nameJField);
		leftPanel.add(startNrJField);
		leftPanel.add(timeJField);
		leftPanel.add(tshirtJField);
		
		JButton addButton = new JButton("Add jogger");
		addButton.addActionListener(this);
		leftPanel.add(addButton);
		
		JButton showButton = new JButton("Show jogger");
		showButton.addActionListener(this);
		leftPanel.add(showButton);
		
		JButton clearButton = new JButton("Clear");
		clearButton.addActionListener(this);
		leftPanel.add(clearButton);
		contJ.add(leftPanel);
		contJ.add(joggerList);
		
		buildMenu();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	
		JPanel rightPanel = new JPanel(new GridLayout(9,1));
		rightPanel.add(nameEField);
		rightPanel.add(startNrEField);
		rightPanel.add(timeEField);
	    rightPanel.add(clubEField);
		rightPanel.add(licenseEField);
		
		JButton addButtonE = new JButton("Add eliterunner");
		addButtonE.addActionListener(this);
		rightPanel.add(addButtonE);
		
		JButton showButtonE = new JButton("Show eliterunner");
		showButtonE.addActionListener(this);
		rightPanel.add(showButtonE);
		
		JButton winButtonE = new JButton("Show eliterunner winner");
		winButtonE.addActionListener(this);
		rightPanel.add(winButtonE);
		
		JButton clearButtonE = new JButton("Clear");
		clearButtonE.addActionListener(this);
		rightPanel.add(clearButtonE);
		contE.add(rightPanel);
		
		contE.add(eliteRunnerList);
	}
	
	private void buildMenu()
	{
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Archive");
		menuBar.add(menu);
		JMenuItem menuItem = new JMenuItem("Add runner");
		menu.add(menuItem);
		menuItem.addActionListener(this);
		menuItem = new JMenuItem("End");
		menu.add(menuItem);
		menuItem.addActionListener(this);
		setJMenuBar(menuBar);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String theText = event.getActionCommand();
		if(theText.equals("Add jogger"))
		{
			addJogger();
		}
		if(theText.equals("Show jogger"))
		{
			showSelectedJoggers();
		}
		if(theText.equals("Add eliterunner"))
		{
			addEliteRunner();
		}
		if(theText.equals("Show eliterunner"))
		{
			showSelectedEliteRunners();
		}
		if(theText.equals("Show eliterunner winner"))
		{
			showWinnerEliteRunners();
		}
		if(theText.equals("Rensa"))
		{
			clear();
		}
		if (theText.equals("Add jogger"))
		{
			addJogger();
		}
		if (theText.equals("Add eliterunner"))
		{
			addEliteRunner();
		}
		if (theText.equals("End"))
		{
			System.exit(0);
		}
	}
	
	private void addJogger()
	{
		funkJ.addJogger(nameJField.getText(), startNrJField.getText(), timeJField.getText(), tshirtJField.getText());
		joggerList.setListData(funkJ.getAllJoggers().toArray());
		clear();
	}
	
	private void addEliteRunner()
	{
		funkE.addEliteRunner(nameEField.getText(), startNrEField.getText(), timeEField.getText(), clubEField.getText(), licenseEField.getText());
		eliteRunnerList.setListData(funkE.getAllEliteRunners().toArray());
		clear();
	}
	
	private void showSelectedJoggers()
	{
		int position = joggerList.getSelectedIndex();
		if(position > -1)
		{
			nameJField.setText(funkJ.getNameForJoggerAt(position));
			startNrJField.setText(funkJ.getStartNrForJoggerAt(position));
			timeJField.setText(funkJ.getTimeForJoggerAt(position));
			tshirtJField.setText(funkJ.getTshirtRegForJoggerAt(position));
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Du måste markera en person i listan!");
		}
	}
	
	private void showSelectedEliteRunners()
	{
		int position = eliteRunnerList.getSelectedIndex();
		if(position > -1)
		{
			nameEField.setText(funkE.getNameForEliteRunnerAt(position));
			startNrEField.setText(funkE.getStartNrForEliteRunnerAt(position));
			timeEField.setText(funkE.getTimeForEliteRunnerAt(position));
			clubEField.setText(funkE.getClubForEliteRunnerAt(position));
			licenseEField.setText(funkE.getLicenseForEliteRunnerAt(position));
			
				
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Du måste markera en person i listan!");
		}
		
		
	}
	
	private void showWinnerEliteRunners()
	{
		int minIndex = funkE.findMinIndex(runners);
			nameEField.setText(funkE.getNameForEliteRunnerAt(minIndex));
			startNrEField.setText(funkE.getStartNrForEliteRunnerAt(minIndex));
			timeEField.setText(funkE.getTimeForEliteRunnerAt(minIndex));
			clubEField.setText(funkE.getClubForEliteRunnerAt(minIndex));
			licenseEField.setText(funkE.getLicenseForEliteRunnerAt(minIndex));
	
		
	}
	private void clear()
	{
		nameJField.setText("");
		startNrJField.setText("");
		timeJField.setText("");
		tshirtJField.setText("");
		nameEField.setText("");
		startNrEField.setText("");
		timeEField.setText("");
		clubEField.setText("");
		licenseEField.setText("");
	}
}
import java.util.ArrayList;

public class Funktionality
{
	private ArrayList<Runner6> runners;
	private String startNumber;
	private String name;
	private String time;
	
	public Funktionality()
	{
		this.runners = new ArrayList<Runner6>(20);
	}
	
	public void addJogger(  String name,String startNumber, String time, String order)
	{
		this.runners.add(new Jogger6( name, startNumber, time, order));
	}
	public ArrayList<Runner6> getAllJoggers()
	{
		return this.runners;
	}
	public void addEliteRunner( String name,String startNumber,  String time, String club, String licenseNumber)
	{
		this.runners.add(new EliteRunner6(name,startNumber,  time, club, licenseNumber));
	}
	public ArrayList<Runner6> getAllEliteRunners()
	{
		return this.runners;
	}
	public int findMinIndex(ArrayList<Runner6> runners)
	   {
	   		int minIndex = 0;
	   		
				for (int  i = 0; i < this.runners.size() ; i++)
				{
					if (this.runners.get(i) instanceof EliteRunner6 )
					{
						String text1 = getTimeForEliteRunnerAt(i);
						String term1 = "";	
						double nr1 = 0;
						term1 = text1.substring(0, text1.length());
						nr1 = Double.parseDouble(term1);
						
			
		
						String text2 = getTimeForEliteRunnerAt(minIndex);
						String term2 = "";	
						double nr2 = 0;
						term2 = text2.substring(0, text2.length());
						nr2= Double.parseDouble(term2);	
			
						if (nr1 < nr2)
						{
								minIndex = i;
						}
					}
	   			}
			return minIndex;
	  }
	public String getNameForJoggerAt(int position)
	{
		return (this.runners.get(position)).getName();
	}
	
	public String getStartNrForJoggerAt(int position)
	{
		return (this.runners.get(position)).getStartNumber();
	}
	
	public String getTimeForJoggerAt(int position)
	{
		return (this.runners.get(position)).getTime();
	}
	
	public String getTshirtRegForJoggerAt(int position)
	{
		return ((Jogger6)this.runners.get(position)).getOrder();
	}
	
	public String getNameForEliteRunnerAt(int position)
	{
		return (this.runners.get(position)).getName();
	}
	
	public String getStartNrForEliteRunnerAt(int position)
	{
		return (this.runners.get(position)).getStartNumber();
	}
	
	public String getTimeForEliteRunnerAt(int position)
	{
		return (this.runners.get(position)).getTime();
	}
	
	public String getClubForEliteRunnerAt(int position)
	{
		return ((EliteRunner6)this.runners.get(position)).getClub();
	}
	
	public String getLicenseForEliteRunnerAt(int position)
	{
		return ((EliteRunner6)this.runners.get(position)).getLicenseNumber();
	}
	
}
public abstract class Runner6
{
	// instancevariables
	private String startNumber;
	private String name;
	private String time;
	
	// construktor
	
	public Runner6( String name, String startNumber,String time)
	{
		this.name = name;
		this.startNumber = startNumber;
		this.time = time;
	}
	
	// methods
	public String getStartNumber()
	{
		return this.startNumber;
	}
	
	public String getTime()
	{
		return this.time;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public void setStartNumber(String startNumber)
	{
		this.startNumber = startNumber;
	}
	
	public void setTime(String time)
	{
		this.time = time;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public String toString()
	{
		return this.name + " " + this.startNumber + " " + this.time + " " + toStringSpec();	
	}
	
	public abstract String toStringSpec();
}
public class Jogger6 extends Runner6  // Jogger6 ärver från Runner6
{
	// instansvariabler
	private String order;
		
	public Jogger6( String name, String startNumber,String time, String order)
	{
		super(name,startNumber,  time); // anrop till basklassens konstruktor 
		this.order = order;
	}
	
	// metoder
	public void setOrder(String order)
	{
		this.order = order;
	}
	
	public String getOrder()
	{
		return this.order;
	}
	
	public String toStringSpec()
	{
		return this.order + " ";
	}
}
public class EliteRunner6 extends Runner6  // EliteRunner ärver från Runner
{
	// instansvariabler
	private String club;
	private String licenseNumber;
	
	public EliteRunner6(String name, String startNumber,  String time, String club, String licenseNumber)
	{
		super( name, startNumber,  time); // anrop till basklassens konstruktor 
		this.club = club;
		this.licenseNumber = licenseNumber;
	}
	
	// metoder
	public String getClub()
	{
		return this.club;
	}
	
	public void setClub(String club)
	{
		this.club = club;
	}
	public String getLicenseNumber()
	{
		return this.licenseNumber;
	}
	
	public void setLicenseNumber(String licenseNumber)
	{
		this.licenseNumber = licenseNumber;
	}
	
	public String toStringSpec()
	{
		return this.club + " " + this.licenseNumber;
	}
}

Sounds like you could have two definitions using the same name. One local and one global. You update the local one. The global one never changes.

Be a part of the DaniWeb community

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