I'm trying to set the alignment when inserting an icon into a
jtextpane to center when I insert but I'm a little confused on how to
do it? I'm trying to do it this way but of course it doesn't work?
MutableAttributeSet attr = new
SimpleAttributeSet();
doc.setText("");
StyleConstants.setAlignment(attr,
StyleConstants.ALIGN_CENTER);
StyleConstants.setIcon(attr, ImgThumbnail);
//try to insert image
try {
(this.doc.getDocument()).insertString(
doc.getCaretPosition(),
" ", attr);
new StyledEditorKit.AlignmentAction("",
StyleConstants.ALIGN_CENTER);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
ex,
"",
JOptionPane.ERROR_MESSAGE);
}
Andrew T. - 06 Jul 2006 06:32 GMT
> I'm trying to set the alignment when inserting an icon into a
> jtextpane ..
Why? Are there other components in the JTextPane?
If the icon is the only component, it makes more sense to
put the icon in a JLabel then use an appropriate layout
to align the label.
If the icon is amongst other elements in the JTextPane,
using alignment constraints on the icon itself seems rather
pointless. Instead you might look to positioning the icon
amongst HTML that is displayed in a JEditorPane.
Andrew T.
Chris Smith - 06 Jul 2006 07:23 GMT
> I'm trying to set the alignment when inserting an icon into a
> jtextpane to center when I insert but I'm a little confused on how to
> do it? I'm trying to do it this way but of course it doesn't work?
> //try to insert image
> try {
[quoted text clipped - 3 lines]
> new StyledEditorKit.AlignmentAction("",
> StyleConstants.ALIGN_CENTER);
Thatg certainly won't work. You are getting an Action object that
represents centering smoething, but then never using it. Actually,
Action objects are normall used for event handling plumbing. If you
just want to center something, look at StyledDocument's method called
setParagraphAttributes.

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
Barkster - 06 Jul 2006 15:13 GMT
Thanks I'll look at that
> > I'm trying to set the alignment when inserting an icon into a
> > jtextpane to center when I insert but I'm a little confused on how to
[quoted text clipped - 17 lines]
> Chris Smith - Lead Software Developer / Technical Trainer
> MindIQ Corporation