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