I am trying to extend the StyledEditorKit in Swing to be able to include JLabel inside the editor. I was able to do this, and this is what I got so far. In the image below, the highlighted text button is of type JLabel, while the rest of the text is plain text.

As you can see, the label looks a little lower than the plain text. How to align the top of the top of the remaining text?
Here is the view code that is used to create this label element:
class ComponentView(Element elem) {
@Override
protected Component createComponent() {
JLabel lbl = new JLabel("");
lbl.setOpaque(true);
lbl.setBackground(Color.red);
try {
int start = getElement().getStartOffset();
int end = getElement().getEndOffset();
String text = getElement().getDocument().getText(start, end - start);
lbl.setText(text);
} catch (BadLocationException e) {}
return lbl;
}
}
source
share