Using the new JLayer decorator for a password field - Java Swing

Java examples for Swing:JLayer

Description

Using the new JLayer decorator for a password field

Demo Code

import java.awt.AWTEvent;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.LayerUI;
import java.awt.BorderLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.plaf.LayerUI;
class PasswordLayerUI extends LayerUI<JPanel> {

    private String errorMessage = "Password too short";

    @Override/*from   w w  w. java 2 s.co m*/
    public void paint(Graphics g, JComponent c) {
        FontMetrics fontMetrics;
        Font font;
        int height;
        int width;
        
        super.paint(g, c);
        Graphics2D g2d = (Graphics2D) g.create();
        int componentWidth = c.getWidth();
        int componentHeight = c.getHeight();

        // Display error message
        g2d.setFont(c.getFont());
        fontMetrics = g2d.getFontMetrics(c.getFont());
        height = fontMetrics.getHeight();
        g2d.drawString(errorMessage, 
                componentWidth / 2 + 10, componentHeight / 2 + height);
        
        // Display watermark
        String displayText = "Beta Version";
        font = new Font("Castellar",Font.PLAIN, 16);
        fontMetrics = g2d.getFontMetrics(font);
        g2d.setFont(font);
        width = fontMetrics.stringWidth(displayText);
        height = fontMetrics.getHeight();
        
        Composite com = g2d.getComposite();
        AlphaComposite ac = AlphaComposite.getInstance(
                ((AlphaComposite)com).getRule(),0.25f);
        g2d.setComposite(ac);
        g2d.drawString(displayText, 
                (componentWidth - width) / 2, 
                (componentHeight - height) / 2);
        
        g2d.dispose();
    }

    @Override
    public void installUI(JComponent component) {
        super.installUI(component);
        ((JLayer) component).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
    }

    @Override
    public void uninstallUI(JComponent component) {
        super.uninstallUI(component);
        ((JLayer) component).setLayerEventMask(0);
    }

    protected void processKeyEvent(KeyEvent event, JLayer layer) {
        JTextField f = (JTextField) event.getSource();
        if (f.getText().length() < 6) {
            errorMessage = "Password too short";
        } else {
            errorMessage = "";
        }
        layer.repaint();
    }
}
public class Main extends JFrame {
    public Main() {
        this.setTitle("Example");
        this.setSize(300, 100);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        LayerUI<JPanel> layerUI = new PasswordLayerUI();
        JLayer<JPanel> jlayer = new JLayer<JPanel>(getPanel(), layerUI);
        this.add(jlayer);
    }
    private JPanel getPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        
        JPanel gridPanel = new JPanel(new GridLayout(1, 2));
        JLabel quantityLabel = new JLabel("Password");
        gridPanel.add(quantityLabel);
        JPasswordField passwordField = new JPasswordField();
        gridPanel.add(passwordField);
        panel.add(gridPanel, BorderLayout.CENTER);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton okButton = new JButton("OK");
        buttonPanel.add(okButton);
        JButton cancelButton = new JButton("Cancel");
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        return panel;
    }

    public static void main(String[] args) {            
        SwingUtilities.invokeLater(() ->{
                Main window = new Main();
                window.setVisible(true);
        });
    }
}

Related Tutorials