bonsoir,

j'ai une classe qui affiche un aperçu avant impression d'un composant. voila ma classe :


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
public class PreviewDialog extends JDialog implements ActionListener {
 
	private final static double DEFAULT_ZOOM_FACTOR_STEP = 0.1;
	protected Pageable pageable;
 
	public PreviewDialog (String title, JFrame owner, Pageable pageable, double zoom){
		super(owner,title,true);
		this.pageable = pageable;
		Preview preview = new Preview(pageable, zoom);
		JScrollPane scrollpane = new JScrollPane(preview);
		getContentPane().add(scrollpane, "Center");
		JToolBar toolbar = new JToolBar();
		toolbar.add(getButton("Back24.gif", new BrowseAction(preview, -1)));
		toolbar.add(getButton("Forward.gif", new BrowseAction(preview, 1)));
		toolbar.add(new JToolBar.Separator());
		toolbar.add(getButton("ZoomIn24.gif", new ZoomAction(preview, DEFAULT_ZOOM_FACTOR_STEP)));
		toolbar.add(getButton("ZoomOut24.gif", new ZoomAction(preview, -DEFAULT_ZOOM_FACTOR_STEP)));
		toolbar.add(new JToolBar.Separator());
		JPanel dialog = new JPanel();
		dialog.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton ok=new JButton("OK");
		ok.addActionListener(this);
		dialog.add(ok);
		getContentPane().add(dialog, "South");
	}
 
	public PreviewDialog(String title, JFrame owner, Pageable pageable){
		this(title, owner, pageable, 0.0);
	}
 
	public PreviewDialog(String title, JFrame owner, Printable printable, PageFormat format, int pages, double zoom){
		this(title, owner, new MyPageable(printable, format,pages), zoom);
	}
 
	public PreviewDialog(String title, JFrame owner, Printable printable,PageFormat format, int pages){
		this(title, owner, printable, format,pages, 0.0);
	}
 
	public static class MyPageable implements Pageable {
 
		private Printable printable;
		private PageFormat format;
		private int pages;
 
		public MyPageable(Printable printable, PageFormat format, int pages){
			this.printable = printable;
			this.format = format;
			this.pages = pages;
		}
 
		public int getNumberOfPages() {
			return pages;
		}
 
		public PageFormat getPageFormat(int index){
				if (index >= pages) throw new IndexOutOfBoundsException ();
			return format;
		}
 
		public Printable getPrintable(int index){
			if (index >= pages) throw new IndexOutOfBoundsException ();
			return printable;
		}
 
	}
 
	private JButton getButton(String iconName){
		return getButton(null, iconName, null);
	}
 
	private JButton getButton(String iconName, AbstractAction action){
		return getButton(null, iconName, action);
	}
 
	private JButton getButton(String name, String iconName, AbstractAction action){
		JButton result = null;
		ImageIcon icon = null;
		URL imageURL = getClass().getClassLoader().getResource("images/"+iconName);
		if(imageURL != null)
			icon = new ImageIcon(imageURL);
 
		if(action != null){
			if(icon != null) action.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
			if(name != null) action.putValue(Action.NAME, name);
			result = new JButton(action); 
		} else 
			result = new JButton(name, icon);
		return result;
	}
 
	public void actionPerformed(ActionEvent e) {
			dispose();
	}
}

Comment je peut appeler cette classe avec un composant de type Pageable sachant que la conversion de Printable vers Pageable me donne une exception :

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
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.TablePrintable cannot be cast to java.awt.print.Pageable
	at de.java.print.TestPreview.actionPerformed(TestPreview.java:86)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
merci.