Advertisement
Guest User

JFrame/swing appears to use my method in its own code.

a guest
Mar 21st, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.83 KB | Source Code | 0 0
  1. import javax.swing.*;
  2. import javax.swing.text.DefaultCaret;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.FocusEvent;
  7. import java.awt.event.FocusListener;
  8.  
  9. public class newMain {
  10.     public static void main(String[] args) {
  11.         new Start();
  12.     }
  13. }
  14.  
  15. class Start extends JFrame {
  16.     public Start() {
  17.         setTitle("Original Title");
  18.         setSize(500, 300);
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.  
  21.         // Set initial background color of the content pane
  22.         getContentPane().setBackground(Color.WHITE);
  23.  
  24.         JPanel topPanel = new JPanel();
  25.         topPanel.setLayout(new FlowLayout());
  26.         topPanel.setOpaque(false);
  27.  
  28.         JPanel bottomPanel = new JPanel();
  29.         bottomPanel.setLayout(new FlowLayout());
  30.         bottomPanel.setOpaque(false);
  31.  
  32.         CTextField tfNaam = new CTextField(10, "Name here");
  33.         topPanel.add(tfNaam);
  34.  
  35.         JButton btnOk = new JButton("Okay!");
  36.         topPanel.add(btnOk);
  37.  
  38.         JButton btnAddTestUsr = new JButton("AddTestUser!");
  39.         topPanel.add(btnAddTestUsr);
  40.  
  41.         JLabel lText = new JLabel();
  42.         bottomPanel.add(lText);
  43.  
  44.         btnOk.addActionListener(new ActionListener() {
  45.             @Override
  46.             public void actionPerformed(ActionEvent e) {
  47.                 lText.setText("Hallo " + tfNaam.getText());
  48.             }
  49.         });
  50.  
  51.         btnAddTestUsr.addActionListener(new ActionListener() {
  52.             @Override
  53.             public void actionPerformed(ActionEvent e) {
  54.                 Color newColor = new Color((int) (Math.random() * (255)), (int) (Math.random() * (255)), (int) (Math.random() * (255)));
  55.                 tfNaam.setForeground(newColor);
  56.                 tfNaam.setText("Kevin");
  57.             }
  58.         });
  59.  
  60.         add(topPanel, BorderLayout.NORTH);
  61.         add(bottomPanel, BorderLayout.CENTER);
  62.  
  63.         setLocationRelativeTo(null);
  64.         setVisible(true);
  65.     }
  66. }
  67.  
  68.  
  69. /**
  70.  * CTextField is a custom implementation of JTextField with full placeholder support and compatibility with other methods
  71.  * @version 1.0
  72.  * @see JTextField
  73.  */
  74. class CTextField extends JTextField {
  75.     private String placeholder;
  76.     private Color textColor;
  77.     private boolean placeholderSet;
  78.  
  79.     /**
  80.      * Constructs a new TextField. A default model is created, the initial string is null, and the number of columns is set to 0.
  81.      */
  82.     public CTextField() {
  83.         super();
  84.     }
  85.  
  86.     /**
  87.      * Constructs a new TextField initialized with the specified text. A default model is created and the number of columns is 0.
  88.      * @param text the text to be displayed, or null
  89.      */
  90.     public CTextField(String text) {
  91.         super(text);
  92.     }
  93.  
  94.     /**
  95.      * Constructs a new empty TextField with the specified number of columns. A default model is created and the initial string is set to null.
  96.      * @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
  97.      */
  98.     public CTextField(int columns) {
  99.         super(columns);
  100.     }
  101.  
  102.     /**
  103.      * Constructs a new TextField initialized with the specified text and columns. A default model is created.
  104.      * @param text the text to be displayed, or null
  105.      * @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
  106.      */
  107.     public CTextField(String text, int columns) {
  108.         super(text, columns);
  109.     }
  110.  
  111.     /**
  112.      * 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.
  113.      * @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
  114.      * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
  115.      */
  116.     public CTextField(int columns, String placeholder) {
  117.         super(columns);
  118.         this.placeholder = placeholder;
  119.  
  120.         init();
  121.     }
  122.  
  123.     /**
  124.      * Constructs a new TextField initialized with the specified text and placeholder. A default model is created and the number of columns is 0.
  125.      * @param text the text to be displayed, or null
  126.      * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
  127.      */
  128.     public CTextField(String text, String placeholder) {
  129.         super(text);
  130.         this.placeholder = placeholder;
  131.  
  132.         init();
  133.     }
  134.  
  135.     /**
  136.      * Constructs a new TextField initialized with the specified text, columns and placeholder. A default model is created.
  137.      * @param text the text to be displayed, or null
  138.      * @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
  139.      * @param placeholder the text to be displayed when no text is entered into the TextField and when the TextField is not selected
  140.      */
  141.     public CTextField(String text, int columns, String placeholder) {
  142.         super(text, columns);
  143.         this.placeholder = placeholder;
  144.         init();
  145.     }
  146.  
  147.     private void init() {
  148.         textColor = getSuperForeground();
  149.         addPlaceholderFunc();
  150.     }
  151.  
  152.     /**
  153.      * Returns the text contained in this <code>TextComponent</code>.
  154.      * If the underlying document is <code>null</code>,
  155.      * will give a <code>NullPointerException</code>.
  156.      * Note that text is not a bound property, so no <code>PropertyChangeEvent
  157.      * </code> is fired when it changes. To listen for changes to the text,
  158.      * use <code>DocumentListener</code>.
  159.      *
  160.      * @return the text
  161.      * @see #setText
  162.      */
  163.     public String getText() {
  164.         if (!placeholderSet) return getSuperText();
  165.         return "";
  166.     }
  167.  
  168.     /**
  169.      * Sets the text of this <code>TextComponent</code>
  170.      * to the specified text.  If the text is <code>null</code>
  171.      * or empty, has the effect of simply deleting the old text.
  172.      * When text has been inserted, the resulting caret location
  173.      * is determined by the implementation of the caret class.
  174.      * <p>
  175.      * Note that text is not a bound property, so no <code>PropertyChangeEvent
  176.      * </code> is fired when it changes. To listen for changes to the text,
  177.      * use <code>DocumentListener</code>.
  178.      *
  179.      * @param text the new text to be set
  180.      * @see #getText
  181.      * @see DefaultCaret
  182.      */
  183.     public void setText(String text) {
  184.         setSuperText(text);
  185.         setSuperForeground(textColor);
  186.         placeholderSet = false;
  187.     }
  188.  
  189.     /**
  190.      * Gets the foreground color of this component.
  191.      * @return this component's foreground color; if this component does
  192.      * not have a foreground color, the foreground color of its parent
  193.      * is returned
  194.      * @see #setForeground
  195.      */
  196.     public Color getForeground() {
  197.         return this.textColor;
  198.     }
  199.  
  200.     /**
  201.      * Sets the foreground color of this component.  It is up to the
  202.      * look and feel to honor this property, some may choose to ignore
  203.      * it.
  204.      *
  205.      * @param color  the desired foreground <code>Color</code>
  206.      * @see #getForeground
  207.      */
  208.     public void setForeground(Color color) {
  209.         setSuperForeground(color);
  210.         this.textColor = color;
  211.     }
  212.  
  213.     private void addPlaceholderFunc() {
  214.         this.addFocusListener(new FocusListener() {
  215.             @Override
  216.             public void focusGained(FocusEvent e) {
  217.                 if (getSuperText().equals(placeholder) && placeholderSet) {
  218.                     setSuperText("");
  219.                     setSuperForeground(textColor);
  220.                     placeholderSet = false;
  221.                 }
  222.             }
  223.             @Override
  224.             public void focusLost(FocusEvent e) {
  225.                 if (getSuperText().isEmpty()) {
  226.                     setSuperText(placeholder);
  227.                     setSuperForeground(Color.GRAY);
  228.                     placeholderSet = true;
  229.                 }
  230.             }
  231.         });
  232.     }
  233.  
  234.     //    super methods are used to apply changes to the actual element without triggering the other methods (eg. setText())
  235.     private String getSuperText() {
  236.         return super.getText();
  237.     }
  238.     private void setSuperText(String text) {
  239.         super.setText(text);
  240.     }
  241.     private Color getSuperForeground() {
  242.         return super.getForeground();
  243.     }
  244.     private void setSuperForeground(Color color) {
  245.         super.setForeground(color);
  246.     }
  247. }
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement