Two Swing classes support styled text: JEditorPane and its subclass JTextPane.
The JEditorPane class is the foundation for Swing's styled text components and provides a
mechanism through which you can add support for custom text formats.
Editor panes, by default, know how to read, write, and edit plain, HTML, and RTF text.
Text panes inherit this capability but impose certain limitations.
As mentioned previously, a text pane requires its document to implement
the StyledDocument interface. The Swing text package provides a default
implementation of this interface, DefaultStyledDocument, which is the document that
text panes use by default. A text pane also requires that its editor kit be an instance of
a StyledEditorKit (or a subclass). Be aware that the read and write methods
for StyleEditorKit work with plain text.
Through their styled document and styled editor kit, text panes provide support for
named styles and logical styles. The JTextPane class itself contains many methods for
working with styles that simply call methods in its document or editor kit.
Through the API provided in the JTextPane class, you can embed images and
components in a text pane. You can embed images in an editor pane, too, but only by
including the images in an HTML or RTF file.
A text pane insists that its document implement
the StyledDocument
interface. HTMLDocument and RTFDocument are both StyledDocuments so HTML and
RTF work as expected within a text pane. If you load a text pane with plain text though,
the text pane's document is not a PlainDocumentas you might expect, but
a DefaultStyledDocument.
To support a custom text format, implement an editor kit that can read, write, and edit
text of that format. Then call theregisterEditorKitForContentType method to register
your kit with the JEditorPane class. By registering an editor kit in this way, all editor
panes and text panes in your program will be able to read, write, and edit the new
format. However, if the new editor kit is not a StyledEditorKit, text panes will not
support the new format.
Required Libraries:
ojdbc6.jar
2. Select Java from Categories and Java Application from Projects. Then Next.
3. Give the name of the application and click Finish.
4. Add required libraries .
5. Main Class
package jdbcstroedprocedures;
/**
*
* @author faisalijaz
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import static jdbcstroedprocedures.App.conn;
import static jdbcstroedprocedures.App.makeList;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
frame.setLayout(new BorderLayout());
panel.add(scroll1);
panel.add(scroll2);
frame.getContentPane().add(panel, BorderLayout.CENTER);
Action bold = new StyledEditorKit.BoldAction();
Action italic = new StyledEditorKit.ItalicAction();
Action underline = new StyledEditorKit.UnderlineAction();
Action highlight = new StyledEditorKit.ForegroundAction(null, Color.red);
JButton boldButton = new JButton(bold);
boldButton.setText("Bold");
buttonsPanel.add(boldButton);
buttonsPanel.add(underlineButton);
JButton highlightButton = new JButton(highlight);
highlightButton.setText("Highlight");
buttonsPanel.add(highlightButton);
buttonsSecondPanel.add(saveButton);
saveButton.addActionListener(new ActionListener() {
}
});
JButton loadButton = new JButton("load");
// buttonsSecondPanel.add(loadButton);
loadButton.addActionListener(new ActionListener() {
readintoDatabaseButton.addActionListener(new ActionListener() {
}
}
});
buttonsPanel.add(readintoDatabaseButton);
writeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
buttonsPanel.add(fontBox);
buttonsPanel.add(printButton);
htmlEditorPane.setContentType("text/html");
textPane_1.setContentType("text/html");
frame.add(buttonsPanel, BorderLayout.NORTH);
frame.add(buttonsSecondPanel, BorderLayout.SOUTH);
try {
// 1. Get a connection to database
myConn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.192.9.205:1521:legacy", "awais", "12345678");
// 2. Prepare statement
String sql = "update clobpractice set info=? where id=67";
// String query ="INSERT INTO employees (id,info) VALUES (3,'" + p.getText().toString() + "')";
myStmt = myConn.prepareStatement(sql);
// 4. Execute statement
System.out.println("\nStoring resume in database: " + theFile);
System.out.println(sql);
myStmt.executeUpdate();
System.out.println("\nCompleted successfully!");
//
else{
//
//
close(myConn, myStmt);
}
}
6. Run it
7. For Inserting formatted data into database the SaveToDatabase button reads the formatted
data from textpane that is on left side and insert it into database table(clob column) using
following command:
INSERT INTO awais.clobpractice (id,info) VALUES (74,'" + storedText + "')
Where storedText is the string that we get from textpane.
8. For reading formatted data from database that we just inserted using savetodatabase button
we will be using ReadFromDatabase button. By using this button we will get the formatted data
from database table(clobpratice) on the right sided textpane.