Center Icon in JTextPane

Discussion in 'Java' started by Barkster, Jul 6, 2006.

  1. Barkster

    Barkster Guest

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

    }
    Barkster, Jul 6, 2006
    #1
    1. Advertising

  2. Barkster

    Andrew T. Guest

    Barkster wrote:
    > 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.
    Andrew T., Jul 6, 2006
    #2
    1. Advertising

  3. Barkster

    Chris Smith Guest

    Barkster <> wrote:
    > 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.

    --
    Chris Smith - Lead Software Developer / Technical Trainer
    MindIQ Corporation
    Chris Smith, Jul 6, 2006
    #3
  4. Barkster

    Barkster Guest

    Thanks I'll look at that

    Chris Smith wrote:
    > Barkster <> wrote:
    > > 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.
    >
    > --
    > Chris Smith - Lead Software Developer / Technical Trainer
    > MindIQ Corporation
    Barkster, Jul 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miro Durcik

    app.rc resource's icon to .net Icon

    Miro Durcik, Oct 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,381
    Miro Durcik
    Oct 9, 2004
  2. rj

    JTextPane columnar data

    rj, Sep 1, 2003, in forum: Java
    Replies:
    1
    Views:
    491
    Sandip Chitale
    Sep 1, 2003
  3. Adam
    Replies:
    0
    Views:
    438
  4. Dan Polansky
    Replies:
    3
    Views:
    789
    Larry Barowski
    Feb 12, 2007
  5. Bodi
    Replies:
    1
    Views:
    272
    Lasse Reichstein Nielsen
    Nov 18, 2003
Loading...

Share This Page