Results 1 to 4 of 4

Thread: Custom Background Icon for JTextField?

  1. #1
    Join Date
    Aug 2010
    Posts
    5

    Custom Background Icon for JTextField?

    Is it possible to create a custom background for a JTextField? Perhaps using an Icon or ImageIcon?

    I have a number of JTextField entries in a GridLayout and need to display some information in each field to indicate what may be entered into that field. That information changes over time so I have to update it and redisplay it. I can't make do with tooltips or similar as I want the information to be constantly displayed - at least until the user enters text into the JTextField.

    I hoped to be able to create (and update) a background image with the information and use it but the only thing I found for JTextField was a setBackground method taking a Color argument.

    Mark

  2. #2
    Join Date
    Feb 2010
    Posts
    182

    Re: Custom Background Icon for JTextField?

    You could charge you draw your icon yourself inside the method paintComponent.

    Code:
    import java.awt.Graphics ;
    import java.awt.Image ;
    import javax.swing.ImageIcon ;
    import javax.swing.JButton ;
     
    public class MyButton extends JButton { / / ! we must extend the component in which you want to insert a background image
       
       private Image img ;
       private String imageName ;
       
       / / A builder for more simply choose the image
       public MyButton(String imageName) {
           img = New ImageIcon(getClass().GetResource(imageName)).getImage();
       }
       
       / / We must redefine the method paintComponent () for components Swing ! and paint ( ) to awt
       @ Override
       protected void paintComponent(Graphics g) {
           / / super.paintComponent ( g );
           if (img == null) return;
           g.drawImage(img 0, 0, getWidth(), getHeight(), this);
           getIcon().paintIcon(this, g (getWidth()getIcon -().getIconWidth())/2, (getHeight()getIcon -().getIconHeight())/2);
          }
     }
    here is the line added
    Code:
    getIcon().paintIcon(this, g (getWidth()getIcon -().getIconWidth())/2, (getHeight()getIcon -().getIconHeight())/2);
    getIcon method (.. ) takes as parameter the component where we want to draw the graphics with which we draw , and the location of the icon ( x, y) , here I made sure that it is center.

  3. #3
    Join Date
    Aug 2010
    Posts
    5

    Re: Custom Background Icon for JTextField?

    Osman,

    I notice you commented out the super.repaint() method call. In my case I don't think I can do this as I have a JTextField that I want to paint as normal when the user enters text into it. I just want a background icon I can use (or not use as appropriate). I will play with your approach. Thanks.

  4. #4
    Join Date
    Aug 2010
    Posts
    5

    Re: Custom Background Icon for JTextField?

    Just wanted to post a final note. That approach turned out to be even simpler than I thought. I subclassed JTextField and had an icon as an attribute. The paintComponent method was pretty simple:

    Code:
    	protected void paintComponent(Graphics g) 
    	{
    		if (this.getText().isEmpty())
    			setOpaque(false);
    		else
    			setOpaque(true);
    		m_pi.paintIcon(this, g, 0, 0);
    
    		super.paintComponent(g);
    	}
    Thanks for the help.

Similar Threads

  1. Mac OS X Lion 10.7.4 Update blocked Custom Login Background
    By Oppilaa in forum Operating Systems
    Replies: 3
    Last Post: 15-05-2012, 07:42 PM
  2. Windows 7 custom logon screen background is not working
    By Mr.Ghost in forum Customize Desktop
    Replies: 4
    Last Post: 10-02-2012, 05:06 PM
  3. Replies: 4
    Last Post: 05-09-2011, 06:51 PM
  4. How to design custom twitter background
    By Anek in forum Technology & Internet
    Replies: 3
    Last Post: 05-05-2009, 02:27 PM
  5. Custom background picture of a folder!
    By pancham in forum Customize Desktop
    Replies: 1
    Last Post: 17-01-2009, 12:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,716,457.24581 seconds with 17 queries