I think you are almost there.
You do not have re-intialise your list everytime.
Once you parse the model you have to create a scene(Model) based on the xml
Each scene will have list of choices(list of scene objects)
On confirming the list selection Pass the scene pertaining to the choice to load.
Hope the below code helps you.
The Scene Model
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
*
* @author user
*/
public class Scene {
private String id;
private String descrition;
List choices = new ArrayList();
@Override
public int hashCode() {
int hash = 3;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Scene other = (Scene) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
public Scene(String id, String descrition) {
this.id = id;
this.descrition = descrition;
}
public List getChoices() {
return choices;
}
public String getId() {
return id;
}
public String getDescrition() {
return descrition;
}
}
The Choice Model
public class Choice {
String id;
String description;
String outSceneId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getOutSceneId() {
return outSceneId;
}
public void setOutSceneId(String outSceneId) {
this.outSceneId = outSceneId;
}
@Override
public String toString() {
return "Choice{" + "description=" + description + '}';
}
}
And the Frame which displays the scenes
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.io.*;
import java.util.*;
import javax.swing.DefaultListModel;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class TextAdventureGUI extends JFrame implements ActionListener {
private JPanel contentPane;
/**
* @wbp.nonvisual location=-20,79
*/
private final JPanel panel = new JPanel();
private static JList list;
JScrollPane scrollPane;
private final JTextPane txtpn = new JTextPane();
private final JScrollPane scrollPane_1 = new JScrollPane();
private final JButton Button = new JButton("Confirm");
private final DefaultListModel model = new DefaultListModel();
private Map scenesMap = new HashMap();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextAdventureGUI storyBoard = new TextAdventureGUI();
File inputFile = new File("input");
SAXBuilder saxB = new SAXBuilder();
Document doc = saxB.build(inputFile);
Element storyElement = doc.getRootElement();
Scene firstScene = null;
List scenesList = storyElement.getChildren();
for (Element sceneElement : scenesList) {
Scene scene = storyBoard.buildScene(sceneElement);
storyBoard.getScenesMap().put(scene.getId(), scene);
if (firstScene == null) {
firstScene = scene;
}
}
storyBoard.initScene(firstScene);
storyBoard.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private Scene buildScene(Element sceneElement) {
Scene scene = null;
String id = sceneElement.getAttributeValue("id");
if (id != null) {
String sceneDescription = sceneElement.getChild("SceneDescription").getText();
scene = new Scene(id, sceneDescription);
for (Element element : sceneElement.getChildren()) {
if (element.getName().equals("choice")) {
scene.getChoices().add(this.buildChoice(element));
}
}
}
return scene;
}
private Choice buildChoice(Element choiceElement) {
Choice choice = null;
String id = choiceElement.getAttributeValue("no");
if (null != id) {
choice = new Choice();
choice.setId(id);
choice.setDescription(choiceElement.getChildText("choiceDescription"));
choice.setOutSceneId(choiceElement.getChildText("outcome"));
}
return choice;
}
public TextAdventureGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 482, 311);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(5, 0));
//String[] array = null;
list = new JList();
list.setModel(model);
scrollPane = new JScrollPane(list);
contentPane.add(scrollPane_1);
scrollPane_1.setViewportView(txtpn);
txtpn.setEditable(false);
Button.addActionListener(this);
contentPane.add(scrollPane);
scrollPane.setViewportView(list);
contentPane.add(Button);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Confirm"));
{
Choice choice = (Choice) list.getSelectedValue();
Scene outCome = scenesMap.get(choice.getOutSceneId());
if (outCome != null) {
initScene(outCome);
}
}
}
public Map getScenesMap() {
return scenesMap;
}
public void initScene(Scene scene) {
txtpn.setText(scene.getDescrition());
model.clear();
for (Choice choice : scene.getChoices()) {
model.addElement(choice);
}
}
}