Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java gui io

  • 27-04-2006 12: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