Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Display a JTextField inside a JPanel

  1. #1

    Default Display a JTextField inside a JPanel

    Hello, I use a JPanel to draw all my graphics and I need to get some user input so I thought that I should add a JTextField.
    I have tried to add the JTextField but it just wont show up:

    //snip
    this.setVisible(true);
    this.setFocusable(true);
    this.setSize(800, 480);
    this.addMouseListener(new MouseListener());
    this.addMouseMotionListener(new MouseListener());
    this.addKeyListener(new KeyboardListener());
    this.setLayout(new BorderLayout());
     
    JTextField text = new JTextField();
    text.setVisible(true);
    text.setPreferredSize(new Dimension(40, 20));
    this.add(text);
    //snip

    Also I have tried to attach it to my JFrame as well and whenever I try to resize the frame, I can see the JTextField but it fills the entire JFrame.
    Last edited by nivangerow; January 8th, 2012 at 12:17 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    Can you make a small complete program(SSCCE) to show the problem. Something that compiles and executes. Your short <snip> doesn't show enough.

  3. #3

    Default Re: Display a JTextField inside a JPanel

    Well that "snip" is basically my code. Just put it in a JFrame.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Display a JTextField inside a JPanel

    Your code doesn't define what 'this' is, but I presume it is a JFrame (for future reference, to avoid confusion post an SSCCE). If you add components to a container (JFrame) after it is realized (visible) you must validate the container (this appears to be the case in your code snippet presuming my assumptions are correct).

    In other words, add components before you make it visible or call validate or revalidate and repaint.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    Just put it in a JFrame.
    There needs to be code that shows your problem. I'm sure you can create the code needed for testing.

  6. #6

    Default Re: Display a JTextField inside a JPanel

    Ok im creating it.

  7. #7

    Default Re: Display a JTextField inside a JPanel

    This is my code, but the thing is that it works. In the actual program I have my JPanel in a separate class but I dont think that makes a difference.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Main extends JFrame{
     
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("thing");
    		frame.setSize(800, 480);
    		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    		frame.setLocation((int)(dim.getWidth()/2)-(frame.getWidth()/2), (int)(dim.getHeight()/2)-(frame.getHeight()));
    		frame.setResizable(true);
    		frame.setMinimumSize(frame.getSize());
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		panel.setVisible(true);
    		panel.setFocusable(true);
    		panel.setSize(800, 480);
    		frame.add(panel);
    		JTextField text = new JTextField();
            text.setVisible(true);
            text.setPreferredSize(new Dimension(40, 20));
            panel.add(text);
    		frame.setBackground(Color.BLACK);
    		frame.validate();
    	}
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    If it works, then there is nothing to change, is there?
    We'd need the code that does not work.

  9. #9

    Default Re: Display a JTextField inside a JPanel

    The thing is that that is the code. I have just found out that the reason why I cant see the JTextField is because I am drawing images onto the JPanel using the paint() method. That seems to paint over the JTextField. Is there any way to move it or something?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    That seems to paint over the JTextField
    I guess it is a question of who has control over what is painted in the JPanel? You or the layout manager.
    Can you split the panel into a paint area and a textfield area?

  11. #11

    Default Re: Display a JTextField inside a JPanel

    I think that I do. I want the JTextField on top of the drawn images. How do I do that? I need to change it's Z position, but how?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    I think I found a program that did that by googling. I can't remember the program's name.
    Try looking for: java adding background image

  13. #13

    Default Re: Display a JTextField inside a JPanel

    Quote Originally Posted by Norm View Post
    I think I found a program that did that by googling. I can't remember the program's name.
    Try looking for: java adding background image
    Do you mean this: How to set background image in Java? - Stack Overflow
    He is just drawing an image and I am doing that as well. Is there any way to change Z positions? Am I missing something?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Display a JTextField inside a JPanel

    The program I found had an image behind some labels and text fields.

Similar Threads

  1. display pdf file into jpanel problem
    By Jhovarie in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2011, 03:29 PM
  2. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  3. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  4. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM