New Post

[Java] [Swing] [Netbeans] [ Custom Palette ] jPasswordfield Round With Place Holder


  1.    
  2. import java.awt.BasicStroke;  
  3. import java.awt.Color;  
  4. import java.awt.Dimension;  
  5. import java.awt.Font;  
  6. import java.awt.FontMetrics;  
  7. import java.awt.Graphics;  
  8. import java.awt.Graphics2D;  
  9. import java.awt.Insets;  
  10. import java.awt.Paint;  
  11. import java.awt.RenderingHints;  
  12. import java.awt.font.TextLayout;  
  13. import java.awt.geom.Rectangle2D;  
  14. import java.awt.geom.RoundRectangle2D;  
  15. import javax.swing.JPasswordField;  
  16. import javax.swing.border.EmptyBorder;  
  17.   
  18. public class Ae_passField extends JPasswordField {  
  19.   
  20.     private Boolean left = true;  
  21.     protected float Ae_sizeBorder = 1.0F;  
  22.     protected Color Ae_colorBorder = Color.decode("#ADADAD");  
  23.     private String Ae_placeholder = "Type password here";  
  24.     private Color Ae_txtColorPlaceholder = Color.decode("#808080");  
  25.     private final Integer Ae_borderRadius = 8;  
  26.     private final Color txtcolor = Color.decode("#000000");  
  27.     private final Color bgcolor = Color.decode("#FFFFFF");  
  28.   
  29.     public Ae_passField() {  
  30.         setOpaque(false);  
  31.         setBorder(new EmptyBorder(01004));  
  32.         setPreferredSize(new Dimension(18830));  
  33.         setFont(new Font("Segoe UI"012));  
  34.         setForeground(txtcolor);  
  35.         setBackground(bgcolor);  
  36.     }  
  37.   
  38.     @Override  
  39.     protected void paintComponent(Graphics g) {  
  40.         Graphics2D g2 = (Graphics2D) g;  
  41.         Paint oldPaint = g2.getPaint();  
  42.   
  43.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  44.   
  45.         RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0.0F, 0.0F, getWidth(), getHeight(), this.Ae_borderRadius, this.Ae_borderRadius);  
  46.   
  47.         g2.clip(r2d);  
  48.   
  49.         g2.setColor(getBackground());  
  50.         g2.fillRoundRect(00, getWidth(), getHeight(), this.Ae_borderRadius, this.Ae_borderRadius);  
  51.         if (getText().length() < 1) {  
  52.             Insets insets = getInsets();  
  53.             FontMetrics fm = getFontMetrics(getFont());  
  54.             g2.setColor(getForeground());  
  55.             if (getText() == null) {  
  56.                 setText("  ");  
  57.             }  
  58.             TextLayout layout = new TextLayout(this.Ae_placeholder == null ? " " : this.Ae_placeholder, getFont(), g2.getFontRenderContext());  
  59.   
  60.             Rectangle2D bounds = layout.getBounds();  
  61.   
  62.             int x = (int) (getWidth() - insets.left - insets.right - bounds.getWidth()) / 2;  
  63.             if (getHorizontalAlignment() != 0) {  
  64.                 x = 0 + insets.left;  
  65.             }  
  66.             int y = (getHeight() - insets.top - insets.bottom - fm.getMaxAscent() - fm.getMaxDescent()) / 2;  
  67.   
  68.             y += fm.getAscent() - 1;  
  69.             g2.setColor(this.Ae_txtColorPlaceholder);  
  70.             layout.draw(g2, x, y);  
  71.         }  
  72.         g2.setPaint(oldPaint);  
  73.         super.paintComponent(g);  
  74.     }  
  75.   
  76.     @Override  
  77.     protected void paintBorder(Graphics g) {  
  78.         int x = 1;  
  79.         int y = 1;  
  80.         int w = getWidth() - 2;  
  81.         int h = getHeight() - 2;  
  82.         Graphics2D g2 = (Graphics2D) g.create();  
  83.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  84.   
  85.         g2.setStroke(new BasicStroke(this.Ae_sizeBorder));  
  86.         g2.setColor(this.Ae_colorBorder);  
  87.         g2.drawRoundRect(x, y, w, h, this.Ae_borderRadius, this.Ae_borderRadius);  
  88.         g2.dispose();  
  89.     }  
  90.   
  91.     public Color getAe_TxtColorPlaceholder() {  
  92.         return this.Ae_txtColorPlaceholder;  
  93.     }  
  94.   
  95.     public void setAe_TxtColorPlaceholder(Color Ae_txtColorPlaceholder) {  
  96.         this.Ae_txtColorPlaceholder = Ae_txtColorPlaceholder;  
  97.     }  
  98.   
  99.     public Boolean getLeft() {  
  100.         return this.left;  
  101.     }  
  102.   
  103.     public void setLeft(Boolean left) {  
  104.         this.left = left;  
  105.         repaint();  
  106.     }  
  107.   
  108.     public float getAe_SizeBorder() {  
  109.         return this.Ae_sizeBorder;  
  110.     }  
  111.   
  112.     public void setAe_SizeBorder(float Ae_sizeBorder) {  
  113.         this.Ae_sizeBorder = Ae_sizeBorder;  
  114.     }  
  115.   
  116.     public Color getAe_ColorBorder() {  
  117.         return this.Ae_colorBorder;  
  118.     }  
  119.   
  120.     public void setAe_ColorBorder(Color Ae_colorBorder) {  
  121.         this.Ae_colorBorder = Ae_colorBorder;  
  122.     }  
  123.   
  124.     public String getAe_Placeholder() {  
  125.         return this.Ae_placeholder;  
  126.     }  
  127.   
  128.     public void setAe_Placeholder(String Ae_placeholder) {  
  129.         this.Ae_placeholder = Ae_placeholder;  
  130.     }  
  131. }  

Lihat tutorial sebelumnya untuk jtextfield

No comments