Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-27-2007, 08:05 PM
Member
 
Join Date: Jul 2007
Posts: 26
osval is on a distinguished road
how to insert tables into JTextPane
I have a JTextPane and I'm trying to insert tables into the JTextPane. I'm using the default rtf editor kit together with styled document.

I need a way to insert tables into the styled document of the JTextPane something like Microsoft word.

can you help me?
thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-29-2007, 10:11 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Adding table to textPane
Code:
import java.awt.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.text.*; public class AddingTables { JTextPane textPane; public AddingTables() { String[] text = { "This component models paragraphs that are composed of " + "runs of character level attributes.\n", // 0 - 89 " \n", // 90 - 91 "Each paragraph may have a logical style attached to it which " + "contains the default attributes to use if not overridden by " + "attributes set on the paragraph or character run. Components " + "and images may be embedded in the flow of text." // 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); 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()); 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], 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() { JTable table = new JTable(getModel()); Dimension d = table.getPreferredSize(); d.width = 300; table.setPreferredScrollableViewportSize(d); return new JScrollPane(table); } private AbstractTableModel getModel() { return new AbstractTableModel() { public int getColumnCount() { return 3; } public int getRowCount() { return 3; } public Object getValueAt(int row, int col) { return String.valueOf(row + 1) + (col + 1); } }; } 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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Insert Help!!!! shaungoater New To Java 1 06-14-2008 04:14 AM
How to insert graph in java valery Advanced Java 1 08-06-2007 09:38 PM
how to show a html document on JTextPane mary Advanced Java 2 08-02-2007 02:40 PM
How do insert a Graphic carl New To Java 1 08-01-2007 06:30 AM
Transparent JTextPane Ada AWT / Swing 1 05-31-2007 10:50 PM


All times are GMT +3. The time now is 04:14 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org