Salut tous,

Je cherche à personnaliser la couleur, la taille, la police et la forme de mon texte écrit sur un JTextPane
Ce que je désire développer rassemble trop à l'éditeur du word, yahooMail, etc. ..
Voilà le code que j'ai et qui regroupe toutes les fonctionnalités souhaitées.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
 
public class JFontChooser extends JDialog implements ActionListener {
 
  /** DOCUMENT_ME */
  private JColorChooser colorChooser = null;
 
  /** DOCUMENT_ME */
  private JComboBox fontName = null;
 
  /** DOCUMENT_ME */
  private JCheckBox fontBold;
 
  /** DOCUMENT_ME */
  private JCheckBox fontItalic = null;
 
  /** DOCUMENT_ME */
  private JSpinner fontSize = null;
 
  /** DOCUMENT_ME */
  private JLabel previewLabel = null;
 
  /** DOCUMENT_ME */
  private SimpleAttributeSet attributes = null;
 
  /** DOCUMENT_ME */
  private Font newFont = null;
 
  /** DOCUMENT_ME */
  private Color newColor = null;
 
  /** DOCUMENT_ME */
  private JComponent compo = null;
 
  /**
   * Creates a new JFontChooser object.
   *
   * @param parent DOCUMENT ME!
   * @param compo DOCUMENT ME!
   */
 
  public JFontChooser(Frame parent, JComponent compo) {
    super(parent, "Font Chooser", true);
    this.compo = compo;
 
    setSize(450, 450);
    attributes = new SimpleAttributeSet();
 
    // Make sure that any way the user cancels the window does the right thing
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          closeAndCancel();
        }
      });
 
    // Start the long process of setting up our interface
    Container c = getContentPane();
 
    JPanel fontPanel = new JPanel();
    fontName = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment()
                                                .getAvailableFontFamilyNames());
    fontName.setSelectedItem(compo.getFont().getName());
    fontName.addActionListener(this);
    fontSize = new JSpinner(new SpinnerNumberModel(compo.getFont().getSize(),
          4, 50, 2));
    fontSize.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent arg0) {
          actionPerformed(null);
        }
      });
    fontBold = new JCheckBox("Bold");
    fontBold.setSelected(compo.getFont().isBold());
    fontBold.addActionListener(this);
    fontItalic = new JCheckBox("Italic");
    fontItalic.setSelected(compo.getFont().isItalic());
    fontItalic.addActionListener(this);
 
    fontPanel.add(fontName);
    fontPanel.add(new JLabel(" Size: "));
    fontPanel.add(fontSize);
    fontPanel.add(fontBold);
    fontPanel.add(fontItalic);
 
    c.add(fontPanel, BorderLayout.NORTH);
 
    // Set up the color chooser panel and attach a change listener so that color
    // updates get reflected in our preview label.
    colorChooser = new JColorChooser(compo.getForeground());
    colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
          updatePreviewColor();
        }
      });
    c.add(colorChooser, BorderLayout.CENTER);
 
    JPanel previewPanel = new JPanel(new BorderLayout());
    previewLabel = new JLabel("Le résultat ressemblera un peu à ça");
    previewLabel.setBackground(Color.WHITE);
    previewLabel.setForeground(colorChooser.getColor());
    previewPanel.add(previewLabel, BorderLayout.CENTER);
 
    // Add in the Ok and Cancel buttons for our dialog box
    JButton okButton = new JButton("Ok");
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          closeAndApply();
        }
      });
 
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          closeAndCancel();
        }
      });
 
    JPanel controlPanel = new JPanel();
    controlPanel.add(okButton);
    controlPanel.add(cancelButton);
    previewPanel.add(controlPanel, BorderLayout.SOUTH);
 
    // Give the preview label room to grow.
    previewPanel.setMinimumSize(new Dimension(100, 100));
    previewPanel.setPreferredSize(new Dimension(100, 100));
 
    c.add(previewPanel, BorderLayout.SOUTH);
    actionPerformed(null);
  }
 
 
  // Ok, something in the font changed, so figure that out and make a
  // new font for the preview label
  public void actionPerformed(ActionEvent ae) {
    // Check the name of the font
    if (!StyleConstants.getFontFamily(attributes).equals(fontName.getSelectedItem())) {
      StyleConstants.setFontFamily(attributes,
        (String) fontName.getSelectedItem());
    }
 
    // Check the font size (no error checking yet)
    if (StyleConstants.getFontSize(attributes) != ((Integer) fontSize.getValue()).intValue()) {
      StyleConstants.setFontSize(attributes,
        ((Integer) fontSize.getValue()).intValue());
    }
 
    // Check to see if the font should be bold
    if (StyleConstants.isBold(attributes) != fontBold.isSelected()) {
      StyleConstants.setBold(attributes, fontBold.isSelected());
    }
 
    // Check to see if the font should be italic
    if (StyleConstants.isItalic(attributes) != fontItalic.isSelected()) {
      StyleConstants.setItalic(attributes, fontItalic.isSelected());
    }
 
    // and update our preview label
    updatePreviewFont();
  }
 
  // Get the appropriate font from our attributes object and update
  // the preview label
  protected void updatePreviewFont() {
    String name = StyleConstants.getFontFamily(attributes);
    boolean bold = StyleConstants.isBold(attributes);
    boolean ital = StyleConstants.isItalic(attributes);
    int size = StyleConstants.getFontSize(attributes);
 
    //Bold and italic don’t work properly in beta 4.
    Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0),
        size);
    previewLabel.setFont(f);
  }
 
  // Get the appropriate color from our chooser and update previewLabel
  protected void updatePreviewColor() {
    previewLabel.setForeground(colorChooser.getColor());
 
    // Manually force the label to repaint
    previewLabel.repaint();
  }
 
  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public Font getNewFont() {
    return newFont;
  }
 
  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public Color getNewColor() {
    return newColor;
  }
 
  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public AttributeSet getAttributes() {
    return attributes;
  }
 
  /**
   * DOCUMENT ME!
   */
  public void closeAndApply() {
    // Save font & color information
    newFont = previewLabel.getFont();
    newColor = previewLabel.getForeground();
 
    // apply the changes to the jcomponent
    compo.setFont(newFont);
    compo.setForeground(newColor);
 
    // Close the window
    setVisible(false);
  }
 
  /**
   * DOCUMENT ME!
   */
  public void closeAndCancel() {
    // Erase any font information and then close the window
    newFont = null;
    newColor = null;
    setVisible(false);
  }
 
    int showDialog(Container parent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
}
Le problème c'est comment faire pour appeler cet éditeur?
Et puis, comment valider le choix sur le texte déjà écrit?

Merci pour votre attention..