I am Doing Simple text Report on JTextArea
in JApplet
.
I am Trying to display this
String
of Array in Table format, it's quite work well but i want Integer
number hould be in RightAlign and Text should be in LeftAlign.as well as please suggest me JTextArea
is good or JTextpane
is ?
To make simple Text report.my next target is to get input from XML file and display as in table structure, i know its too hard to make such thing, but what they say about "Nothing is impossible". so plz someone help me to do this i am completely stuck with this.
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 java.util.MissingFormatArgumentException;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.border.Border;
public class MainReport extends JApplet implements ActionListener {
String[] colName = new String[] { "Date", "Account.No", "Description",
"Deposit", "Withdraw" };
// here is my input of String Array.
String a[][] = new String[][] {
{ "13/12/2013", "101", "AlphaSoftInfotekNashik", "3000", "0" },
{ "15/12/2013", "102", "Bank Ladger 2 xxxxxxxxxxxxx", "5000", "0" },
{ "16/12/2013", "103", "Accout Closing with Details", "800", "0" } };
StringBuilder sb = new StringBuilder();
final String Header = "%s %85s%n%n%s %10s%n";
// final String HRule ="";
final String format = "|%1$-30s|%2$-30s|%3$-30s|%4$-30s|%5$-35s";
final String NumColF = "";
String cformat = "%20s";
String format1 = "%20s";
String s1 = "%61s";// for spillover text
String s2 = "%10s";
int i, j, k;
int Position = 0;
boolean flag = false;
String a1, a2;
Container c;
JTextArea outputArea;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
JButton b;
public void init() {
c = getContentPane();
c.setLayout(new FlowLayout());
outputArea = new JTextArea(30, 60);
outputArea.setLocation(100, 100);
outputArea.setColumns(80);
outputArea.setLineWrap(true);
outputArea.setWrapStyleWord(false);
// outputArea= new JTextArea(LINES,CHAR_PER_LINE);
// outputArea.setSize(40, 40);
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) throws MissingFormatArgumentException {
// here i am trying to make header as well as footer//footer is not
// created yet but its my plan to add next.
outputArea.setEditable(false);
outputArea.setFont(new Font("Times New Roman", Font.PLAIN, 14));
outputArea.setText(" ");
outputArea.append(String.format("%s %n", " "));
outputArea.append(String.format(format, colName));
for (i = 0; i < a.length; i++) {
outputArea.append("\n");
for (j = 0; j < a[i].length; j++) {
if (a[i][j].length() > 20) {
a1 = a[i][j].substring(0, 20);
flag = true;
Position = j;
outputArea.append(a1 + String.format(s2, ' '));
a2 = a[i][j].substring(20);
} else {
outputArea.append(a[i][j] + String.format(cformat, ' '));
}
}
if (flag == true) {
outputArea.append(String.format("%n", ' '));
flag = false;
for (k = 0; k < a[i].length; k++) {
if (k == Position) {
outputArea.append(String.format(s1, ' ') + a2);
}
}
}
outputArea.append("\n");
}
}
}
You may use JEditorPane instead of JtextArea. there you use
SetContentType("text/html");
Method and then use html tables to display data.
http://www.w3schools.com/html/html_tables.asp
You may also use tabs '/t' to part the words to just look like a table and giving a '|' in between them...