Hi,
i have to add multiple tables in a jTextPane. I am able to add only one jTable, if i try for multiple tables old tables replaced with new onea and getting only single jTable on jTextPane.
here is my code
package nettry;
import hello.datadisplay;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class AddingTables {
JTextPane textPane;
public AddingTables() {
System.out.println("This one is running, nettry");
String[] text = {
"Name: Sandeep Singh\n " ,//text[0]
"ID:Auio\n" , // text[1] 0 - 89
"Designation:business manager\n",//text[2] // 90 - 91
"Location :Delhi\n \n" ,//text[3]
" Signature\n" ,//text[4]
" Name\n" , //text[5]
"" //text[6] 92 - 321
};
textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
createStyles(doc);
setContent(doc, text);
styleContent(doc);
}
private void createStyles(StyledDocument doc) {
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);
style = doc.addStyle("table", null);
StyleConstants.setComponent(style, getTableComponent(datadisplay.table));
StyleConstants.setComponent(style, getTableComponent(datadisplay.table1));
style = doc.addStyle("tableParagraph", null);
StyleConstants.setLeftIndent(style, 35f);
StyleConstants.setRightIndent(style, 35f);
StyleConstants.setSpaceAbove(style, 15f);
StyleConstants.setSpaceBelow(style, 15f);

}
private void createStyles1(StyledDocument doc) {
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);
style = doc.addStyle("table", null);
StyleConstants.setComponent(style, getTableComponent(datadisplay.table));
StyleConstants.setComponent(style, getTableComponent(datadisplay.table1));
style = doc.addStyle("tableParagraph", null);
StyleConstants.setLeftIndent(style, 35f);
StyleConstants.setRightIndent(style, 35f);
StyleConstants.setSpaceAbove(style, 15f);
StyleConstants.setSpaceBelow(style, 15f);

}
private void setContent(StyledDocument doc, String[] text) {
try {
doc.insertString(0, text[0]+text[1], doc.getStyle("base"));
doc.insertString(doc.getLength(), text[2]+text[3], doc.getStyle("base"));
System.out.println("First table printed" + doc.getLength());
doc.insertString(doc.getLength(), text[2], doc.getStyle("table"));
System.out.println("First table printed end"+ doc.getLength());
doc.insertString(doc.getLength(), text[4], doc.getStyle("base"));
System.out.println("First table printed end"+ doc.getLength());
doc.insertString(doc.getLength(), text[3], doc.getStyle("table"));
System.out.println("First table printed enddddd"+ doc.getLength());
doc.insertString(doc.getLength(),text[4], doc.getStyle("base"));
doc.insertString(doc.getLength(), text[4], doc.getStyle("table"));
doc.insertString(doc.getLength(), text[4], doc.getStyle("base"));
doc.insertString(doc.getLength(), text[5], 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 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);
}
private JScrollPane getTableComponent(JTable hh) {
Dimension d = hh.getPreferredSize();
d.width = 300;
hh.setPreferredScrollableViewportSize(d);
return new JScrollPane(hh);
}

private JTable getModel(JTable hh) {
return hh;
}
private JScrollPane getContent() {
return new JScrollPane(textPane);
}
public static void main(String[] args) {
System.setProperty("swing.aatext", "true");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new AddingTables().getContent());

f.setSize(500,400);
f.setLocation(200,200);
f.setVisible(true);
}
}