lining up text with components in a JTextPane
I have a JTextPane that contains other components as well as Strings, and I want the text of the components to line up with the rest of the text. If you run this example, you can see how the component text is below the rest of the text on the line. Any suggestions on how to fix or work around this would be appreciated.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
publicclass TextAlignmentDemoextends JFrame
{
public TextAlignmentDemo()
{
super("Text Alignment Demo");
JTextPane textPane =new JTextPane();
textPane.setEditable(false);
StyleContext sContext = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sContext.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.Foreground, Color.black);
Document document = textPane.getDocument();
getContentPane().add(textPane);
String[] boxText ={"component text"};
JComboBox b =new JComboBox(boxText);
try
{
document.insertString(document.getLength(),"text is out of line with ", aSet);
textPane.setCaretPosition(document.getLength());
textPane.insertComponent(b);
}
catch(BadLocationException e)
{
e.printStackTrace();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,70);
setVisible(true);
}
publicstaticvoid main(String[] args)
{
new TextAlignmentDemo();
}
}

