I have a applet program which reads data from an xml file and puts the elements in a list of movie objects. It then puts it in a JTable which has a custom table model to handle the data as well as a renderer to draw the title of the movie name into the table cells. I originally placed it in a JFrame and it worked perfectly as show in the image below.
However when I place it in a class which extends JApplet and call the getContentPane method it appears as this.
cells appear as "no programmes available" as the custom renderer writes it when the String movieName is "null".
Here is the applet code
public class BackEndApplet extends JApplet{
private ArrayList<Channel> al;
public void init() {
setUp();
try{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createGUI();
}
});
} catch (Exception e){
System.err.println("Did not run successfully");
}
}
private void setUp(){
//parse xml file into Java
String fileName = "XMLFiles/bondFilms.xml";
MovieParser movieParser = new MovieParser();
movieParser.parseMovie(fileName);
//sort movie list to channel and sort by time
MovieChannelSorter mcSorter = new MovieChannelSorter();
mcSorter.sortMovieList(movieParser.getMovieList());
//retrieves channels from channel sorter
al = mcSorter.getChannels();
}
private void createGUI(){
ProgrammeGuidePanel gPane = new ProgrammeGuidePanel(al);
gPane.setOpaque(true);
setContentPane(gPane);
}
}
and this my Main Panel code:
public class ProgrammeGuidePanel extends JPanel{
private ArrayList <Channel> channels;
private String [] channelNames = {"Sean Connery",
"George Lazenby",
"Roger Moore",
"Timothy Dalton",
"Pierce Brosnan",
"Daniel Craig"};
private String [] pHeader = {"Slot 1","Slot 2","Slot 3","Slot 4"};
private CustomTModel customModel;
public ProgrammeGuidePanel(ArrayList <Channel> ch) {
super(new BorderLayout());
channels = ch;
//create title table
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Channels",channelNames);
JTable channelTable = new JTable(model);
channelTable.setRowSelectionAllowed(false);
//Create and fill Programme table
customModel = new CustomTModel(channels,pHeader);
JTable programmeTable = new JTable(customModel);
//set up panel for titles
JScrollPane scroller1 = new JScrollPane(channelTable);
scroller1.setMinimumSize(new Dimension(100,500));
scroller1.setPreferredSize(new Dimension(150,250));
//set up panel for movies
JScrollPane scroller2 = new JScrollPane(programmeTable);
//scroller1.setMinimumSize(new Dimension(100,100));
//scroller1.setPreferredSize(new Dimension(300,250));
//add scrollPanes to main panel
add(scroller1,BorderLayout.WEST);
add(scroller2,BorderLayout.CENTER);
}
}
I also tried using appletviewer in command line but it doesnt appear when i run the html file.
I'm completely stumped at why its doing it. So any help will be greatly appreciated.
UPDATE:
I may have figured out why its displaying the wrong data. In my Sax parser I was using
InputStream xmlInput = new FileInputStream(fileName);
I tried creating an executable jar in eclipse and got the results of picture 2. So I assume my parser class was returning a list full of empty objects since it couldn'f find the xml file. I did some research and saw I had to use
InputStream xmlInput = getClass().getResourceAsStream("file.xml");
However it keeps returning null when I run it i eclipse. I've looked into some of the same questions in stackoverflow but I cant seem to get my head around on how to implement getResourceAsStream()
. I've also used the getClassLoader()
method and setting an absolute path with "/" but to no avail.
Here's an SSCCE of my parser.
public class XMLParser {
public static void main(String [] args){
XMLParser x = new XMLParser();
x.parse();
}
public void parse(){
SAXParserFactory factory = SAXParserFactory.newInstance();
try{
InputStream xmlInput = getClass().getResourceAsStream("file.xml");
//InputStream xmlInput = new FileInputStream("file.xml");
SAXParser saxParser = factory.newSAXParser();
Handler handler = new Handler();
saxParser.parse(xmlInput,handler);
for(int i = 0;i<handler.plist.size();i++){
System.out.println(handler.plist.get(i));
}
} catch (Throwable err){
err.printStackTrace();
}
}
private class Handler extends DefaultHandler{
ArrayList<String>plist = new ArrayList<String>();
private String name;
private String lastName;
private boolean bname;
private boolean blname;
@Override
public void startElement(String uri,String localName, String qName,
Attributes attributes) throws SAXException{
System.out.println("end element: " + qName);
if (qName.equalsIgnoreCase("NAME")) {
bname = true;
}
if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("end element: " + qName);
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
if(bname){
name = new String(ch,start,length);
plist.add(name);
bname = false;
}
if(blname){
lastName = new String(ch,start,length);
plist.add(lastName);
blname = false;
}
}
}
}
Here's what the structure looks like in Eclipse