New Post

[Java] [Swing] [Netbeans] [ Custom Palette ] jTextfield 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 java.sql.DriverManager;  
  16. import java.sql.SQLException;  
  17. import javax.swing.JOptionPane;  
  18. import javax.swing.JTextField;  
  19. import javax.swing.border.EmptyBorder;  
  20.   
  21. public class Ae_textField extends JTextField {  
  22.   
  23.     public java.sql.Connection conn;  
  24.     public java.sql.Statement stmt;  
  25.     private Boolean left = true;  
  26.     protected float Ae_sizeBorder = 1.0F;  
  27.     protected Color Ae_colorBorder = Color.decode("#ADADAD");  
  28.     private String Ae_placeholder = "Type value here";  
  29.     private Color Ae_txtColorPlaceholder = Color.decode("#808080");  
  30.     private final Integer Ae_borderRadius = 8;  
  31.     private final Color txtcolor = Color.decode("#000000");  
  32.     private final Color bgcolor = Color.decode("#FFFFFF");  
  33.   
  34.     public Ae_textField() {  
  35.         setOpaque(false);  
  36.         setBorder(new EmptyBorder(01004));  
  37.         setPreferredSize(new Dimension(18830));  
  38.         setFont(new Font("Segoe UI"012));  
  39.         setForeground(txtcolor);  
  40.         setBackground(bgcolor);  
  41.     }  
  42.   
  43.     public final void koneksi() {  
  44.         try {  
  45.             Class.forName("org.sqlite.JDBC");  
  46.             conn = DriverManager.getConnection("jdbc:sqlite:dbconfig/ae_config.sqlite");  
  47.             stmt = conn.createStatement();  
  48.         } catch (ClassNotFoundException | SQLException e) {  
  49.             JOptionPane.showMessageDialog(null"Connection Lose..");  
  50.         }  
  51.     }  
  52.   
  53.     @Override  
  54.     protected void paintComponent(Graphics g) {  
  55.         Graphics2D g2 = (Graphics2D) g;  
  56.         Paint oldPaint = g2.getPaint();  
  57.   
  58.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  59.   
  60.         RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0.0F, 0.0F, getWidth(), getHeight(), this.Ae_borderRadius, this.Ae_borderRadius);  
  61.   
  62.         g2.clip(r2d);  
  63.   
  64.         g2.setColor(getBackground());  
  65.         g2.fillRoundRect(00, getWidth(), getHeight(), this.Ae_borderRadius, this.Ae_borderRadius);  
  66.         if (getText().length() < 1) {  
  67.             Insets insets = getInsets();  
  68.             FontMetrics fm = getFontMetrics(getFont());  
  69.             g2.setColor(getForeground());  
  70.             if (getText() == null) {  
  71.                 setText("  ");  
  72.             }  
  73.             TextLayout layout = new TextLayout(this.Ae_placeholder == null ? " " : this.Ae_placeholder, getFont(), g2.getFontRenderContext());  
  74.   
  75.             Rectangle2D bounds = layout.getBounds();  
  76.   
  77.             int x = (int) (getWidth() - insets.left - insets.right - bounds.getWidth()) / 2;  
  78.             if (getHorizontalAlignment() != 0) {  
  79.                 x = 0 + insets.left;  
  80.             }  
  81.             int y = (getHeight() - insets.top - insets.bottom - fm.getMaxAscent() - fm.getMaxDescent()) / 2;  
  82.   
  83.             y += fm.getAscent() - 1;  
  84.             g2.setColor(this.Ae_txtColorPlaceholder);  
  85.             layout.draw(g2, x, y);  
  86.         }  
  87.         g2.setPaint(oldPaint);  
  88.         super.paintComponent(g);  
  89.     }  
  90.   
  91.     @Override  
  92.     protected void paintBorder(Graphics g) {  
  93.         int x = 1;  
  94.         int y = 1;  
  95.         int w = getWidth() - 2;  
  96.         int h = getHeight() - 2;  
  97.         Graphics2D g2 = (Graphics2D) g.create();  
  98.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  99.   
  100.         g2.setStroke(new BasicStroke(this.Ae_sizeBorder));  
  101.         g2.setColor(this.Ae_colorBorder);  
  102.         g2.drawRoundRect(x, y, w, h, this.Ae_borderRadius, this.Ae_borderRadius);  
  103.         g2.dispose();  
  104.     }  
  105.   
  106.     public Color getAe_TxtColorPlaceholder() {  
  107.         return this.Ae_txtColorPlaceholder;  
  108.     }  
  109.   
  110.     public void setAe_TxtColorPlaceholder(Color Ae_txtColorPlaceholder) {  
  111.         this.Ae_txtColorPlaceholder = Ae_txtColorPlaceholder;  
  112.     }  
  113.   
  114.     public Boolean getLeft() {  
  115.         return this.left;  
  116.     }  
  117.   
  118.     public void setLeft(Boolean left) {  
  119.         this.left = left;  
  120.         repaint();  
  121.     }  
  122.   
  123.     public float getAe_SizeBorder() {  
  124.         return this.Ae_sizeBorder;  
  125.     }  
  126.   
  127.     public void setAe_SizeBorder(float Ae_sizeBorder) {  
  128.         this.Ae_sizeBorder = Ae_sizeBorder;  
  129.     }  
  130.   
  131.     public Color getAe_ColorBorder() {  
  132.         return this.Ae_colorBorder;  
  133.     }  
  134.   
  135.     public void setAe_ColorBorder(Color Ae_colorBorder) {  
  136.         this.Ae_colorBorder = Ae_colorBorder;  
  137.     }  
  138.   
  139.     public String getAe_Placeholder() {  
  140.         return this.Ae_placeholder;  
  141.     }  
  142.   
  143.     public void setAe_Placeholder(String Ae_placeholder) {  
  144.         this.Ae_placeholder = Ae_placeholder;  
  145.     }  
  146. }  

Untuk menampilkan Placeholder pada code tema default rubah dari Nimbus menjadi Windows

 Bagian Properties yang dapat di rubah



No comments