Not able to add XML content to existing JEditorPane



I am currently working on a Java application and the aim is to design a simple interface for asking some questions on Java. So far I have programmed a JEditorPane and a button.


I am using sax to read xml data stored in a folder. I am able to retrieve the the content of the xml and display it in the console log and it prints as expected, however when I try to set this to the JEditorPane (in SetXml class), I fail miserably. The idea is to click on a button and display the xml output to a JEdiorPane. I have spent hours on this and cannot seem to work out what the issue is. I have pasted the code below. I would very much appreciate if anyone could guide me of how to resolve this.


Controller.java



public class Controller {
static JButton btnFirst = new JButton("First Question");
static SetXml xmlObj = new SetXml();

public static void main (String[] args)
{
JFrame frame = new JFrame();
MainWin mainObj = new MainWin();

frame.setSize(500, 500);
frame.add(mainObj.mainWin());
frame.add(btnFirst, BorderLayout.WEST);
btnFirst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
xmlObj.readXml();
}
});
frame.setVisible(true);
}}


MainWin.java



public class MainWin {

JPanel qPanel = new JPanel();
questionWin qObj = new questionWin();

public JComponent mainWin()
{
qPanel.setLayout(new BorderLayout());
qPanel.add(qObj.questionWindow(), BorderLayout.CENTER);
return qPanel;

}}


questionWin.java



public class questionWin {

JEditorPane editorPane = new JEditorPane();
JPanel panel = new JPanel();
/**
* @wbp.parser.entryPoint
*/
public JComponent questionWindow() {

GroupLayout groupLayout = new GroupLayout(panel);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(36)
.addComponent(editorPane, GroupLayout.PREFERRED_SIZE, 357, GroupLayout.PREFERRED_SIZE)
.addContainerGap(41, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(26)
.addComponent(editorPane, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
.addContainerGap(129, Short.MAX_VALUE))
);
panel.setLayout(groupLayout);
return panel;
}}


SetXml.java



public class SetXml
{
questionWin questWinObj = new questionWin();

public JComponent readXml()
{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("Questions/q1.xml");
NodeList questionList = doc.getElementsByTagName("Question");
for(int i=0; i<questionList.getLength();i++)
{
Node q = questionList.item(i);
Element a = (Element)q;
questWinObj.editorPane.setText(a.getTextContent());
System.out.print(a.getTextContent());
}
}

catch (ParserConfigurationException e)
{
e.printStackTrace();
}

catch (SAXException e)
{

e.printStackTrace();
} catch (IOException e)
{

e.printStackTrace();
}

return null;

}}

No comments:

Post a Comment