1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

JTextField Not Displaying Properly When Backgrnd Image is set on jframe

Discussion in 'Java' started by sahildave1991, Dec 2, 2011.

  1. sahildave1991

    sahildave1991 New Member

    Joined:
    Dec 2, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I was making a tool in which i had to put an image as the background image and over it put some JTextField.

    i have made this code:
    Code:
    package firstTry;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Second extends JFrame
    {
    	public JPanel mainpanel = new JPanel();
    	JLabel mainhead = new JLabel("Population Forecast Tool");
    	
    	ImageIcon back = new ImageIcon("year2.jpg");
    	public JLabel backLabel = new JLabel(back);
    	
    	
    	public Second()
    	{
    			
    		mainpanel.setLayout(null);
    		
    		mainhead.setFont(new Font("Arial", 1, 20));
    		mainhead.setBounds(240,10,300,30);
    		backLabel.setBounds(40,45, 627, 364);
    
    		mainpanel.add(mainhead);
    		mainpanel.add(backLabel);
    		
    		add(mainpanel);
    		
    		SecondComp obj = new SecondComp(mainpanel);
    	}
    	
    	
    	public static void main(String[] args) 
    	{
    		Second obj = new Second();
    		
    		obj.setSize(800,550);
    		obj.setVisible(true);
    		obj.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		obj.setResizable(false);
    		obj.setTitle("Population Predictor");
    	}
    
    }
    
    the JTextField are added in the SecondComp class which is:

    Code:
    package firstTry;
    
    import java.awt.Color;
    
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class SecondComp 
    {
    	JTextField firstTf = new JTextField(102);
    	JTextField secondTf = new JTextField(102);
    	JTextField thirdTf = new JTextField(102);
    	JTextField fourthTf = new JTextField(102);
    	JTextField fifthTf = new JTextField(102);
    	
    	
    	public SecondComp(JPanel mainpanel) 
    	{
    		
    		firstTf.setBounds(148,107,102,33);
    		secondTf.setBounds(148,142,102,33);
    		thirdTf.setBounds(148,177,102,33);
    		fourthTf.setBounds(148,212,102,33);
    		fifthTf.setBounds(148,247,102,33);
    		
    		firstTf.setHorizontalAlignment(JTextField.CENTER);
    		firstTf.setBorder(BorderFactory.createLineBorder(Color.black));
    		secondTf.setHorizontalAlignment(JTextField.CENTER);
    		secondTf.setBorder(BorderFactory.createLineBorder(Color.black));
    		thirdTf.setHorizontalAlignment(JTextField.CENTER);
    		thirdTf.setBorder(BorderFactory.createLineBorder(Color.black));
    		fourthTf.setHorizontalAlignment(JTextField.CENTER);
    		fourthTf.setBorder(BorderFactory.createLineBorder(Color.black));
    		fifthTf.setHorizontalAlignment(JTextField.CENTER);
    		fifthTf.setBorder(BorderFactory.createLineBorder(Color.black));
    		
    		thirdTf.setEnabled(true);
    		
    		
    		mainpanel.add(firstTf);
    		mainpanel.add(secondTf);
    		mainpanel.add(thirdTf);
    		mainpanel.add(fourthTf);
    		mainpanel.add(fifthTf);
    	}
    }
    
    
    The problem is that when the image is in the background the JTextField donot appear properly. I have to press TAB key in order to enable them. You can see i had tried "thirdTf.setEnabled(true);" but it also didnt work.

    PLEASE SEE THE IMAGES TO UNDERSTAND
     

    Attached Files:

  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Code:
    .....
            backLabel.setBounds(40,45, 627, 364);
            backLabel.setOpaque(false);//try inserting this line of code here
            mainpanel.add(mainhead);
    ......
    
     
  3. sahildave1991

    sahildave1991 New Member

    Joined:
    Dec 2, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for reply, but it didnt work...
     
  4. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    maybe this?

    Code:
    ......
            backLabel.setBounds(40,45, 627, 364);
            mainpanel.add(backLabel);
            mainpanel.add(mainhead);
            add(mainpanel);
            SecondComp obj = new SecondComp(mainpanel);
    .......
    
     

Share This Page