javaswingjeditorpanemusicxml

MusicXML files in java swing - visual representation and dynamic editing


I'm developing a desktop application with java swing which should be able to display the visual content (notes, clefs, measures) defined in a MusicXML file in the frame. All .xml parsers that I found allow me to only create trees. I couldn't display the content with the JEditorPane, too.

Can I do it or will I need to first transform it dynamically to some other format such as .pdf? If so - how can I do it in java?


Solution

  • Use a JTextArea to let the user edit raw XML. Load the file in the background using SwingWorker, as shown here, to mitigate the effect of any latency.

    image

    @Override
    protected Integer doInBackground() {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader("build.xml"));
            String s;
            try {
                while ((s = in.readLine()) != null) {
                    publish(s);
                }
            } catch (IOException ex) {
                ex.printStackTrace(System.err);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace(System.err);
            }
        }
        return status;
    }
    

    By visual representation, I meant that I needed a way to display this type of visual output.

    You might look at the JMusicXML project, which has "some Java awt/swing graphics player." More resources may be found here.

    image