Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java gui io

  • 27-04-2006 01:46PM
    #1
    Closed Accounts Posts: 839 ✭✭✭


    I'm writing a program to load/save from a file i.e. the contents in the file would be displayed in the gui, I had thought about an XML file but can someone point me at some example code on how to do this


    e.g. file contains colour - red so would be the value and colour the key.

    so in the colour option in the gui it would read the value from the file and show that the currently selected colour is red.

    maybe a file is not the best way to do this - has anyone any other possible solutions?


Comments

  • Closed Accounts Posts: 261 ✭✭bishopLEN


    zap wrote:
    I'm writing a program to load/save from a file i.e. the contents in the file would be displayed in the gui, I had thought about an XML file but can someone point me at some example code on how to do this


    e.g. file contains colour - red so would be the value and colour the key.

    so in the colour option in the gui it would read the value from the file and show that the currently selected colour is red.

    maybe a file is not the best way to do this - has anyone any other possible solutions?
    I will try put some code together for you, I think the xml idea is fine; it is a good way for storing settings and the like. Draw up a DTD for an XML in the meantime.


  • Closed Accounts Posts: 261 ✭✭bishopLEN


    I presume you have enough experience to get this up and running, I got an xml tag to be read just now so you should be able to adapt it.

    Xml File
    <?xml version="1.0" ?>
    <!DOCTYPE Test SYSTEM "Test.dtd">
    <Test>
        <paragraph>
            Hi what is up today
        </paragraph>
        <colour>
            Red
        </colour>
     </Test>
    
    Java Section
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
                DocumentBuilder DOM = fact.newDocumentBuilder();
    Document document = DOM.parse("Text.xml");
                NodeList nodes = document.getElementsByTagName("paragraph");
    System.out.println("Contents : "+nodes.item(0).getTextContent());
    
    That is is the working section for you, hope you get it working


  • Closed Accounts Posts: 839 ✭✭✭zap


    can you give me the full code that you used, I can't seem to get it working?


  • Closed Accounts Posts: 261 ✭✭bishopLEN


    import javax.xml.parsers.*;
    import java.io.*;
    import org.w3c.dom.*;
    import javax.imageio.metadata.IIOMetadataNode;

    public class TestXML
    {
    public TestXML()
    {
    System.out.println("Start");
    try
    {
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder DOM = fact.newDocumentBuilder();
    System.out.println("Middle1");
    /*
    *FileInputStream in = new FileInputStream("Text.xml");
    BufferedReader b = new BufferedReader(new InputStreamReader(in));

    System.out.println("Line 1"+b.readLine());
    System.out.println("Line 2"+b.readLine());
    System.out.println("Line 3"+b.readLine());
    */

    Document document = DOM.parse("Text.xml");
    NodeList nodes = document.getElementsByTagName("paragraph");
    System.out.println("Nodes : "+ nodes.getLength());
    System.out.println("Contents : "+nodes.item(0).getTextContent());
    System.out.println(""+DOM.parse("Text.xml").getDoctype());
    System.out.println("Middle3");
    }
    catch(Exception e)
    {

    }
    }
    public static void main(String [] args)
    {
    new TestXML();
    }
    }


Advertisement