Inserting components into a JTextPane

  • Thread starter Jonck van der Kogel
  • Start date
J

Jonck van der Kogel

Hi everybody,
I am trying to set up a JTextPane that will not only hold text but
components as well. So what I want is for a user to be able to type
some text in the JTextPane, then insert a component, type some more
text, etc...
The component that I want inserted only has a visual purpose, it
should be a piece of gray text that cannot be edited. So the user
should not be able to perceive any difference between the text that
he/she typed and the text that is painted on the component aside from
that the text on the component is gray and the normal text is black. I
want it to be like this so that the user either has a certain
component inserted or not, no individual letters of the component can
be deleted.

I'm using the insertComponent method of the JTextPane, but I'm not
altogether sure what type of Java component I should use to paint on.
I've tried overriding JLabel, JPanel and JComponent and so far only
JComponent gave any results.

The class looks like this (note it's only an experiment, so it's very
basic):

private class TextLabel extends JComponent {
String message = "Test";
Font myFont = new Font("Lucida Grande", Font.PLAIN, 13);
Dimension compDimensions;
int width;
int height;

public TextLabel(String message) {
this.message = message;
setBackground(Color.WHITE);
setForeground(Color.GRAY);
FontMetrics metrics = getFontMetrics(myFont);
width = metrics.stringWidth(message);
height = metrics.getHeight();

compDimensions = new Dimension(width, height);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(myFont);
g2.drawString(message, width, height);
}

public Dimension getMinimumSize() {
return compDimensions;
}

public Dimension getPreferredSize() {
return compDimensions;
}

}

Now when I use insertComponent method of JTextPane to insert my custom
JComponent, the JComponent is much too big (the text is drawn
correctly, but it has a lot of white space surrounding it). If I try
to extend JLabel or JPanel nothing appears whatsoever.

Does anybody have some tips for me how to get this working?

Thanks very much, Jonck
 
A

Andrew Thompson

I am trying to set up a JTextPane ..
(snip)
Does anybody have some tips for me how to get this working?

1) Post an example..
<http://www.physci.org/codes/sscce.jsp>
2) To the best group for GUI matters,
further described here..
<http://www.physci.org/codes/javafaq.jsp#cljg>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
A

Andrei Kouznetsov

I am trying to set up a JTextPane that will not only hold text but
components as well. So what I want is for a user to be able to type
some text in the JTextPane, then insert a component, type some more
text, etc...

did you tried to set border of your component to EmptyBorder?
alternatively you could also insert Icon.
write custom Icon which renders supplied text...
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top