- PR -

JavaでJTextAreaの印刷

1
投稿者投稿内容
Karman
会議室デビュー日: 2004/01/21
投稿数: 1
投稿日時: 2004-01-21 14:55
はじめまして、Javaの初心者なのでわからないことがたくさんあるのですが。
JTextAreaの印刷をしたく思い、下記のコードを作成したのですが。
まったく印刷されません。
どなたか教えていただけたら幸いです。(無駄なコードも多々あると思います。)
プラットフォームは、WindowsXP pro,j2sdk1.4.2です。

コード:

JButton printButton = new JButton(new ImageIcon("image/print.gif"));
printButton.setRequestFocusEnabled(false);
printButton.setMargin(new Insets(1,1,1,1));
printButton.setToolTipText("印刷");
printButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
final int selectedIndex = myTabPane.getSelectedIndex();
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat fmt = job.pageDialog(job.defaultPage());
job.setPrintable(new Printable(){
int cnt = 0;
private JTextArea ta1 = myTextArea[selectedIndex];
private final int page = 33;
private final Font printFont = new Font("Courier",Font.PLAIN, 10);
private int i = 1;
private int ls = 0;
private int lc = ta1.getLineCount() - 1;
JTextArea ta = new JTextArea();
public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
ta.setDoubleBuffered(false);
ta.setLineWrap(false);
ta.setFont(printFont);
ta.setRows(page + 3);
ta.setColumns(132);
i = i+1;
int gBW = (int)ta.getBounds().width;
int gBH = (int)ta.getBounds().height;
if(cnt == 0) {
if(ls > lc) return Printable.NO_SUCH_PAGE;
try {
int off = ta1.getLineStartOffset(ls);
int le = (((i*page) < lc) ? (i*page) : lc);
int len = ta1.getLineEndOffset(le) - off;
ta.setText(ta1.getText(off,len));
for(int j=0; j<(page - (le-ls)); j++) ta.append("\n");
ta.append("\n \t \t \t \t \t \t \tPage "+i);
ls = le + 1;
}catch(Exception ex) {return Printable.NO_SUCH_PAGE;}
cnt++;
}else{
Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(),pf.getImageableY());
g2.setClip(0,0,gBW,gBH);
ta.paint(g2);
cnt = 0;
}
return Printable.PAGE_EXISTS;
}
},fmt);
if(job.printDialog()){
try{
job.print();
}catch(Exception ex) {
}
}
}
});
add(printButton);




[ メッセージ編集済み 編集者: Karman 編集日時 2004-01-21 15:38 ]
1

スキルアップ/キャリアアップ(JOB@IT)