Center Icon in JTextPane

B

Barkster

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);

}
 
A

Andrew T.

Barkster said:
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.
 
C

Chris Smith

Barkster said:
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 {
(this.doc.getDocument()).insertString(
doc.getCaretPosition(),
" ", attr);
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.
 
B

Barkster

Thanks I'll look at that

Chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top