Hi!

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.

Thanks!

Recommended Answers

All 10 Replies

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).

Thanks!

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:

Rectangle;40;9;86;78;true;0;0;0
Oval;136;56;279;126;true;0;0;0
Rectangle;60;139;117;203;true;255;51;51
Line;343;174;385;226;true;255;51;51

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");
                name.setText("label");
                root.addContent(name);
                Element x = new Element("x");
                x.setText(Integer.toString(c[i].getX()));
                root.addContent(x);
                Element y = new Element("y");
                y.setText(Integer.toString(c[i].getY()));
                root.addContent(y);
                Element width = new Element("width");
                width.setText(Integer.toString(c[i].getWidth()));
                root.addContent(width);
                Element height = new Element("height");
                height.setText(Integer.toString(c[i].getHeight()));
                root.addContent(height);
            }
        }
        try{
            writeToFile("myProj.cbd", doc);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void writeToFile(String fname, Document doc)
    {
        try{
            FileOutputStream out = new FileOutputStream(fname);
            XMLOutputter op = new XMLOutputter();
            op.output(doc, out);
            out.flush();
            out.close();
        }
        catch (IOException e){
            System.err.println(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());

        try{
    	   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){
    		System.out.println(io.getMessage());
    	 }catch(JDOMException jdomex){
    		System.out.println(jdomex.getMessage());
    	}
    }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.