Methods Summary |
---|
public void | focusGained(java.awt.event.FocusEvent event)Turns on drawing of the component's thicker frame and the caret.
haveFocus = true;
repaint();
|
public void | focusLost(java.awt.event.FocusEvent event)Turns off drawing of the component's thicker frame and the caret.
haveFocus = false;
repaint();
|
public java.awt.font.TextHitInfo | getCaret()Returns a text hit info indicating the current caret (insertion point).
This class always returns a caret at the end of the text that the user
has entered. Subclasses may return a different caret or null.
return TextHitInfo.trailing(committedText.length() - 1);
|
public java.awt.Rectangle | getCaretRectangle()Returns a 0-width caret rectangle. This rectangle is derived from
the caret returned by getCaret. getCaretRectangle returns
null iff getCaret does.
TextHitInfo caret = getCaret();
if (caret == null) {
return null;
}
return getCaretRectangle(caret);
|
public java.awt.Rectangle | getCaretRectangle(java.awt.font.TextHitInfo caret)Returns a 0-width caret rectangle for the given text index.
It is calculated based on the text layout returned by getTextLayout,
so this method can be used for the entire displayed text.
TextLayout textLayout = getTextLayout();
int caretLocation;
if (textLayout != null) {
caretLocation = Math.round(textLayout.getCaretInfo(caret)[0]);
} else {
caretLocation = 0;
}
FontMetrics metrics = getGraphics().getFontMetrics();
return new Rectangle(textOriginX + caretLocation,
textOriginY - metrics.getAscent(),
0, metrics.getAscent() + metrics.getDescent());
|
public java.text.AttributedCharacterIterator | getCommittedText()Returns the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
AttributedString string = new AttributedString(committedText.toString());
return string.getIterator();
|
public java.text.AttributedCharacterIterator | getCommittedText(int beginIndex, int endIndex)Returns a subrange of the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
AttributedString string = new AttributedString(committedText.toString());
return string.getIterator(null, beginIndex, endIndex);
|
public int | getCommittedTextLength()Returns the length of the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
return committedText.length();
|
public java.text.AttributedCharacterIterator | getDisplayText()Returns the text that the user has entered.
As TextLayout requires a font to be defined for each character,
the default font is applied to the entire text.
A subclass that supports on-the-spot input must override this
method to include composed text.
AttributedString string = new AttributedString(committedText.toString());
if (committedText.length() > 0) {
string.addAttribute(TextAttribute.FONT, getFont());
}
return string.getIterator();
|
public synchronized java.awt.font.TextLayout | getTextLayout()Returns a text layout for the text that the user has entered.
This text layout is created from the text returned by getDisplayText.
The text layout is cached until invalidateTextLayout is called.
if (!validTextLayout) {
textLayout = null;
AttributedCharacterIterator text = getDisplayText();
if (text.getEndIndex() > text.getBeginIndex()) {
FontRenderContext context = ((Graphics2D) getGraphics()).getFontRenderContext();
textLayout = new TextLayout(text, context);
}
}
validTextLayout = true;
return textLayout;
|
public java.awt.Point | getTextOrigin()Returns the origin of the text. This is the leftmost point
on the baseline of the text.
return new Point(textOriginX, textOriginY);
|
public void | insertCharacter(char c)Inserts the given character at the end of the text.
committedText.append(c);
invalidateTextLayout();
|
public synchronized void | invalidateTextLayout()Invalidates the cached text layout. This must be called whenever
the component's text is modified.
validTextLayout = false;
|
public void | keyPressed(java.awt.event.KeyEvent event)Ignores key pressed events.
|
public void | keyReleased(java.awt.event.KeyEvent event)Ignores key released events.
|
public void | keyTyped(java.awt.event.KeyEvent event)Handles the key typed event. If the character is backspace,
the last character is removed from the text that the user
has entered. Otherwise, the character is appended to the text.
Then, the text is redrawn.
char keyChar = event.getKeyChar();
if (keyChar == '\b") {
int len = committedText.length();
if (len > 0) {
committedText.setLength(len - 1);
invalidateTextLayout();
}
} else {
insertCharacter(keyChar);
}
event.consume();
repaint();
|
public synchronized void | paint(java.awt.Graphics g)Draws the component. The following items are drawn:
- the component's background
- a frame, thicker if the component has the focus
- the component name
- the text that the user has entered
- the caret, if the component has the focus
// draw the background
g.setColor(getBackground());
Dimension size = getSize();
g.fillRect(0, 0, size.width, size.height);
// draw the frame, thicker if the component has the focus
g.setColor(Color.black);
g.drawRect(0, 0, size.width - 1, size.height - 1);
if (haveFocus) {
g.drawRect(1, 1, size.width - 3, size.height - 3);
}
// draw the component name
g.setColor(getForeground());
g.drawString(name, textOriginX, nameOriginY);
// draw the text that the user has entered
TextLayout textLayout = getTextLayout();
if (textLayout != null) {
textLayout.draw((Graphics2D) g, textOriginX, textOriginY);
}
// draw the caret, if the component has the focus
Rectangle rectangle = getCaretRectangle();
if (haveFocus && rectangle != null) {
g.setXORMode(getBackground());
g.fillRect(rectangle.x, rectangle.y, 1, rectangle.height);
g.setPaintMode();
}
|
public void | setFontSize(int size)
setFont(new Font("Dialog", Font.PLAIN, size));
nameOriginY = LINE_OFFSET + size;
textOriginX = 10;
textOriginY = 2 * (LINE_OFFSET + size);
|