[Swing]Créer un mini editeur de text (JEditorPane )en enregistrer en HTML
Salut,
Je suis en train d'ecrire un pogramme qui permettra de saisir du texte dans un JEditorPane. L'utilisateur pourra sélectionner une partie du texte et en cliquant sur le boutton: bold, le mettre en gras ect... Voici une version simplifiée du code qui marche. Il charge dans le JEditorPane un page HTML en guise d'exemple.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //for layout managers and more
import java.awt.event.*; //for action events
import java.net.URL;
import java.io.IOException;
public class EditorPane extends JPanel{
private String newline = "\n";
private JPanel buttonPanel;
private JPanel textPanePanel;
private JEditorPane myEditorPane;
private JButton boldButton;
private JButton colorButton;
private JButton imgButton;
private JButton saveButton;
public EditorPane() {
createGUI();
}
private void createGUI() {
buttonPanel = new JPanel();
boldButton = new JButton("Bold");
boldButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
boldButtonActionPerformed(evt);
}
});
colorButton = new JButton("Color");
colorButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
colorButtonActionPerformed(evt);
}
});
imgButton = new JButton("Image");
imgButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
imgButtonActionPerformed(evt);
}
});
saveButton = new JButton("Save");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
buttonPanel.setLayout(new GridLayout(1, 4));
buttonPanel.add(boldButton);
buttonPanel.add(colorButton);
buttonPanel.add(imgButton);
buttonPanel.add(saveButton);
textPanePanel = new JPanel();
textPanePanel.setLayout(new BorderLayout());
myEditorPane = this.createEditorPane();
JScrollPane paneScrollPane = new JScrollPane(myEditorPane);
textPanePanel.add(paneScrollPane, BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(textPanePanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("TextSamplerDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditorPane newContentPane = new EditorPane();
//newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
}
});
}
private JEditorPane createEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(true);
java.net.URL helpURL = TextSamplerDemo.class.getResource(
"simpleHTMLPage.html");
if (helpURL != null) {
try {
editorPane.setPage(helpURL);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + helpURL);
}
} else {
System.err.println("Couldn't find file: simpleHTMLPage.html");
}
return editorPane;
}
private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("boldButtonNextActionPerformed");
//myEditorPane
// TODO add your handling code here:
}
private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("colorButtonActionPerformed");
// TODO add your handling code here:
}
private void imgButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("imgButtonActionPerformed");
// TODO add your handling code here:
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("saveButtonActionPerformed");
String newStr = new String("this is the new String");
//newStr.setFont(new java.awt.Font("Tahoma", 0, 14));
this.myEditorPane.replaceSelection("sdfsdfsd");
// TODO add your handling code here:
}
} |
Et le code du fichier HTML est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Simple text</p>
<p><strong>Bold text</strong></p>
<p><em>Italic text</em></p>
<p align="center">Center text</p>
<p><font color="#000099"><strong>Color text</strong></font></p>
<p>image here : <img src="images/Pig.gif" width="121" height="129"></p>
<p> </p>
</body>
</html> |
Egalement je voudrais qu'en cliquant sur le bouton Save on enregistre et exporte le contenu du JEditorPane en HTML. Actuellement il change seulement le texte selectionné.
Toute suggestions ou bouts de code pouvant m'aider serait grandement apprecié.
Merci.