< prev index next >
src/java.desktop/share/classes/java/awt/TextComponent.java
Print this page
*** 79,88 ****
--- 79,97 ----
* @see #isEditable()
*/
boolean editable = true;
/**
+ * A boolean indicating whether EOL characters present in the input
+ * text should be replaced by a space character.
+ * This variable is set to <code>true</code> for <code>TextField</code>.
+ * If set to <code>true</code> EOL will be replaced with space.
+ * If set to <code>false</code> text remains unchanged.
+ */
+ boolean replaceEOL;
+
+ /**
* The selection refers to the selected text, and the
* <code>selectionStart</code> is the start position
* of the selected text.
*
* @serial
*** 134,143 ****
--- 143,191 ----
GraphicsEnvironment.checkHeadless();
this.text = (text != null) ? text : "";
setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
+ /**
+ * Constructs a new text component. Replaces EOL character from the input
+ * text with space character if the flag replaceEOL is <code>true</code>
+ * and also sets the value of the cursor to <code>Cursor.TEXT_CURSOR</code>.
+ * @param text the text to be displayed; if
+ * <code>text</code> is <code>null</code>, the empty
+ * string <code>""</code> will be displayed
+ * @param replaceEOL boolean flag to decide whether to replace
+ * EOL or not.
+ * @exception HeadlessException if
+ * <code>GraphicsEnvironment.isHeadless</code>
+ * returns true
+ * @see java.awt.GraphicsEnvironment#isHeadless
+ * @see java.awt.Cursor
+ */
+ TextComponent(String text, boolean replaceEOL) throws HeadlessException {
+ GraphicsEnvironment.checkHeadless();
+ this.text = (text != null) ? text : "";
+ if (replaceEOL) {
+ this.replaceEOL = replaceEOL;
+ if (this.text != "") {
+ replaceEOL();
+ }
+ }
+ setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
+ }
+
+ /**
+ * Replaces EOL characters from the text variable with a space character.
+ */
+ private void replaceEOL() {
+ String[] strEOLs = {System.getProperty("line.separator"), "\n"};
+ for (String eol : strEOLs) {
+ if (text.contains(eol)) {
+ text = text.replace(eol, " ");
+ }
+ }
+ }
+
private void enableInputMethodsIfNecessary() {
if (checkForEnableIM) {
checkForEnableIM = false;
try {
Toolkit toolkit = Toolkit.getDefaultToolkit();
*** 230,239 ****
--- 278,290 ----
*/
public synchronized void setText(String t) {
boolean skipTextEvent = (text == null || text.isEmpty())
&& (t == null || t.isEmpty());
text = (t != null) ? t : "";
+ if (replaceEOL && text != "") {
+ replaceEOL();
+ }
TextComponentPeer peer = (TextComponentPeer)this.peer;
// Please note that we do not want to post an event
// if TextArea.setText() or TextField.setText() replaces an empty text
// by an empty text, that is, if component's text remains unchanged.
if (peer != null && !skipTextEvent) {
< prev index next >