Reputation: 179
I am using JEditorPane to set background image on my JFrame. The reason why I use this component, is that I want to set small image and be able to repeat it according to size of the frame. Similar to CSS analogue of background-repeat. The problem which I have is, I am not able to load image from my local folder.
background.setContentType("text/html");
background.setText("<html><body style=\"background-image: url(http://hq-wallpapers.ru/wallpapers/8/hq-wallpapers_ru_abstraction3d_39318_1920x1200.jpg);\"></body></html>");
this.setContentPane(background);
What I tried is to use: url(../image.jpg); but it doesn't work.
If can offer better way of doing this task, I will appreciate it. P.S.: And I am not allowed to use JFrameForm, because of requirements of my project.
Upvotes: 1
Views: 1144
Reputation: 347314
../image.jpg
isn't a valid URL, as the JEditorPane
has no "code base" from which to access where the "reference" should be loaded from.
Instead, you could use a File
to generate a URL
(or if it's an embedded resource, Class#getResource
), for example...
File background = new File("../image.jpg");
URL url = background.toURI().toURL();
ep.setContentType("text/html");
ep.setText("<html><body style='color: #ffffff; background-image: url(" + url.toString() + ");'>Boo</body></html>");
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
try {
setLayout(new BorderLayout());
JEditorPane ep = new JEditorPane();
File background = new File("/Volumes/Disk02/Dropbox/MegaTokyo/Aqua/aria_fanart_by_imskeptical-d5xbvgz.jpg");
URL url = background.toURI().toURL();
ep.setContentType("text/html");
ep.setText("<html><body style='color: #ffffff; background-image: url(" + url.toString() + ");'>Boo</body></html>");
add(new JScrollPane(ep));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Now if URL url = background.toURI().toURL();
doesn't work, you might need to use URL url = background.getCanonicalFile().toURI().toURL();
instead
Upvotes: 2