Page 1 of 6 123 ... LastLast
Results 1 to 20 of 115
Like Tree3Likes

Thread: How display streamresults in JTextArea (Jlabel)

  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default How display streamresults in JTextArea (Jlabel)

    I have a xmltransformer class
    Java Code:
    import java.io.File;
    import java.io.*;  
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactoryConfigurationError;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import java.util.Vector;
    
    public class CrunchifyCreateXMLDOM1 {
     
       public static String xmlmain() {
            String xmlString = "";
            DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder icBuilder;
            try {
                icBuilder = icFactory.newDocumentBuilder();
                Document doc = icBuilder.newDocument();
                Element mainRootElement = doc.createElementNS("http://crunchify.com/CrunchifyCreateXMLDOM", "File"+""+"System");
                doc.appendChild(mainRootElement);
                
                Filewalker.walk("c:\\" );
                
                for ( Filewalker fp : Filewalker.listed ) {
                
                mainRootElement.appendChild(getCompany(doc, fp.name, fp.length, fp.path));
                
               // output DOM XML to console 
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
                DOMSource source = new DOMSource(doc);
                StreamResult console = new StreamResult(System.out);
                transformer.transform(source, console);
                xmlString = console.toString();
                }
                System.out.println("\nXML DOM Created Successfully..");
     
            } catch (Exception e) {
                e.printStackTrace();
            }
           return xmlString;
        }  
     
        private static Node getCompany(Document doc, String name, long length, String path) {
            Element company = doc.createElement("File");
            company.appendChild(getCompanyElements(doc, company, "Name", name));
            company.appendChild(getCompanyElements(doc, company, "Size", String.valueOf(length)));
            company.appendChild(getCompanyElements(doc, company, "Path", path));
            return company;
        }
     
        // utility method to create text node
        private static Node getCompanyElements(Document doc, Element element, String name, String value) {
            Element node = doc.createElement(name);
            node.appendChild(doc.createTextNode(value));
            return node;
        }
        
        public static void main(String[] args) {
    		CrunchifyCreateXMLDOM1.xmlmain();
    	}
    }
    That display the string in such way as:
    <FileSystem xmlns="http://crunchify.com/CrunchifyCreateXMLDOM">
    <File>
    <Name>IO.SYS</Name>
    <Size>222390</Size>
    <Path>c:\IO.SYS</Path>
    </File>
    <File>
    <Name>MSDOS.SYS</Name>
    <Size>6</Size>
    <Path>c:\MSDOS.SYS</Path>
    </File>
    <File>
    ...
    But I need to display (if possible) in JTexArea, Jlabel, JtextPane or in AWT
    how to do it?
    As I got such
    string --javax.xml.transform.stream.StreamResult@d8326d--
    as the result of executing the code:
    Java Code:
    import javax.swing.*;
    
    public class gui {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame ("tt");
            frame.setSize(500,500);
            frame.setResizable(false);
    
    String ta=CrunchifyCreateXMLDOM1.xmlmain();
    JTextArea textArea = new JTextArea();
        textArea.append(ta);
        textArea.setSize(500,500);    
        textArea.setLineWrap(true);
        textArea.setEditable(true);
        textArea.setVisible(true);
    
        JScrollPane scroll = new JScrollPane (textArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
       //frame.add(textArea);
        frame.add(scroll);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        }
    Can anybody suggest something? Maybe I need to use invokelater()of Swing Utilities?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    That display the string in such way as:
    What statement(s) in you code displays those Strings? Can the code be changed to append the Strings into a text area?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    StreamResult console = new StreamResult(System.out); //this string display the text in console transformer.transform(source, console); //this string transform the xml to console beforehand
    xmlString = console.toString(); //this string convert the console to string to make the whole method of string return type; in the end I return xmlString.

    The append method make the same as the textarea constructor. So I need to know is it possible to display the whole xml list of thousands of lines that took several minutes to show and form, as the textarea field seems to created instantly.

    There is read() method for textarea that took the argument of inputstream but the exact type should be Reader, so I need to make xmlmain() of Writer return type, but I do not know how convert the system.out(Outputstream) to writer??
    And if so could it be possible to display along the several minutes the inputstream to JtextArea?
    And the other question why the textarea is not scrollable, despite I have such option in code?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    StreamResult console = new StreamResult(System.out); //this string display the text in console
    Replace the arg for the StreamResult class's constructor with a byte array output stream. Then the output will be in the class's object where you can extract it for your purposes.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    public static void main(String[] args)
    {
    JFrame frame = new JFrame ("tt");
    frame.setSize(500,500);
    frame.setResizable(false);
    gui1.xmlmain();
    PipedOutputStream po;
    PipedInputStream pi;
    try {
    po = new PipedOutputStream();
    pi=new PipedInputStream(po);
    } catch (Exception e) {
    e.printStackTrace();
    }

    JTextArea textArea = new JTextArea();
    InputStreamReader reader = new InputStreamReader (System.in); //Outputsream got through the Piped .. Stream
    /*try {
    textArea.read (reader,null);
    } catch (Exception e) {
    e.printStackTrace();
    }*/
    textArea.setSize(500,500);
    textArea.setLineWrap(true);
    textArea.setEditable(false);
    textArea.setVisible(true);
    JScrollPane scroll = new JScrollPane (textArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERT ICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HO RIZONTAL_SCROLLBAR_ALWAYS);
    //frame.add(textArea);
    frame.add(scroll);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    }
    }
    Due to commented textArea.read() method I cannot even Textarea Window, despite without it commented one I got simple
    emprty TextArea - I think the reason is in try-catch block that someway hides it??
    Anyway It would be simply to append the xml Document to textArea depsite there is also the uncaught exception error

  6. #6
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    Java Code:
    public static void main(String[] args)
        {
            JFrame frame = new JFrame ("tt");
            frame.setSize(500,500);
            frame.setResizable(false);
         gui1.xmlmain();
         PipedOutputStream po;
         PipedInputStream pi;
         try {
        po = new PipedOutputStream();
        pi=new PipedInputStream(po); 
         } catch (Exception e) {
                e.printStackTrace();
            }
            
        JTextArea textArea = new JTextArea();
        InputStreamReader reader = new InputStreamReader (System.in); //Outputsream got through the Piped .. Stream
        /*try {
        textArea.read (reader,null);
        } catch (Exception e) {
                e.printStackTrace();
            }*/             
        textArea.setSize(500,500);    
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        textArea.setVisible(true);
        JScrollPane scroll = new JScrollPane (textArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        //frame.add(textArea);
        frame.add(scroll);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        }
    Due to commented textArea.read() method I cannot even Textarea Window, despite without it commented one I got simple
    emprty TextArea - I think the reason is in try-catch block that someway hides it??
    Anyway It would be simply to append the xml Document to textArea depsite there is also the uncaught exception error

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    Sorry, I don't understand what the problem is.
    simply to append the xml Document to textArea
    Did you try that? What happened?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    When I append the toString of StreamResults to textArea I got such line in void TextArea --
    javax.xml.transform.stream.StreamResult@1ea817f --What it means? This "@1ea817f " especially.
    It must the streams is not converted to StringWriter to String?
    Maybe I do not correctly use the Piped ... Stream --maybe the Outputsream to Sytem in conversion give nothing:
    gui1.xmlmain();
    PipedOutputStream po;
    PipedInputStream pi;
    try {
    po = new PipedOutputStream();
    pi=new PipedInputStream(po);
    } catch (Exception e) {
    e.printStackTrace();
    }

    JTextArea textArea = new JTextArea();
    InputStreamReader reader = new InputStreamReader (System.in); //Outputsream got through the Pip
    It is impossible to append Document to textArea as Document is interface.
    Despite maybe I need to create doc object of it and convert with toString -- but I fears the same error would be.
    To be frankly I did not saw any workable read() method of textArea application in the internet???

  9. #9
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    And Document object converted to String bring #document:null -
    here is explanation - http://www.coderanch.com/t/632186/ja...g-XML-Document
    So I just need to use pipes to convert System.out to read method of textArea.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    Did you try what I suggested in post#4?


    to convert System.out to read method of textArea.
    Can you make a small, simple program that shows what you are trying to do?
    There are probably ways to get the data from where it is to display in a text area.
    Last edited by Norm; 11-01-2015 at 11:44 PM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    StreamResult console = new StreamResult(System.out); //this string display the text in console- yes of course - it is in initial version.
    But how i can check if my pipes are workable it should be very simple--but do not work.

  12. #12
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)


  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    Why are you trying to use pipes? Why not write the data into an object that can be turn it into a String which can be appended to the contents of the textarea?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    What kind of object? Document to string - return #document:null (As I should probably manually do XML tags with the bunch of cycle). Maybe Dom.
    Anyway the pipes should work - as they are conceived to transfer OutPutStream to InPutStream.
    As I checked the pipedin...available() maybe I could check pipedoutputstream as it is the initial stage of this chanell.

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How display streamresults in JTextArea (Jlabel)

    It's not that hard. Turn your XML into a String by handing it to a Transformer (Insert bad Optimus Prime joke here). This is a method I have in an XMLUtil class:
    Java Code:
    public class XMLUtil {
       private static TransformerFactory factory = TransformerFactory.newInstance();
       
       public static String getDocumentAsString(Document doc) throws XMLException {
            StringWriter sw = new StringWriter();
            try {
                Transformer trans = factory.newTransformer();
                Source source = new DOMSource(doc);
                StreamResult result = new StreamResult(sw);
                trans.transform(source, result);
            }
            catch (TransformerException e) {
                throw new XMLException("Error transforming document to String", e);
            }
            return sw.toString();
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  16. #16
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    What kind of object? Document to string - return #document:null (As I should probably manually do XML tags with the bunch of cycle). Maybe Dom.
    Anyway the pipes should work - as they are conceived to transfer OutPutStream to InPutStream.
    As I checked the pipedin...available() maybe I could check pipedoutputstream as it is the initial stage of this chanell.
    Even if I created the String method that I pass to append () I got such one like it:javax.xml.transform.stream.StreamResult@1ea817f
    So it is no probable to use StreamResult to append. So just use read() but in this case the Textarea window even not appear. So I want to settle correctly pipes as just it can help.

  17. #17
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    I used: How to convert org.w3c.dom.Document to String
    Document To String : DOM DocumentÂ*«Â*XMLÂ*«Â*Java -- just substituted the return type to String against Void.

  18. #18
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    Such code is working fine (but it is from file), so I need to change System.out againts reader (maybe Setout if not just I need Reader), and EOF is just simple boolean variable, not build-in keyword in java:
    /* try {
    FileReader f = new FileReader("D:\\file.xml");
    BufferedReader b = new BufferedReader(f);

    boolean eof = false;

    while(! eof)
    {
    String lineIn = b.readLine();
    if(lineIn == null)
    {
    eof = true;
    }
    else
    {
    textArea.append(lineIn + System.getProperty("line.separator"));
    }
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    */

  19. #19
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: How display streamresults in JTextArea (Jlabel)

    Or it seems that EOF is just the end of input source, not just file - anyway I need the Inputstream

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How display streamresults in JTextArea (Jlabel)

    What does the API doc say is returned by the readLine() method?

    I need the Inputstream
    For what? Please explain why that is needed.

    NOTE: Please wrap posted code in code tags to preserve readability.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 6 123 ... LastLast

Similar Threads

  1. Dynamic Jlabel display
    By shashank.pr in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2013, 02:08 PM
  2. How can i display a number on a jlabel.
    By Shin in forum New To Java
    Replies: 4
    Last Post: 10-15-2012, 05:13 PM
  3. [Urgent Help] JLabel images display
    By baobao816 in forum AWT / Swing
    Replies: 2
    Last Post: 05-12-2012, 09:46 AM
  4. Display partial file contents in JTextArea
    By tmoehlman in forum New To Java
    Replies: 0
    Last Post: 11-02-2009, 11:03 PM
  5. Jtextarea Content Display Problem
    By REVANSIDDHA in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2009, 06:53 PM

Tags for this Thread

Posting Permissions

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