Wednesday, June 18, 2014

Java print Simple report Document on JTextArea


package reportlast;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.border.Border;
/**
 *  @author icebreakersandy
 *  email: sndpdevhare10@gmail.com
 */
public class Testit extends JApplet implements ActionListener {
String[] colName = new String[] { "Date", "Account.No", "Description",
"Deposit", "Withdraw" };
String A[][] = {
{ "13/12/2013", "101", "AlphaSoft Infotek Ltd. Nashik", "3000", "0" },
{ "15/12/2013", "103", "Bank Ladger ", "5000", "0" } };
String[][] B = new String[3][5];
String[] K = new String[5];
Container c;
JTextArea outputArea;
JButton b;
public void init() {
c = getContentPane();
c.setLayout(new FlowLayout());
outputArea = new JTextArea(20, 80);
Border border = BorderFactory.createLineBorder(Color.RED);
outputArea.setBorder(BorderFactory.createCompoundBorder(border,BorderFactory.createEmptyBorder(10, 10, 10, 10)));
c.add(outputArea);
b = new JButton("Show Report");
b.addActionListener(this);
c.add(b);
}
public void actionPerformed(ActionEvent e) {
outputArea.setEditable(false);
outputArea.setFont(new Font("Times New Roman", Font.PLAIN, 14));
outputArea.setText(" ");
outputArea.append("ALPHASOFT -- BANK LADGER , REPORTS.\n\n");
// outputArea.append("Sr.No");
for (int j = 0; j < colName.length; j++) {
outputArea.append("\t" + colName[j]);
}
outputArea.append("\n");
for (int i = 0; i < A.length; i++) {
int colv = A[i].length;
String D = new String(" ");
// String D = "";
String Z[][] = A;
String Y[] = new String[5];
for (int ZX = 0; ZX < A[i].length; ZX++) {
Y[ZX] = A[i][ZX];
}
while (Y != null) {
System.out.println(Y[i]);
Z[i] = Y;
Y = null;
D = " ";
for (int nx = 0; nx < A[i].length; nx++) {
if (Z[i][nx].length() > 20) {
int stln = 20;
for (int zx = 20; zx > 0; zx--) {
char result = Z[i][nx].charAt(zx);
if (result == ' ') {
stln = zx + 1;
break;
}
}
D += rightpad(Z[i][nx].substring(0, stln), 20, "R");
Y[nx] = Z[i][nx].substring(stln);
} else {
D += rightpad(Z[i][nx], 20, "R");
Y[nx] = null;
}
}

outputArea.append(D + "\n");

}

}
}
public static String rightpad(String inp, int ln, String rl) {
String pd = "";
for (int nx = 0; nx < ln - inp.length(); nx++) {
pd += " ";
}
if (rl == "R") {
pd = inp + pd;
} else {

pd = pd + inp;
}

return pd;
}

}

No comments:

Post a Comment