By ka3ak


2012-12-18 12:48:45 8 Comments

Currently it looks so

enter image description here

What to do so that it looks so?

enter image description here

Below is my code:

    JFrame f = new JFrame();
    JTextPane textPane = new JTextPane();

    JTextField component = new JTextField("      ");
    component.setMaximumSize(component.getPreferredSize());
    textPane.setSelectionStart(textPane.getDocument().getLength());
    textPane.setSelectionEnd(textPane.getDocument().getLength());
    textPane.insertComponent(component);
    try {
        textPane.getDocument().insertString(textPane.getDocument().getLength(), "text",
            new SimpleAttributeSet());
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    f.add(new JScrollPane(textPane));
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

The single question which is near to this topic I found: JTextPane insert component, faulty vertical alignment But there is no answer how to change the alignment. But it must be possible according to the discussion there.

3 comments

@user114676 2013-11-18 12:59:53

Based on the answer above (which didn't work for me, but helped me find this), I used:

Style s = doc.addStyle("icUf", regular);        
ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
if (icUf != null) {
    // create label with icon AND text
    JLabel jl = new JLabel("some text",icUf, SwingConstants.LEFT);
    StyleConstants.setComponent(s, jl);
}
doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"))

This properly aligned the text 'some text' and the icon.

@david 2013-10-03 08:40:55

Define a style for your document with a JLabel and set the vertical aligment on it:

Style s = doc.addStyle("icUf", regular);        
ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
if (icUf != null) {
    JLabel jl = new JLabel(icUf);
    jl.setVerticalAlignment(JLabel.CENTER);
    StyleConstants.setComponent(s, jl);
}

Insert the label:

doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"));

and the text:

doc.insertString(doc.getLength(), " text ", doc.getStyle("bold"));

@StanislavL 2012-12-18 13:12:38

You can use this http://java-sl.com/tip_center_vertically.html

It should work with JComponents as well.

You can also override LabelView's getPreferredSpan() adding some space to the bottom.

Alternatively you can try to override RowView inner class in ParagraphView

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/ParagraphView.java#ParagraphView.Row

That points to inner class Row extends BoxView

You should replace it with own one. Try to override

public float getAlignment(int axis) 

to return CENTER (0.5). If this does not help override layoutMinorAxis(0 to return proper offsets (shifted).

@ka3ak 2012-12-18 15:08:14

getPrefferedSpan() does the job. I can adjust text position with a constant value. But what if components of different size are used? I want everything to look similar to a skewer of meat. Although the cuts of meat can have different size they are all skewered through their center. Skewer is text. Can this be done with "RowView" and "ParagraphView"? If so, I couldn't understand how a class can be overridden and there is also no such inner class in the ParagraphView.

@ka3ak 2012-12-18 15:42:47

Thanks! The overriding of getAlignment() has helped.

@tenorsax 2014-09-17 23:22:39

@StanislavL Thank you for CenteredBoxView, +1! One question though, shouldn't it be textBlockHeight += spans[i]; in CenteredBoxView.layoutMajorAxis() instead of textBlockHeight = spans[i];?

Related Questions

Sponsored Content

55 Answered Questions

[SOLVED] How to fix 'android.os.NetworkOnMainThreadException'?

103 Answered Questions

[SOLVED] How to horizontally center a <div>

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

65 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3924978 View
  • 3387 Score
  • 65 Answer
  • Tags:   java random integer

45 Answered Questions

[SOLVED] How to vertically center a div for all browsers?

42 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

41 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

28 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1897790 View
  • 2201 Score
  • 28 Answer
  • Tags:   java arrays

38 Answered Questions

[SOLVED] How to align checkboxes and their labels consistently cross-browsers

Sponsored Content