Java Reference
In-Depth Information
public void paint (Graphics g) {
g.drawImage(grayImage, 0, 0, this);
super.paint(g);
}
};
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
JTextComponent Operations
The
JTextComponent
defines the basic framework for many of the operations performed on the
text controls.
I/O:
The
public void read(Reader in, Object description)
and
public void
write(Writer out)
methods (both throw
IOException
) allow you to read or write
the text component contents with ease.
•
Clipboard access:
The
public void cut()
,
public void copy()
, and
public void paste()
methods provide direct access to the system clipboard.
•
Positioning:
The
public void moveCaretPosition(int position)
method allows you to
position the caret. The position represents a one-dimensional location indicating the
number of characters to precede the caret from the beginning of the text component.
•
•
Selection:
The
public void replaceSelection(String content)
,
public void
selectAll()
, and
public void select(int selectionStart, int selectionEnd)
methods allow you to select part of the content within the component and replace
the content that is selected.
Conversion:
The
public Rectangle modelToView(int position) throws
BadLocationException
and
public int viewToModel(Point point)
methods allow
you (or, more likely, allow the system) to map a position within the
JTextComponent
to
a physical mapping within the representation of the contents for the specific text UI
delegate.
•
Now that you've had an overview of the
JTextComponent
class, it's time to look at its
different subclasses. First up is the
JTextField
, which will be used to demonstrate the operations
just listed.
JTextField Class
The
JTextField
component is the text component for a single line of input. The data model
for a
JTextField
is the
PlainDocument
implementation of the
Document
interface. The
PlainDocument
model limits input to single-attributed text, meaning that it must be a single font and color.