vendredi 27 mars 2015

java swing JEditorPane image

I have a frame which contains JEditorPane wrapped inside JScrollPane.


The problem is that when the frame is loaded JScrollPane is too small to show all JEditorPane content. But when I resize the frame or minimize/restore it JScrollPane gets normal.


I'm sure that is because JEditorPane contains an image, that has not already loaded.


The only way I have found is to revalidate frame in invokeLater method. It seems to me very odd and I want to find a way to do the same without invokeLater.



class MyFrame extends JFrame{

MyFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 800);

JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText("<p><img src=\"file:C:\\image.png\"></p>");

JScrollPane scrollpane = new JScrollPane(editor);

JPanel mainPanel = new JPanel();
mainPanel.add(scrollpane);


add(mainPanel,BorderLayout.CENTER);

setLocationRelativeTo(null);
setVisible(true);

revalidate(); // not working
repaint(); // not working

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
revalidate(); // OK
repaint(); // OK
}
});
}
}

Aucun commentaire:

Enregistrer un commentaire