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

    Default JTextArea and xml file

    i want that the content of the xml file will be displayed in my JTextArea , jt,

    How can i do this?

    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("dear test :) ",5,20);
     
       
    
        SAXBuilder builder = new SAXBuilder();
        	File xmlFile = new File("file.xml");
           
            try{
    
        	   Document document = (Document) builder.build(xmlFile);
               Element rootNode = document.getRootElement();
               List list = rootNode.getChildren("staff");
    
               for (int i=0; i< list.size(); i++)
               {
                 Element node = (Element) list.get(i);
    
                 System.out.println("First Name : "  + node.getChildText("firstname"));
           	     System.out.println("Last Name : "  + node.getChildText("lastname"));
          	     System.out.println("Nick Name : "  + node.getChildText("nickname"));
          	     System.out.println("Salary : "  + node.getChildText("salary"));
                 jt.read(node.getChildText("salary"));
                 
               }
    
        	 }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();
                }
            });
        }
    
     }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Welcome to forum. :)

    What happen, did you comes with any error in your code? Where you stuck with?

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

    Default don't w

    don't know how to show the output of
    System.out.println("First Name : " + node.getChildText("firstname"));
    System.out.println("Last Name : " + node.getChildText("lastname"));
    System.out.println("Nick Name : " + node.getChildText("nickname"));
    System.out.println("Salary : " + node.getChildText("salary"));


    in the JTextArea

    would like to get this information in the JTextArea
    node.getChildText("firstname")
    node.getChildText("lastname")
    node.getChildText("nickname")
    node.getChildText("salary")

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

    Default

    Have you looked at the JTextArea API? If not, consider doing so. One JTextArea method that may be worth considering (if you haven't already) is the append method.

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

    Question still can't find it

    i had looked at api of jtextarea, sorry but i still can't find the way to solve the problem, and display the xml elements into jtextarea

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

    Default

    I told you which method to use, the append method. I'm afraid I have no idea of how to help you if you don't show us how you've tried to use this method and failed.

  7. #7
    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
    i had looked at api of jtextarea, sorry but i still can't find the way to solve the problem, and display the xml elements into jtextarea

    i can only repeat what Fubarable has already written. to display new text into a jtextarea the method yourTextArea.append(YourString) is used. here is a textdemo http://java.sun.com/docs/books/tutor.../TextDemo.java that shows how to append text. or do you need a xml-parser to extract the elements?
    Last edited by j2me64; 04-14-2010 at 08:58 AM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think OPs' stuck on with how to display (append) text in text area. @OP, go through what j2me64 posted link for the demo on text area.

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

    Default

    basically what i pretend is to print the output of the lines in the jtextarea ,

    System.out.println("First Name : " + node.getChildText("firstname"));
    System.out.println("Last Name : " + node.getChildText("lastname"));
    System.out.println("Nick Name : " + node.getChildText("nickname"));
    System.out.println("Salary : " + node.getChildText("salary"));

    i try this way , and some others but doesn't work out
    jt.getDocument(System.out.println("First Name : " + node.getChildText("firstname")));

  10. #10
    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
    basically what i pretend is to print the output of the lines in the jtextarea

    could you please attach your file.xml with some entries. so i can really run the same data you are using. i try with a other xml file but with this data the for loop for println is never executed.
    Last edited by j2me64; 04-14-2010 at 11:05 AM.

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

    Default this is my xml file

    <?xml version="1.0" ?>
    - <company>
    - <staff>
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
    </staff>
    - <staff>
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </staff>
    </company>

  12. #12
    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
    <?xml version="1.0" ?>
    - <company>
    - <staff>
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
    </staff>
    - <staff>
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </staff>
    </company>

    opening the file.xml with the content above produce

    Java Code:
    Error on line 2 of document file:/E:/workspace/Gaming/TestProject/file.xml: Content is not allowed in prolog.

    ok, removing the hyphen i was able to read 2 entries. even if the + in string is not very efficient to print the readed input to the jtextarea add in the for loop after each println append(), for example

    Java Code:
    				// print to the console
    				System.out.println("First Name : "
    						+ node.getChildText("firstname"));
    				// print in the JTextArea
    				jt.append("First Name : " + node.getChildText("firstname") + "\n");

    instead of calling several times the append method i suggest you construct your dataset in a StringBuilder and then add it to the jtextarea at once.
    Last edited by j2me64; 04-14-2010 at 01:06 PM.

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

    Default

    the ouput of the program is

    First Name : yong
    Last Name : mook kim
    Nick Name : mkyong
    Salary : 100000
    First Name : low
    Last Name : yin fong
    Nick Name : fong fong
    Salary : 200000


    but i want to have this output inside of jtextarea , i just don't know how to do it ,
    all the rest works fine

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

    Default the xml file

    <?xml version="1.0" ?>
    <company>
    <staff>
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
    </staff>
    <staff>
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </staff>
    </company>

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

    Default

    to build your string for the jtextarea you could call this method once! with jt.append(getString(list)); and then all entries in your list will be added to the jtextarea.

    Java Code:
    	private static String getString(List list) {
    		
    		StringBuffer s = new StringBuffer(60*list.size());
    		
    		for (int i = 0; i < list.size(); i++) {
    			Element node = (Element) list.get(i);
    			s.append("First Name : ");
    			s.append(node.getChildText("firstname"));
    			s.append("\n");
    			
    			s.append("Last Name : ");
    			s.append(node.getChildText("lastname"));
    			s.append("\n");		
    			
    			s.append("Nick Name : ");
    			s.append(node.getChildText("nickname"));
    			s.append("\n");	
    			
    			s.append("Salary : ");
    			s.append(node.getChildText("salary"));
    			s.append("\n\n");	
    		}
    		return s.toString();
    	}

    it uses the StringBuffer with is very efficient to handle String and concatenation. the drawback is when your xml schema changes you will have to adapt the method.

    put a scrollbar to your gui, otherwise you can't scroll through the jtextarea.
    Last edited by j2me64; 04-15-2010 at 09:52 AM.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pipikrk View Post
    basically what i pretend is to print the output of the lines in the jtextarea ,

    System.out.println("First Name : " + node.getChildText("firstname"));
    System.out.println("Last Name : " + node.getChildText("lastname"));
    System.out.println("Nick Name : " + node.getChildText("nickname"));
    System.out.println("Salary : " + node.getChildText("salary"));

    i try this way , and some others but doesn't work out
    jt.getDocument(System.out.println("First Name : " + node.getChildText("firstname")));
    think you still struggle with how to manipulate the XML content. And j2me64 gives you a better explanation.

    And also I hope that in your xml file that hyphen is not contain, you just copy the content from the browser, isn't it?

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

    Default it works great

    i was using apend method , but somehow it did not work.

    thanks 2me64

  18. #18
    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
    i was using apend method , but somehow it did not work.

    when i execute your file.xml with my method getString() all worked fine. what did not work? please tell the details of post your code.

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

    Default put the append after the } by mistake

    as soon as i correct start to work fine.

    btw

    do you know how to associate a radio button with xml element?

    the xml file would be an questionaire

    <file>
    <question>
    <quest>who is the best football player?</quest>
    <answercorrect>messi</answercorrect>
    <answerincorrect>ronaldo</answerincorrect>
    <answerincorrect>kaka</answerincorrect>
    </question>
    </file>

    that when i could select only 1 answer by radio button,
    now i know how to display the xml element,
    but i don't know how to associate the radio button to it

  20. #20
    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
    that when i could select only 1 answer by radio button, now i know how to display the xml element,
    but i don't know how to associate the radio button to it

    afaik you can't add a jradiobutton component to a textarea. but you can either remove your textarea and add your jradiobuttons or you add your jradiobuttons in the NORTH region and the textarea in CENTER region.

    but: will you now always read data that need jradiobuttons or data that need only to be displayed in the textarea? could you please specify first what you need?
    Last edited by j2me64; 04-15-2010 at 10:49 AM.

Page 1 of 2 12 LastLast

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
  •