I want to load an SVG-file on a JPanel with this simple code but the JPanel is grey. Do i something wrong?
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;
public class SVGApplication extends JPanel
{
public SVGApplication(){
JSVGCanvas svg = new JSVGCanvas();
// location of the SVG File
svg.setURI("file:/C:/Users/Linda/Desktop/test.svg");
JPanel panel = new JPanel();
panel.add(svg);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("SVGView");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SVGApplication());
frame.pack();
frame.setSize(500, 400);
frame.setVisible(true);
}
}
You're adding things to a JPanel, one here named "panel", that gets added to nothing and so is never displayed:
public SVGApplication(){
JSVGCanvas svg = new JSVGCanvas();
// location of the SVG File
svg.setURI("file:/C:/Users/Linda/Desktop/test.svg");
JPanel panel = new JPanel(); // *** what is this for? ***
panel.add(svg); // **** you never add this panel to anything ****
}
Get rid of panel:
public SVGApplication(){
JSVGCanvas svg = new JSVGCanvas();
// location of the SVG File
svg.setURI("file:/C:/Users/Linda/Desktop/test.svg");
// JPanel panel = new JPanel(); // *** what is this for? ***
// panel.add(svg);
add(svg);
}
Better still, why not simply use a JSVGCanvas component? Why wrap it in your SVGApplication panel?