Custom JTextArea with Background Image


/ Published in: Java
Save to your folder(s)

This is a sample JTextArea with a custom background image.


Copy this code and paste it in your HTML
  1. import java.awt.Dimension;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5.  
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextArea;
  10.  
  11.  
  12. public class CustomTextArea extends JPanel {
  13.  
  14. private static final long serialVersionUID = 7502204181430286959L;
  15. private static final Image image = new ImageIcon( "images/windows.jpg" ).getImage();
  16.  
  17. public static void main(String[] args) {
  18. JFrame frame = new JFrame( "Hello" );
  19. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  20. JPanel panel = new JPanel();
  21. panel.add( new CustomTextArea() );
  22. frame.setContentPane( panel );
  23. frame.pack();
  24. frame.setVisible( true );
  25. }
  26.  
  27. public CustomTextArea() {
  28. super();
  29. init();
  30. }
  31.  
  32. public Dimension getPreferredSize() {
  33. return new Dimension( image.getWidth( null ), image.getHeight( null ) );
  34. }
  35. public void init() {
  36. JTextArea ta = new JTextArea();
  37. Font f = new Font("Verdana", Font.BOLD, 36 );
  38. ta.setFont( f );
  39. ta.setLineWrap( true );
  40. ta.setWrapStyleWord( true );
  41. ta.setPreferredSize(getPreferredSize());
  42. ta.setOpaque( false );
  43. add( ta );
  44. }
  45.  
  46. public void paintComponent( Graphics g ) {
  47. super.paintComponent( g );
  48. g.drawImage(image, 0, 0, null);
  49. }
  50. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.