J'ai un JScrollPane qui contient un JeditorPane. D'après la doc, JScrollPane devrait appeler la méthode getPreferredScrollableViewportSize() pour décider du taille qu'il devrait prendre. Or dans mon petit exemple ci-dessous cette méthode n'est même pas appelée. Du coup, je ne vois ni l'editorpane ni le scrolpane sur mon Jpanel principale.

Pour voir quelque chose, il faut que je remplace setLocation() par setBounds() dans lequel je spécifie une dimension au jscrollpane qui peut ne rien à voir avec la dimension souhaitée de l'editorpane qui est retournée par getPreferredScrollableViewportSize() .

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
 
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;
 
class EditorP extends JEditorPane
{
	EditorP(String str1, String str2){super(str1,str2);}
 
	public Dimension getPreferredScrollableViewportSize()
	{
		return (new Dimension(600,100));
	}
/*
	public boolean getScrollableTracksViewportHeight()
	{
		return true;
	}
	public boolean getScrollableTracksViewportWidth()
	{
		return true;
	}
*/
}
 
public class ScrolFan extends JPanel 
{
	BufferedImage img1=null;
	JScrollPane scrol1=null;
	JEditorPane pan1 = null;
 
	ScrolFan()
	{
	  try {
		setLayout(null);
		img1 = readImage("maison.jpg");	
		pan1 = new EditorP("text/html","<html> <body><strong>Ca y est, c'est les vacances. Nathalie et ses parents ont choisi de passer quelques jours à la campagne. Il fait très beau, Nathalie a décidé d’aller se promener autour de la maison.</font></strong></p></body></html>");	
		scrol1=new JScrollPane(pan1);
		scrol1.setLocation(100,400);
	//	scrol1.setBounds(200,600,100,100);
		add(scrol1);
	  } catch(Exception ex) {}
	}
 
    public Dimension getPreferredSize() {
      return new Dimension(1020,700);
    }  
 
    protected void paintComponent(Graphics g) 
    {
    	  g.drawImage(img1, 0, 0, getWidth(),getHeight(), this);
    }
 
     public BufferedImage readImage(String img) throws IOException
     { 	  
 	 		URL url = getClass().getResource(img);
    	 	if (url == null) 
    	 	{
    	 		IOException ioe= new IOException("bad image file name");
    	 		throw ioe;
    	 	}
    	 	return ImageIO.read(url);
     }
 
     public static void main(String[] args) throws IOException {
		ScrolFan app = new ScrolFan();
	    app.setBackground(Color.WHITE);        
		JFrame frame = new JFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setContentPane(app);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setLocation(0,0);
    } 
}
Quelqu'un a t il une idée ?

Merci