Otra opciones q puedes hacer es la siguiente
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
Código PHP:
public class Imprime extends JFrame
{
private JLabel jLabel1;
private JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JButton jButAbrir;
private JButton jButImprime;
private JPanel contentPane;
public Imprime()
{
super();
initializeComponent();
this.setVisible(true);
}
private void initializeComponent()
{
jLabel1 = new JLabel();
jTextArea1 = new JTextArea();
jScrollPane1 = new JScrollPane();
jButAbrir = new JButton();
jButImprime = new JButton();
contentPane = (JPanel)this.getContentPane();
jLabel1.setText("Archivo:");
jScrollPane1.setViewportView(jTextArea1);
jButAbrir.setHorizontalTextPosition(SwingConstants.CENTER);
jButAbrir.setIcon(new ImageIcon("buscar.PNG"));
jButAbrir.setText("Abrir");
jButAbrir.setToolTipText("Abrir");
jButAbrir.setVerticalAlignment(SwingConstants.TOP);
jButAbrir.setVerticalTextPosition(SwingConstants.BOTTOM);
jButAbrir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButAbrir_actionPerformed(e);
}
});
jButImprime.setHorizontalTextPosition(SwingConstants.CENTER);
jButImprime.setIcon(new ImageIcon("Imprime.PNG"));
jButImprime.setText("Imprimir");
jButImprime.setToolTipText("Imprimir");
jButImprime.setVerticalAlignment(SwingConstants.TOP);
jButImprime.setVerticalTextPosition(SwingConstants.BOTTOM);
jButImprime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButImprime_actionPerformed(e);
}
});
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 14,19,487,18);
addComponent(contentPane, jScrollPane1, 14,44,486,230);
addComponent(contentPane, jButAbrir, 126,285,90,80);
addComponent(contentPane, jButImprime, 296,285,90,80);
this.setTitle("Impresion de Archivos TXT");
this.setLocation(new Point(280, 170));
this.setSize(new Dimension(524, 411));
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
private void jButAbrir_actionPerformed(ActionEvent e)
{
System.out.println("\njButAbrir_actionPerformed(ActionEvent e) called.");
//Aqui permites escoger un txt para ponerlo en jtextarea
int state;
String msg;
FileDialog file = new FileDialog (Imprime.this, "Load File", FileDialog.LOAD);
file.setFile ("*.txt");
file.show();
String curFile;
if ((curFile = file.getFile()) != null)
{
String filename = file.getDirectory() + curFile;
char[] data;
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
File f = new File (filename);
try
{
FileReader fin = new FileReader (f);
int filesize = (int)f.length();
data = new char[filesize];
fin.read (data, 0, filesize);
}
catch (FileNotFoundException exc)
{
String errorString = "No se Encontro Archivo: " + filename;
data = errorString.toCharArray ();
}
catch (IOException exc)
{
String errorString = "Tipo de Error: " + filename;
data = errorString.toCharArray ();
}
jLabel1.setText ("Archivo: " + filename);
jTextArea1.setText (new String (data));
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
private void jButImprime_actionPerformed(ActionEvent e)
{
System.out.println("\njButImprime_actionPerformed(ActionEvent e) called.");
// Escoger tipo modelo y etc de fuente para ponerlo en una sola variable
String family = "Serif";
int style = Font.PLAIN;
int size = 20;
Font fuente = new Font(family, style, size);
//Terminacion de fuente
Properties defaultProps = new Properties();
PrintJob print=Toolkit.getDefaultToolkit().getPrintJob(this,"Imprimir",defaultProps);
Graphics g=print.getGraphics();
if(g!=null)
{
jTextArea1.printAll(g);
}
g.dispose();
print.end();
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new Imprime();
}
}