Results 1 to 12 of 12
  1. #1
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default jdom question , show xml

    i have two problem with code , can't see the first question , when i compile the program , and also can't make the layout like boxlayout,
    could you guys please help me with it?

    Thanks


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    
    
    public class Main extends JPanel  {
    
        public Main() {
    
      super(new FlowLayout());
     final JTextArea jt= new JTextArea("  ",5,5);
    
    Box hb = Box.createHorizontalBox();
      
    
    final JLabel aaa = new JLabel();
     
        SAXBuilder builder = new SAXBuilder();
        	File xmlFile = new File("fileq.xml");
    
            try{
    
        	   Document document = (Document) builder.build(xmlFile);
               Element rootNode = document.getRootElement();
               List list = rootNode.getChildren("question");
    
               for (int i=0; i< list.size(); i++)
               {
                 Element node = (Element) list.get(i);
    
    
              jt.append("question : " + node.getChildText("quest") + "\n");
    
               aaa.setText("question : " + node.getChildText("quest") + "\n");
    JRadioButton ans1 = new JRadioButton("answer : " + node.getChildText("answerincorrect1") + "\n");
    JRadioButton ans2 = new JRadioButton("answer : " + node.getChildText("answerincorrect") + "\n");
    JRadioButton ans3 = new JRadioButton("answer : " + node.getChildText("answercorrect") + "\n");
    
    add(aaa);
    ButtonGroup group = new ButtonGroup();
            group.add(ans1);
            group.add(ans2);
            group.add(ans3);
    
            //hb.add(aaa);
           add(ans1);
    add(ans2);
    add(ans3);
    
               }
    
        	 }catch(IOException io){
        		System.out.println(io.getMessage());
        	 }catch(JDOMException jdomex){
        		System.out.println(jdomex.getMessage());
        	}
    
    
    
     add(jt);
    
    
        }
    
    
    
    
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TextDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add contents to the window.
          frame.add(new Main());
    
    
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
    
              frame.setLocationRelativeTo(null);
    
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
     }
    my xml file
    Java Code:
    <?xml version="1.0" ?> 
    <file>
    <question>
    <quest>who is the best football player?</quest> 
    <answercorrect>messi</answercorrect> 
    <answerincorrect>ronaldo</answerincorrect> 
    <answerincorrect1>kaka</answerincorrect1> 
    </question>
    <question>
    <quest>who is the best football club?</quest> 
    <answercorrect>manchester united</answercorrect> 
    <answerincorrect>real madrid</answerincorrect> 
    <answerincorrect1>inter milan</answerincorrect1> 
    </question>
    </file>

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  3. #3
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default and how to display the first xml question?

    and how to display the first xml question?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you get the layout right then you should be able to see all you are displaying.
    You are probably adding a compoent on top of the component displaying the first text.
    Read about the layout managers and all should be clearer.

  5. #5
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default no , the xml display have nothing to do with layout

    coz it work when i use JTextArea

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pipikrk View Post
    coz it work when i use JTextArea
    So all the information from your xml file is there; as r035198x already wrote: get your Swing components right and you're in business.

    kind regards,

    Jos

  7. #7
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default if i would know , i wouldn't ask help

    that why i kind request your help, coz i am not able to fix it on my own

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Are you also unable to read the text on the link I posted?

  9. #9
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default i had correct the layout

    the problem is here i think,
    aaa.setText("question : " + node.getChildText("quest") + "\n");

    because if i change the xml file to
    Java Code:
    <?xml version="1.0" ?> 
    <file>
    <question>
    <quest>who is the best football player?</quest> 
    <answercorrect>messi</answercorrect> 
    <answerincorrect>ronaldo</answerincorrect> 
    <answerincorrect1>kaka</answerincorrect1> 
    </question>
    <question>
    <quest>who is the best football club?</quest> 
    <answercorrect>manchester united</answercorrect> 
    <answerincorrect>real madrid</answerincorrect> 
    <answerincorrect1>inter milan</answerincorrect1> 
    </question>
    <question>
    <quest>who will win the world cup?</quest> 
    <answercorrect>Brasil</answercorrect> 
    <answerincorrect>Spain</answerincorrect> 
    <answerincorrect1>Italy</answerincorrect1> 
    </question>
    </file>
    it only display the last question" who will win the world cup?"

    the other 2 questions never appear

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Because every time you add a component on top of the previous one. That's why you only see the last one that you added.

    Read about layout managers. It won't make your eyes fall off.

  11. #11
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    jt.append("question : " + node.getChildText("quest") + "\n");
    when i use this , the problem doesn't exist

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Read about layout managers. It won't make your eyes fall off.
    Much worse ... here, this is what happened to an arbitrary reader.

    Be warned: don't read!

    kind regards,

    Jos

Similar Threads

  1. extract text from xhtml page using jdom
    By nijil in forum New To Java
    Replies: 7
    Last Post: 02-23-2010, 07:50 PM
  2. Replies: 4
    Last Post: 02-18-2010, 04:24 PM
  3. XML JDOM XPATH NumberFormatException
    By Jazzperson in forum Advanced Java
    Replies: 3
    Last Post: 03-08-2009, 08:15 PM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM
  5. JDOM in java applications
    By boy22 in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •