I need my own file extension for SWING desktop application (image processing tool). In fact, I want a user to be able to save locations of JLabel components on a screen. Please, give me a hint on how to implement this task.


Make one up and then use the ftype and assoc commands to create the associations (on windows, on Linux you probably need to use the "control panel"). Google those commands.

Edit: And, if you wish to "deploy" that association, then make sure whatever "installer" maker you have can handle the registry changes (on windows and the equivalent on *nix).

Thanks! I'll google those commands. And what about using xml? Could it be appropriate for solving my task?

I'll try to specify my question. Let's say there is a single JLabel that is described with x & y coordinates. And let's say that a user wants to save this current project and he/she wants to be able to open it in the future and add more JLabel components. Could please someone give me a code snippet or pseudo code to implement this task?

P.S. A project must be saved in my own file extension (e.g., called as *.cbd).


IOW "do my (home)work for me". No.

Well, I would like to understand what are the main steps used to implement my goal, because I found only separate information on this topic and cannot aggregate it. The main steps could be described in words or in pseudo-code or whatever, it does not matter. If anybody could help, I would appreciate this very much. Thanks.

Yes, you could easily use XML to store the information about the currently open pictures/labels. JDOM is a very easy to work with DOM parser, which you can use to work with the XML doc.

Your doc structure could be as simple as

<panel x='10' y='35 name='Whatever' />

or as complex as you like.

I think the extension doesn't matter. The program just see the encoding of your file. You can use a simple CSV (Comma Separated Value) to do it. It's an example from my homework:


The first value is object type, and the next value is the coordinates, and so on. Quite simple I think.

Ok, thanks! Now I have some ideas of how to implement it. I will post my results later.

Hi! Below you may see a simplified version of my code used to save parameters of JLabel components in my own file format. Thanks for a help!

private void saveAs() {
        Element root = new Element("Panel");
        Document doc = new Document(root);
        Component[] c = MainClass.getSelectablePanel().getComponents();
        for(int i = 0; i < c.length; i++)
             if(c[i] instanceof JLabel)
                Element name = new Element("name");
                Element x = new Element("x");
                Element y = new Element("y");
                Element width = new Element("width");
                Element height = new Element("height");
            writeToFile("myProj.cbd", doc);
        catch (Exception e){

    private static void writeToFile(String fname, Document doc)
            FileOutputStream out = new FileOutputStream(fname);
            XMLOutputter op = new XMLOutputter();
            op.output(doc, out);
        catch (IOException e){

Now I'm trying to read the saved xml file as follows:

private void open() {
        SAXBuilder builder = new SAXBuilder();
    	File xmlFile = new File(openedFileName());

    	   Document document = (Document) builder.build(xmlFile);
           Element rootNode = document.getRootElement();
           List list = rootNode.getChildren("Panel");

           for (int i=0; i< list.size(); i++)
             Element node = (Element) list.get(i);

             System.out.println("Name : "  + node.getChildText("name"));
       	     System.out.println("X : "  + node.getChildText("x"));
      	     System.out.println("Y : "  + node.getChildText("y"));
      	     System.out.println("Width : "  + node.getChildText("width"));
             System.out.println("Height : "  + node.getChildText("height"));

    	 }catch(IOException io){
    	 }catch(JDOMException jdomex){

However, the list.size() returns 0. Could please someone explain me why does it happen? Thanks!