Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.text.DefaultCaret;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- public class newMain {
- public static void main(String[] args) {
- new Start();
- }
- }
- class Start extends JFrame {
- public Start() {
- setTitle("Original Title");
- setSize(500, 300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Set initial background color of the content pane
- getContentPane().setBackground(Color.WHITE);
- JPanel topPanel = new JPanel();
- topPanel.setLayout(new FlowLayout());
- topPanel.setOpaque(false);
- JPanel bottomPanel = new JPanel();
- bottomPanel.setLayout(new FlowLayout());
- bottomPanel.setOpaque(false);
- CTextField tfNaam = new CTextField(10, "Name here");
- topPanel.add(tfNaam);
- JButton btnOk = new JButton("Okay!");
- topPanel.add(btnOk);
- JButton btnAddTestUsr = new JButton("AddTestUser!");
- topPanel.add(btnAddTestUsr);
- JLabel lText = new JLabel();
- bottomPanel.add(lText);
- btnOk.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- lText.setText("Hallo " + tfNaam.getText());
- }
- });
- btnAddTestUsr.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Color newColor = new Color((int) (Math.random() * (255)), (int) (Math.random() * (255)), (int) (Math.random() * (255)));
- tfNaam.setForeground(newColor);
- tfNaam.setText("Kevin");
- }
- });
- add(topPanel, BorderLayout.NORTH);
- add(bottomPanel, BorderLayout.CENTER);
- setLocationRelativeTo(null);
- setVisible(true);
- }
- }
- /**
- * CTextField is a custom implementation of JTextField with full placeholder support and compatibility with other methods
- * @version 1.0
- * @see JTextField
- */
- class CTextField extends JTextField {
- private String placeholder;
- private Color textColor;
- private boolean placeholderSet;
- /**
- * Constructs a new TextField. A default model is created, the initial string is null, and the number of columns is set to 0.
- */
- public CTextField() {
- super();
- }
- /**
- * Constructs a new TextField initialized with the specified text. A default model is created and the number of columns is 0.
- * @param text the text to be displayed, or null
- */
- public CTextField(String text) {
- super(text);
- }
- /**
- * Constructs a new empty TextField with the specified number of columns. A default model is created and the initial string is set to null.
- * @param columns the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
- */
- public CTextField(int columns) {
- super(columns);
- }
- /**
- * Constructs a new TextField initialized with the specified text and columns. A default model is created.
- * @param text the text to be displayed, or null
- * @param columns the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
- */
- public CTextField(String text, int columns) {
- super(text, columns);
- }
- /**
- * Constructs a new empty TextField with the specified number of columns and placeholder. A default model is created and the initial string is set to null.
- * @param columns the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
- * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
- */
- public CTextField(int columns, String placeholder) {
- super(columns);
- this.placeholder = placeholder;
- init();
- }
- /**
- * Constructs a new TextField initialized with the specified text and placeholder. A default model is created and the number of columns is 0.
- * @param text the text to be displayed, or null
- * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
- */
- public CTextField(String text, String placeholder) {
- super(text);
- this.placeholder = placeholder;
- init();
- }
- /**
- * Constructs a new TextField initialized with the specified text, columns and placeholder. A default model is created.
- * @param text the text to be displayed, or null
- * @param columns the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
- * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
- */
- public CTextField(String text, int columns, String placeholder) {
- super(text, columns);
- this.placeholder = placeholder;
- init();
- }
- private void init() {
- textColor = getSuperForeground();
- addPlaceholderFunc();
- }
- /**
- * Returns the text contained in this <code>TextComponent</code>.
- * If the underlying document is <code>null</code>,
- * will give a <code>NullPointerException</code>.
- * Note that text is not a bound property, so no <code>PropertyChangeEvent
- * </code> is fired when it changes. To listen for changes to the text,
- * use <code>DocumentListener</code>.
- *
- * @return the text
- * @see #setText
- */
- public String getText() {
- if (!placeholderSet) return getSuperText();
- return "";
- }
- /**
- * Sets the text of this <code>TextComponent</code>
- * to the specified text. If the text is <code>null</code>
- * or empty, has the effect of simply deleting the old text.
- * When text has been inserted, the resulting caret location
- * is determined by the implementation of the caret class.
- * <p>
- * Note that text is not a bound property, so no <code>PropertyChangeEvent
- * </code> is fired when it changes. To listen for changes to the text,
- * use <code>DocumentListener</code>.
- *
- * @param text the new text to be set
- * @see #getText
- * @see DefaultCaret
- */
- public void setText(String text) {
- setSuperText(text);
- setSuperForeground(textColor);
- placeholderSet = false;
- }
- /**
- * Gets the foreground color of this component.
- * @return this component's foreground color; if this component does
- * not have a foreground color, the foreground color of its parent
- * is returned
- * @see #setForeground
- */
- public Color getForeground() {
- return this.textColor;
- }
- /**
- * Sets the foreground color of this component. It is up to the
- * look and feel to honor this property, some may choose to ignore
- * it.
- *
- * @param color the desired foreground <code>Color</code>
- * @see #getForeground
- */
- public void setForeground(Color color) {
- setSuperForeground(color);
- this.textColor = color;
- }
- private void addPlaceholderFunc() {
- this.addFocusListener(new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- if (getSuperText().equals(placeholder) && placeholderSet) {
- setSuperText("");
- setSuperForeground(textColor);
- placeholderSet = false;
- }
- }
- @Override
- public void focusLost(FocusEvent e) {
- if (getSuperText().isEmpty()) {
- setSuperText(placeholder);
- setSuperForeground(Color.GRAY);
- placeholderSet = true;
- }
- }
- });
- }
- // super methods are used to apply changes to the actual element without triggering the other methods (eg. setText())
- private String getSuperText() {
- return super.getText();
- }
- private void setSuperText(String text) {
- super.setText(text);
- }
- private Color getSuperForeground() {
- return super.getForeground();
- }
- private void setSuperForeground(Color color) {
- super.setForeground(color);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement