Java Utililty Methods JTextField

List of utility methods to do JTextField

  1. HOME
  2. Java
  3. J
  4. JTextField

Description

The list of methods to do JTextField are organized into topic(s).

Method

boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2)
Highlight the given label from the first mouse event to the second Returns true if the highlight was successful, false otherwise.
FontMetrics fm = label.getFontMetrics(label.getFont());
int firstXpos = mouseEvent1.getX();
int lastXpos = mouseEvent2.getX();
int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
if (lastOffset != firstOffset) {
 if (firstOffset > lastOffset) {
 int tmp = firstOffset;
...
void addInputValidator(final DocumentListener inputValidator, final JTextField... textFields)
Adds input validator to text fields.
for (final JTextField textField : textFields) {
 textField.getDocument().addDocumentListener(inputValidator);
void addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container)
add Label Text Rows
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 2, 2, 2);
int numLabels = labels.length;
for (int i = 0; i < numLabels; i++) {
 c.anchor = GridBagConstraints.EAST;
 c.gridwidth = GridBagConstraints.RELATIVE; 
 c.fill = GridBagConstraints.NONE; 
...
void addPlaceHolder(final JTextField field, final String placeHolderText)
add Place Holder
field.addFocusListener(new FocusListener() {
 private Color fontColor = field.getForeground();
 public void focusGained(FocusEvent arg0) {
 if (field.getText().equals(placeHolderText)) {
 field.setText("");
 field.setForeground(fontColor);
 public void focusLost(FocusEvent arg0) {
 if (field.getText().trim().equals("")) {
 fontColor = field.getForeground();
 field.setForeground(Color.GRAY);
 field.setText(placeHolderText);
});
if (field.getText().trim().equals("")) {
 field.setText(placeHolderText);
 field.setForeground(Color.GRAY);
void addStyle(JTextField textField, String labelName)
add Style
textField.setHorizontalAlignment(SwingConstants.RIGHT);
Border line = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
TitledBorder titled = BorderFactory.createTitledBorder(line, labelName);
titled.setTitleFont(new Font("Verdana", 0, 13));
titled.setTitleColor(new Color(213, 225, 185));
Border empty = new EmptyBorder(5, 8, 5, 8);
CompoundBorder border = new CompoundBorder(titled, empty);
textField.setBorder(border);
...
void adjustTextToRight(JTextField textField)
Metodo que quita los espacios que estan solo al final del texto en el jTextField entregado
String text = textField.getText();
String newText = "";
boolean segmentWithoutLetters = false;
for (int i = 0; i < text.length(); i++) {
 if (text.substring(i, text.length()).trim().isEmpty())
 segmentWithoutLetters = !segmentWithoutLetters;
 if (!segmentWithoutLetters)
 newText += text.charAt(i);
...
void applyDefaultProperties(final JTextField comp)
Sets default background and foreground color as well as a default font for the specified component.
if (comp == null) {
 return;
applyProperties(comp, "TextField.background", 
 "TextField.foreground", 
 "TextField.font"); 
boolean AreAllTextFieldsFilled(javax.swing.JTextField[] tfs)
Are All Text Fields Filled
for (int i = 0; i < tfs.length; i++) {
 if (IsTextFieldEmpty(tfs[i]))
 return false;
return true;
void badField(JTextField field)
Beeps to report the error, moves focus to specified field and selects all its content.
field.getToolkit().beep();
field.selectAll();
field.requestFocus();
void bindPropertyChangeListener(final JTextField field, final String name, final PropertyChangeListener listener)
bind Property Change Listener
bindPropertyChangeListener(field, name, listener, null);


AltStyle によって変換されたページ (->オリジナル) /