Results 1 to 6 of 6
  1. #1
    floris is offline Member
    Join Date
    Apr 2012
    Location
    Den Haag, the Netherlands
    Posts
    5
    Rep Power
    0

    Default JTextField on JPanel-class within JFrame-class

    I'm having some difficulties showing my JTextFields in a JPanel class. This JPanel class is executed in a JFrame class.
    I really can't see why they aren't there..
    PS: The JFrame class does show the image from the Jpanel class.




    Maybe some code says more than a thousand words ;)

    This is the JPanel class with the JTextFields..
    Java Code:
    (IMPORTS etc..)
    
    public class NaamInvoer extends JPanel implements ActionListener
    {
    
    	private ImageIcon 	naamInvoerImg;
    	private JTextField 	naamSpeler1;
    	private JTextField 	naamSpeler2;
    	private JTextField 	naamSpeler3;
    	private JTextField 	naamSpeler4;
    	private JButton	volgende;
    	
    	
    	public NaamInvoer() 
    	{
    		
    		
    		this.setLayout(null);
    		this.setSize(453, 436);
    		this.setLocation(125, 160);
    		this.setOpaque(false);
    		
    		this.naamSpeler1 = new JTextField(30);
    		this.naamSpeler2 = new JTextField(30);
    		this.naamSpeler3 = new JTextField(30);
    		this.naamSpeler4 = new JTextField(30);
    		
    		this.naamSpeler1.setLocation(0, 0);
    		
    		
    		add(naamSpeler1);
    		add(naamSpeler2);
    		add(naamSpeler3);
    		add(naamSpeler4);
    		
    		this.naamInvoerImg = new ImageIcon(getClass().getResource("/images/naam invoeren2.png"));
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		g.drawImage(naamInvoerImg.getImage(), 0, 0, 453, 436, null);
    	}
    
    	public void actionPerformed(ActionEvent arg0)
    	{}
    	
    }

    ...And this is the JFrame class with that Jpanel class from above in it.
    Java Code:
    (IMPORTS etc..)
    
    
    public class Opstart extends JFrame implements ActionListener, KeyListener
    {
    
    	private GameBackground achtergrondPaneel;
    	private Spel eenSpel;
    	private ImageIcon systeemImage;
    	private int answer;
    	
    	
    	public Opstart()
    	{	
    		achtergrondPaneel = new GameBackground();
    		systeemImage = new ImageIcon(Opstart.class.getResource("/images/icon.fw.png"));
    		
    //		eenSpel = new Spel(4);
    		JPanel voerNaamIn = new NaamInvoer();
    		
    				
    		
    		this.add(voerNaamIn);
    		this.add(achtergrondPaneel);
    		this.setJMenuBar(menuBar());
    		this.setTitle("Quixo");
    		this.setLocation(500,100);
    		this.setSize(755, 755);
    		this.setLayout(null);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setResizable(false);	
    		this.setVisible(true);
    		this.setIconImage(systeemImage.getImage());
    		
    		addKeyListener(this);
            
    	}
    	
    	public static void main(String[] args)
    	{
    		
    		new Opstart();
    		
    	}
    	
    
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JTextField on JPanel-class within JFrame-class

    The reason is because you're giving your JPanel a null layout and are not specifying your component location or size. So one solution is to call setBounds(...) on your component, but I'm going to recommend that you *not* do this, and instead I will recommend that you not use a null layout unless you've got a very good reason to do so. You don't. Instead read up on the layout managers and learn how to use them to their best effect. You won't regret doing this.

  3. #3
    floris is offline Member
    Join Date
    Apr 2012
    Location
    Den Haag, the Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: JTextField on JPanel-class within JFrame-class

    My good reason for using a null layout is that I want to position the jtextfields pixelperfect on the screen.
    Howcome you are assuming I don't have a good reason for that?

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: JTextField on JPanel-class within JFrame-class

    Hi floris,
    Quote Originally Posted by floris View Post
    Howcome you are assuming I don't have a good reason for that?
    This is more a case of miscommunication than an attempt to invalidate your reason. I understand what you are attempting to achieve and your intention for using a null layout.
    Fubarable's advice is sound and it is worth researching the layout managers. The location of the textfield can be hardcoded instead but this is generally not advisable as this is effected by the resolution of the monitor you are viewing it on. What is seen as center on one resolution may appar bottom right on another.

    Regards.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: JTextField on JPanel-class within JFrame-class

    Howcome you are assuming I don't have a good reason for that?
    You don't.

    What happens when your program runs on a computer with settings that change the fonts of your GUI elements?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JTextField on JPanel-class within JFrame-class

    Quote Originally Posted by floris View Post
    My good reason for using a null layout is that I want to position the jtextfields pixelperfect on the screen.
    Howcome you are assuming I don't have a good reason for that?
    Because this won't work on different platforms. If you show us an image of your desired GUI, we can often help you with the right layout managers to use.

Similar Threads

  1. Replies: 2
    Last Post: 10-17-2011, 09:04 PM
  2. Replies: 0
    Last Post: 08-30-2011, 10:18 AM
  3. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 07:52 PM
  4. Replies: 3
    Last Post: 04-21-2009, 12:31 AM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •