📄 textcomponent.java
字号:
* that is out of bounds, the method enforces these constraints
* silently, and without failure.
* @param selectionEnd the end position of the
* selected text.
* @see java.awt.TextComponent#getSelectionEnd
* @see java.awt.TextComponent#setSelectionStart
* @since JDK1.1
*/
public synchronized void setSelectionEnd(int selectionEnd) {
/* Route through select method to enforce consistent policy
* between selectionStart and selectionEnd.
*/
select(getSelectionStart(), selectionEnd);
}
/**
* Selects the text between the specified start and end positions.
* <p>
* This method sets the start and end positions of the
* selected text, enforcing the restriction that the end
* position must be greater than or equal to the start position.
* The start position must be greater than zero, and the
* end position must be less that or equal to the length
* of the text component's text. If the caller supplies values
* that are inconsistent or out of bounds, the method enforces
* these constraints silently, and without failure.
* @param selectionStart the start position of the
* text to select.
* @param selectionEnd the end position of the
* text to select.
* @see java.awt.TextComponent#setSelectionStart
* @see java.awt.TextComponent#setSelectionEnd
* @see java.awt.TextComponent#selectAll
* @since JDK1.0
*/
public synchronized void select(int selectionStart, int selectionEnd) {
String text = getText();
if (selectionStart < 0) {
selectionStart = 0;
}
if (selectionEnd > text.length()) {
selectionEnd = text.length();
}
if (selectionEnd < selectionStart) {
selectionEnd = selectionStart;
}
if (selectionStart > selectionEnd) {
selectionStart = selectionEnd;
}
this.selectionStart = selectionStart;
this.selectionEnd = selectionEnd;
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.select(selectionStart, selectionEnd);
}
}
/**
* Selects all the text in this text component.
* @see java.awt.TextComponent@select
* @since JDK1.0
*/
public synchronized void selectAll() {
String text = getText();
this.selectionStart = 0;
this.selectionEnd = getText().length();
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.select(selectionStart, selectionEnd);
}
}
/**
* Sets the position of the text insertion caret for
* this text component.
*
* @param position the position of the text insertion caret.
* @exception IllegalArgumentException if the value supplied
* for <code>position</code> is less than zero.
* @since JDK1.1
*/
public synchronized void setCaretPosition(int position) {
if (position < 0) {
throw new IllegalArgumentException("position less than zero.");
}
int maxposition = getText().length();
if (position > maxposition) {
position = maxposition;
}
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.setCaretPosition(position);
} else {
throw new IllegalComponentStateException("Cannot set caret position until after the peer has been created");
}
}
/**
* Gets the position of the text insertion caret for
* this text component.
* @return the position of the text insertion caret.
* @since JDK1.1
*/
public synchronized int getCaretPosition() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
int position = 0;
if (peer != null) {
position = peer.getCaretPosition();
}
return position;
}
/**
* Adds the specified text event listener to recieve text events
* from this textcomponent.
* @param l the text event listener
*/
public synchronized void addTextListener(TextListener l) {
textListener = AWTEventMulticaster.add(textListener, l);
newEventsOnly = true;
}
/**
* Removes the specified text event listener so that it no longer
* receives text events from this textcomponent
*/
public synchronized void removeTextListener(TextListener l) {
textListener = AWTEventMulticaster.remove(textListener, l);
}
// REMIND: remove when filtering is done at lower level
boolean eventEnabled(AWTEvent e) {
if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
textListener != null) {
return true;
}
return false;
}
return super.eventEnabled(e);
}
/**
* Processes events on this textcomponent. If the event is a
* TextEvent, it invokes the processTextEvent method,
* else it invokes its superclass's processEvent.
* @param e the event
*/
protected void processEvent(AWTEvent e) {
if (e instanceof TextEvent) {
processTextEvent((TextEvent)e);
return;
}
super.processEvent(e);
}
/**
* Processes text events occurring on this text component by
* dispatching them to any registered TextListener objects.
* NOTE: This method will not be called unless text events
* are enabled for this component; this happens when one of the
* following occurs:
* a) A TextListener object is registered via addTextListener()
* b) Text events are enabled via enableEvents()
* @see Component#enableEvents
* @param e the text event
*/
protected void processTextEvent(TextEvent e) {
if (textListener != null) {
int id = e.getID();
switch (id) {
case TextEvent.TEXT_VALUE_CHANGED:
textListener.textValueChanged(e);
break;
}
}
}
/**
* Returns the parameter string representing the state of this text
* component. This string is useful for debugging.
* @return the parameter string of this text component.
* @since JDK1.0
*/
protected String paramString() {
String str = super.paramString() + ",text=" + getText();
if (editable) {
str += ",editable";
}
return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
}
/*
* Serialization support. Since the value of the fields
* selectionStart, and selectionEnd, and text aren't neccessarily
* up to date we sync them up with the peer before serializing.
*/
private int textComponentSerializedDataVersion = 1;
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
{
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
text = peer.getText();
selectionStart = peer.getSelectionStart();
selectionEnd = peer.getSelectionEnd();
}
s.defaultWriteObject();
AWTEventMulticaster.save(s, textListenerK, textListener);
s.writeObject(null);
}
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException
{
s.defaultReadObject();
Object keyOrNull;
while(null != (keyOrNull = s.readObject())) {
String key = ((String)keyOrNull).intern();
if (textListenerK == key)
addTextListener((TextListener)(s.readObject()));
else // skip value for unrecognized key
s.readObject();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -