我有一个applet程序,它从xml文件中读取数据并将这些元素放在一个电影对象列表中。然后将它放在一个JTable中,它有一个自定义表模型来处理数据,以及一个渲染器,用于将电影名称的标题绘制到表格单元格中。我最初把它放在一个JFrame中,它完美地工作如下图所示。
但是,当我将它放在扩展JApplet并调用getContentPane方法的类中时,它会显示为此。
单元格显示为“没有程序可用”,因为自定义渲染器在String movieName为“null”时写入它。
这是小程序代码
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);
}
}
这是我的主要面板代码:
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);
}
}
我也尝试在命令行中使用appletviewer,但是当我运行html文件时它不会出现。
我完全不知道为什么这样做。所以任何帮助将不胜感激。
更新:
我可能已经弄明白为什么它显示错误的数据。在我的Sax解析器中,我正在使用
InputStream xmlInput = new FileInputStream(fileName);
我尝试在eclipse中创建一个可执行jar并得到了图2的结果。所以我假设我的解析器类返回一个充满空对象的列表,因为它找不到xml文件。我做了一些研究,看到我必须使用
InputStream xmlInput = getClass().getResourceAsStream("file.xml");
然而,当我运行它时它会继续返回null。我在stackoverflow中研究了一些相同的问题,但我似乎无法理解如何实现getResourceAsStream()
。我还使用了getClassLoader()
方法并使用“/”设置了绝对路径,但无济于事。
这是我的解析器的SSCCE。
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;
}
}
}
}
这是Eclipse中结构的样子