All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.text.LabeledTextField Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.text;

import javax.swing.JLabel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.alantea.liteprops.StringProperty;
import net.alantea.swing.layout.percent.PercentConstraints;
import net.alantea.swing.layout.percent.PercentLayout;
import net.alantea.swing.layout.percent.Where;

/**
 * The Class LabeledTextField.
 */
public class LabeledTextField extends LabeledText
{
   
   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = 1L;
   
   /** The label constraints. */
   private PercentConstraints labelConstraints;
   
   /** The field. */
   private TextField field;
   
   /** The label. */
   private JLabel label;
   
   private StringProperty data = new StringProperty();
   
    /**
     * Instantiates a new labeled text field.
     */
    public LabeledTextField()
    {
       super();
       setLayout(new PercentLayout());
       label = new JLabel();
       labelConstraints = new PercentConstraints(0, 0, 0.25, 1.0);
       add(label, labelConstraints);
       
       field = new TextField();
       PercentConstraints fieldConstraints = new PercentConstraints(0, 0, 0, 1.0);
       fieldConstraints.setXReferent(label, Where.AFTER);
       fieldConstraints.setWReferent(label, Where.AFTER);
       add(field, fieldConstraints);

       field.getDocument().addDocumentListener(new DocumentListener() {
          @Override
          public void changedUpdate(DocumentEvent e) {}
       
          @Override
          public void insertUpdate(DocumentEvent e) {
             data.set(field.getText());
          }
       
          @Override
          public void removeUpdate(DocumentEvent e)  {
             data.set(field.getText());
          }
      });
    }
    
    public StringProperty getStringProperty()
    {
       return data;
    }
    
    /**
     * Sets the label percent.
     *
     * @param value the new label percent
     */
    public void setLabelPercent(double value)
    {
       labelConstraints.setXPercent(value);
    }
    
    /**
     * Gets the text.
     *
     * @return the text
     */
    public String getText()
    {
       return data.get();
    }
    
    /**
     * Sets the text.
     *
     * @param value the new text
     */
    public void setText(String value)
    {
       field.setText(value);
    }
    
    /**
     * Sets the label text.
     *
     * @param value the new text
     */
    public void setLabelText(String value)
    {
       label.setText(value);
    }
    
    /**
     * Gets the label.
     *
     * @return the label
     */
    public JLabel getLabel()
    {
       return label;
    }
    
    /**
     * Gets the text field.
     *
     * @return the text field
     */
    public TextField getTextField()
    {
       return field;
    }

    @Override
    public void setEditable(boolean b)
    {
       field.setEditable(b);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy