import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class AddingTables {
JTextPane textPane;
private Object[][] rows;
private Object[] colNames;
public AddingTables(Object[][] rows, Object[] colNames) {
// String[] text = {"This is the Church Visitation Thingy \n" , " \n", "Mike"};
String[] text = {
"\n" + "\n" + "\n" + "The Heading \n", // 0 - 89
" \n", // 90 - 91
"" // 92 - 321
};
this.rows = rows;
this.colNames = colNames;
JTextField jtf = new JTextField();
jtf.setText("April 15th, 2010");
JTextArea jta = new JTextArea();
jta.setText("We all need to fight Barack Obama. He cannot win. " +
"Obama is our enemy. He hates America. He is a traitor.");
textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
createStyles(doc, rows, colNames);
setContent(doc, text);
styleContent(doc);
StyledDocument document = (StyledDocument) textPane.getDocument();
JTextField jtf2 = new JTextField("March 15th, 2014");
JTextArea area = new JTextArea("We fought Barack Obama today. He was beat up.");
String lineString = "_________________________________________________________________________________________";
JTextField jtf3 = new JTextField("April 7th, 2014");
JTextArea jta2 = new JTextArea("Paul Adcock, the guy who will help save America, had his 25th Birthday today. \n" + "There was a big party. We had lots of fun.");
JTextField jtf4 = new JTextField("July 4th, 2014");
JTextArea jta3 = new JTextArea("Barry tried to ruin our Independence Day today by using his pen and phone to push Amnesty. \n" +
"However, he didn't count on Texas and others refusing to implement it. Also, we had a big protest at the border. \n" +
"Barry woulda no doubt sent in his drones but the state militias came to our aide. We've started the Convention of the States movement in earnest!");
JTextField jtf5 = new JTextField("November 4th, 2014");
JTextArea jta4 = new JTextArea("Today, we took the Senate. Also, we got a lot of Tea Party guys in to replace the RINOs.");
String newStuff = lineString + "\n" + jtf.getText() + "\n" + jta.getText() + "\n" + lineString + "\n" + jtf2.getText() + "\n" + area.getText() + "\n" + lineString + "\n" + jtf3.getText() + "\n" +
jta2.getText() + "\n" + lineString + "\n" + jtf4.getText() + "\n" + jta3.getText() + "\n" + lineString + "\n" + jtf5.getText() + "\n" + jta4.getText() + "\n" + lineString + "\n";
try
{
document.insertString(document.getLength(), newStuff, null);
}
catch(BadLocationException ble)
{
}
}
public JTextPane getTextPane()
{
return textPane;
}
private void createStyles(StyledDocument doc, Object[][] rows, Object[] colNames) {
Style baseStyle = doc.addStyle("base", null);
StyleConstants.setFontFamily(baseStyle, "Lucida Sans Unicode");
StyleConstants.setFontSize(baseStyle, 18);
StyleConstants.setFirstLineIndent(baseStyle, 20f);
StyleConstants.setLeftIndent(baseStyle, 10f);
Style style = doc.addStyle("bold", baseStyle);
StyleConstants.setBold(style, true);
style = doc.addStyle("italic", baseStyle);
StyleConstants.setItalic(style, true);
style = doc.addStyle("blue", baseStyle);
StyleConstants.setForeground(style, Color.blue);
style = doc.addStyle("underline", baseStyle);
StyleConstants.setUnderline(style, true);
style = doc.addStyle("green", baseStyle);
StyleConstants.setForeground(style, Color.green.darker());
StyleConstants.setUnderline(style, true);
style = doc.addStyle("highlight", baseStyle);
StyleConstants.setForeground(style, Color.yellow);
StyleConstants.setBackground(style, Color.black);
style = doc.addStyle("table", null);
StyleConstants.setComponent(style, getTableComponent(rows, colNames));
style = doc.addStyle("tableParagraph", null);
StyleConstants.setLeftIndent(style, 12f);
StyleConstants.setRightIndent(style, 12f);
StyleConstants.setSpaceAbove(style, 15f);
StyleConstants.setSpaceBelow(style, 15f);
}
private void setContent(StyledDocument doc, String[] text) {
try {
doc.insertString(0, text[0], doc.getStyle("base"));
doc.insertString(doc.getLength(), text[1], doc.getStyle("table"));
doc.insertString(doc.getLength(), text[2], doc.getStyle("base"));
} catch(BadLocationException e) {
System.out.printf("Bad location error: %s%n", e.getMessage());
}
}
private void styleContent(StyledDocument doc) {
Style style = doc.getStyle("base");
doc.setLogicalStyle(0, style);
/*
style = doc.getStyle("underline");
doc.setCharacterAttributes(22, 10, style, false);
style = doc.getStyle("highlight");
doc.setCharacterAttributes(62, 26, style, false);
*/
Style logicalStyle = doc.getLogicalStyle(0);
style = doc.getStyle("tableParagraph");
doc.setParagraphAttributes(90, 1, style, false);
style = doc.getStyle("table");
doc.setCharacterAttributes(90, 1, style, false);
doc.setLogicalStyle(92, logicalStyle);
/*
style = doc.getStyle("blue");
doc.setCharacterAttributes(118, 13, style, false);
style = doc.getStyle("italic");
doc.setCharacterAttributes(166, 18, style, false);
style = doc.getStyle("green");
doc.setCharacterAttributes(235, 9, style, false);
doc.setCharacterAttributes(248, 9, style, false);
style = doc.getStyle("bold");
doc.setCharacterAttributes(263, 10, style, false);
doc.setCharacterAttributes(278, 6, style, false);
*/
}
private JScrollPane getTableComponent(Object[][] rows, Object[] colNames) {
JTable table = new JTable(new SpecialTableModel(rows, colNames));
Dimension d = table.getPreferredSize();
d.width = 300;
table.setPreferredScrollableViewportSize(d);
return new JScrollPane(table);
}
private JScrollPane getContent() {
return new JScrollPane(textPane);
}
protected class SpecialTableModel extends DefaultTableModel
{
private Object[][] rows;
private Object[] cNames;
public SpecialTableModel(Object[][] rows, Object[] cNames)
{
super(rows, cNames);
this.rows = rows;
this.cNames = cNames;
}
// public int getColumnCount() {return cNames.length;}
// public int getRowCount() { return rows.length;}
public Object getValueAt(int row, int col)
{
return String.valueOf(rows[row][col]);
}
}
public static void main(String[] args) {
System.setProperty("swing.aatext", "true");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object[][] rows = new Object[1][5];
rows[0][0] = "Mongoose";
rows[0][1] = "Penugin";
rows[0][2] = "Mark Levin";
rows[0][3] = "Rikki-tikki-tavi";
rows[0][4] = "America";
Object[] colNames = new Object[5];
colNames[0] = "Name";
colNames[1] = "Address";
colNames[2] = "City";
colNames[3] = "First Name";
colNames[4] = "Last Name";
final AddingTables at = new AddingTables(rows, colNames);
JMenuBar jmb = new JMenuBar();
f.setJMenuBar(jmb);
JMenu file = new JMenu("File");
jmb.add(file);
JMenuItem print = new JMenuItem("Print");
file.add(print);
print.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
try
{
at.getTextPane().print();
}
catch(java.awt.print.PrinterException pe)
{
}
}});
f.setContentPane(at.getContent());
// at.getTextPane().setFont(new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, 12));
f.setSize(500,400);
f.setLocation(200,200);
f.setVisible(true);
}
}