-1

I am trying to populate a GUI (included pic below) from a text file. The text file has 12 lines of text and is written like this : Matthew Smith;Australia;60,62,58,62,63,70;50,52,56,57,60,56. The aim is to have the names populate the jlist so when you select the name, the country will appear as a jlabel next to 'country' and the textfields will populate with the scores. I've been trying a few things but all I get is the java.Lang etc every time in the jList.. Can someone please point me in the right direction? Thanks very much

pic of GUI

private static Scanner inGui;


public Stage3() {
    initialize();
}
private void readFile() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("Stage3Scores.txt"));
    String line = "";
    int iScore = 0;
    while((line = reader.readLine()) != null) {
        String[] splitLine = line.split(";");
        athletes[iScore] = splitLine[0];
        countries[iScore] = splitLine[1];
        scores[iScore] = splitLine[2];
        iScore++;
    }
    reader.close();

    lblDisplayCountry = new JLabel("l");
    lblDisplayCountry.setBounds(101, 119, 186, 24);
    frame.getContentPane().add(lblDisplayCountry);

    listAthlete = new JList();
    listAthlete.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            //listAthlete.setText(athletes[listAthlete.getSelectedIndex()]);
            lblDisplayCountry.setText(athletes[listAthlete.getSelectedIndex()]);
        }
    });
    listAthlete.setBounds(101, 187, 186, 205);
    frame.getContentPane().add(listAthlete);

    JButton btnLoadAthlete = new JButton("Load");
    btnLoadAthlete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            DefaultListModel DLM = new DefaultListModel();
            DLM.addElement(""+ athletes +"");
            DLM.addElement(""+ countries +"");
            DLM.addElement("" + scores + "");
            listAthlete.setModel (DLM);

        }
    });
    btnLoadAthlete.setBounds(142, 422, 89, 23);
    frame.getContentPane().add(btnLoadAthlete);

    }
    }
Nobby
  • 1
  • 2
  • Welcome to Stack Overflow! Please include an [MCVE](http://stackoverflow.com/help/mcve) with your post (not an entire program or webpage). – intcreator Oct 18 '15 at 05:29
  • *"I've been trying a few things but all I get is the java.Lang etc every time in the jList."* That statement is in vague danger of conveying useful information.. 1) Always copy/paste error and exception output! 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Oct 18 '15 at 05:29
  • `frame.getContentPane().setLayout(null);` 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 18 '15 at 05:29
  • Noting there has been an edit but still no MCVE, am voting to close. – Andrew Thompson Oct 18 '15 at 06:52
  • thanks for not helping whatsoever. its been great. – Nobby Oct 18 '15 at 07:07

1 Answers1

1

The DefaultListModel is: public class DefaultListModel<E> extends AbstractListModel<E> so what you can do is either create a class like POJO and override toString method to show the Athlete Name and add the object to DefaultListModel otherwise only create the DLM as follows:

DefaultListModel<String> DLM = new DefaultListModel<String>();
for(int i = 0; i < athletes.length; i++)
  DLM.addElement(athletes[i]);
listAthlete.setModel (DLM);

Then on selection of the athlete name you can get the details from appropriate array with the index no of the list item selected.

Neenad
  • 861
  • 5
  • 19