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

    Default xml question

    i have the radio button, but i want to have in the radio button , the xml answer

    instead of answer1 having answer:messi

    and then something to tell me if my answer was correct or not

    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 GridLayout());
     final JTextArea jt= new JTextArea(" som ",5,20);
    
    
    
        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");
               jt.append("answer : " + node.getChildText("answercorrect") + "\n");
               jt.append("answer : " + node.getChildText("answerincorrect") + "\n");
               jt.append("answer : " + node.getChildText("answerincorrect") + "\n");
    
               }
    
        	 }catch(IOException io){
        		System.out.println(io.getMessage());
        	 }catch(JDOMException jdomex){
        		System.out.println(jdomex.getMessage());
        	}
    
    
    JRadioButton ans1 = new JRadioButton("answer1");
    JRadioButton ans2 = new JRadioButton("answer2");
    JRadioButton ans3 = new JRadioButton("answer3");
            ButtonGroup group = new ButtonGroup();
            group.add(ans1);
            group.add(ans2);
            group.add(ans3);
    
    add(ans1);
    add(ans2);
    add(ans3);
    
    
     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();
                }
            });
        }
    
     }
    Java Code:
      <?xml version="1.0" ?> 
    - <file>
    - <question>
      <quest>who is the best football player?</quest> 
      <answercorrect>messi</answercorrect> 
      <answerincorrect>ronaldo</answerincorrect> 
      <answerincorrect>kaka</answerincorrect> 
      </question>
      </file>

  2. #22
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by pipikrk View Post
    instead of answer1 having answer:messi
    then why don't your build your JButton with JRadioButton ans1 = new JRadioButton("messi");?

    other question: 1) will you always have questions with 3 answers? is this a game where you have to calculate the score of all answers?

    again: could you please specify first what you need?
    Last edited by j2me64; 04-15-2010 at 12:19 PM.

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

    Default i need validate

    yes, i do need validate the answers

    1) it will be a quizz when i have to validate the answers , and maybe give some score
    so i was thinking it read the data from xml file

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So what you really want to do is not to read in an XML and display the XML as XML in a JTextArea, but rather read and parse an XML file and then use the data obtained to display a quiz in some GUI format. The key I think will be parsing the XML file, and several options are available from JAXB which may be overkill, DOM parsing which would work but could use up a lot of memory if the XML file ever gets large to StAX which seems like possibly a good option. You can read about some of this here: The Java(TM) Web Services Tutorial

    If I were you, I'd create a class called Question that held the question String, the right answer String and the List<String> of incorrect answers, give it a constructor that would allow it to accept the answer and question Strings, perhaps an add(String str) method to allow you to add incorrect answers, a method say called List<String> getPossibleAnswers() that returned a String list in randomized order of the correct answer string and incorrect answer strings (mixed together) a boolean checkAnswer(String possibleAnswer) method, etc....

    Then I'd create a JPanel say called GuiQuestion that could display questions. It could have a setQuestion(Question question) method that on getting passed a question could display the question in a JLabel and the possible answers as JRadioButtons, and give this JPanel a method to validate that would check if the selected JRadioButton holds the String that matches the Question object's correct answer String, and then incorporate this JPanel into your GUI. The key is to think in an OOPS way and subdivide your problem to solve it.

    Edit: I see that you're using JDOM to read your XML, and this is fine if it works for you.
    Last edited by Fubarable; 04-15-2010 at 01:01 PM.

  5. #25
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    If I were you ...

    good ideas. @pipikrk: do you think you can code what Fubarable suggests? if you nedd help let me know.

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

    Default thx

    thx for all u advises , i will give try to it , but it won't be easy...

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

    Default after i try , still have some problems

    i have problem to convert the xml in an string and stored it,

    If I were you, I'd create a class called Question that held the question String, the right answer String and the List<String> of incorrect answers,

    how can i stored the data , that i parse in strings for later use?


    here i display the xml , but i would like to store it into strings , how?
    Java Code:
    for (int i=0; i< list.size(); i++)
               {
                 Element node = (Element) list.get(i);
    
                   
               jt.append("question : " + node.getChildText("quest") + "\n");
               jt.append("answer : " + node.getChildText("answercorrect") + "\n");
               jt.append("answer : " + node.getChildText("answerincorrect") + "\n");
               jt.append("answer : " + node.getChildText("answerincorrect") + "\n");
    
               }
    
        	 }catch(IOException io){
        		System.out.println(io.getMessage());
        	 }catch(JDOMException jdomex){
        		System.out.println(jdomex.getMessage());
        	}

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please check my reply above. What you want (I think) is a way to store the information held in the XML in some logical fashion so that it can be used by your program however it desires, and the key to doing this is to create classes/objects to hold and organize the information, again, as I discussed above. How are you progressing on this?

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

    Default

    i have understand you sugestion, but for create the classes and the methods,
    first i need to convert my xml
    node.getChildText("answercorrect")
    node.getChildText("answerincorrect")
    node.getChildText("answerincorrect1")
    into strings, for manipulate it after , and create the methods for retrive the question the answers etc....
    could you please help me with this litle sept??

    i had try lot of things but i can get into wrok good

    Java Code:
     for (int i=0; i< list.size(); i++)
               {
                 Element node = (Element) list.get(i);
    Object data[] = new Object[10];
    String str[] = (String[])data[i];
              //jt.append("question : " + node.getChildText("quest") + "\n");
    
     String designations[];
    designations = new String[3];
    designations[1] = "General Manager";
    designations[2]="node.getChildText("answerincorrect")";
    designations[0]=designations[2];
    
    for (int i = 0; i < tempArray.length; i++) {
        mainArrayList.add(tempArray[i]);
    }
    
     List<String> answers = new ArrayList<String>();
     answers.add(node.getChildText("answerincorrect"));
     answers.add(node.getChildText("answerincorrect1"));
     answers.add(node.getChildText("answercorrect"));
    
     JRadioButton ans1 = new JRadioButton(""+ answers );
    JRadioButton ans2 = new JRadioButton(""+ answers );
    JRadioButton ans3 = new JRadioButton(""+ answers );
    
               aaa.setText("question : " + node.getChildText("quest") + "\n");
               JLabel bbb = new JLabel("question : " + node.getChildText("quest") + "\n");
    //here must be random
    //JRadioButton ans1 = new JRadioButton("answer : " + data + "\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");
    
    
     Vector a = new Vector();
    //metodo add() recupera dados
     a.add( node.getChildText("answerincorrect"));
     a.add(node.getChildText("answerincorrect"));
     String adeus = (String) a.get(1);
     //sysout(adeus);
    
    
    //group buttons
    ButtonGroup group = new ButtonGroup();
            group.add(ans1);
            group.add(ans2);
            group.add(ans3);
    Moderator Edit: Code tags added
    Last edited by Fubarable; 05-27-2010 at 04:11 PM. Reason: Moderator Edit: Code tags added

  10. #30
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pipikrk View Post
    i have understand you sugestion, but for create the classes and the methods,
    first i need to convert my xml
    node.getChildText("answercorrect")
    node.getChildText("answerincorrect")
    node.getChildText("answerincorrect1")
    into strings,
    This makes no sense to me as node.getChildText(...) already returns a String.

    So you have your Strings in hand, and all you have to do is put them into an object of choice. I'm not sure what's holding you back.

    edit: Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Much luck
    Last edited by Fubarable; 05-27-2010 at 04:10 PM.

  11. #31
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To re-state what I stated before:

    Quote Originally Posted by Fubarable View Post
    If I were you, I'd create a class called Question that held the question String, the right answer String and the List<String> of incorrect answers, give it a constructor that would allow it to accept the answer and question Strings, perhaps an add(String str) method to allow you to add incorrect answers, a method say called List<String> getPossibleAnswers() that returned a String list in randomized order of the correct answer string and incorrect answer strings (mixed together) a boolean checkAnswer(String possibleAnswer) method, etc....

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

    Default

    but how i can manipulate this ?
    node.getChildText("answercorrect")
    node.getChildText("answerincorrect")
    node.getChildText("answerincorrect1")

    when i would create class for add string, question, add random question?

    how can i do this?
    shouldn't i save this into another string , for later i could use it?

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

    Default

    my problem is to pass the xml into this functions that i need to create?
    this is my problem?

  14. #34
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pipikrk View Post
    but how i can manipulate this ?
    node.getChildText("answercorrect")
    node.getChildText("answerincorrect")
    node.getChildText("answerincorrect1")

    when i would create class for add string, question, add random question?:

    Using my suggestion above, you'll create a Question class and have your app hold a List<Question> that may be in fact an ArrayList<Question> here called questionList:
    Java Code:
    String questionText = .....;  // get the question text
    String correctAnswer = node.getChildText("answercorrect");
    
    List<String> incorrectAnswers = new ArrayList<String>();
    incorrectAnswers.add(node.getChildText("answerincorrect"));
    incorrectAnswers.add(node.getChildText("answerincorrect1"));
    //... more?
    
    // class you create called Question
    Question myQuestion = new Question(questionText, correctAnswer, incorrectAnswers);
    
    // here add this current question object to the list held by this class:
    questionList.add(myQuestion);
    The details are left to you.
    Last edited by Fubarable; 05-27-2010 at 04:45 PM.

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

    Default

    the problem is that i have more than one question?
    should i make an loop?
    because if i do this
    String correctAnswer = node.getChildText("answercorrect");

    it will just store 1 correct answer , and about the others?

  16. #36
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pipikrk View Post
    the problem is that i have more than one question?
    should i make an loop?
    Yes, you'll need a loop to read through the XML and this is the lop that you'll use to create Question objects and add them to your questionsList.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Display partial file contents in JTextArea
    By tmoehlman in forum New To Java
    Replies: 0
    Last Post: 11-02-2009, 11:03 PM
  2. Replies: 10
    Last Post: 10-30-2009, 11:15 PM
  3. Replies: 12
    Last Post: 10-09-2009, 06:26 AM
  4. display huge size file in jtextarea
    By tskarthic in forum AWT / Swing
    Replies: 1
    Last Post: 03-21-2009, 06:30 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 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
  •