0

I'm trying to learn Swing on my own. I'm playing with a toy program that asks the user to input their name. I put a JLabel and JTextfield into a JPanel where the user can input their name and submit. However my JTextfield is squished up and invisible and I can't get it to show (I've tried "setSize" to no avail).

Invisible JTextField

This is my code:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class NamePrompt extends JFrame{


    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField();
        textBoxToEnterName.setSize(40, 10);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitButton());
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        setSize(300, 150);
        setLocationRelativeTo(null);


    }



    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true);


    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • `textBoxToEnterName.setSize(40, 10);` See [this thread](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). – Andrew Thompson Dec 04 '12 at 23:02

1 Answers1

2
JTextField textBoxToEnterName = new JTextField(20);

See new JTextField(columns) for explanation. E.G.

NamePrompt

import java.awt.BorderLayout;
import javax.swing.*;

public class NamePrompt extends JFrame{

    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField(21);
        //textBoxToEnterName.setSize(40, 10);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        //submit.addActionListener(new SubmitButton());
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        //setSize(300, 150);
        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Now I'm getting a longer but really thin line (I can see a tiny bit of the top of the text I'm typing) – CodyBugstein Dec 04 '12 at 23:19
  • 1
    Don't call `setSize()` (anywhere in the code)! See the update. – Andrew Thompson Dec 04 '12 at 23:35
  • It works! Can you explain why not to call setSize()? And also, how can I move the text box to be positioned under the text? – CodyBugstein Dec 04 '12 at 23:48
  • I put a comment to the question, that links to a [thread where the Gurus provide the details](http://stackoverflow.com/q/7229226/418556). In this case, if the UI needs more 'white space' (e.g. more height to the dialog) this can be achieved in some cases by using layout padding (often declared in the constructor) or an `EmptyBorder` applied to the relevant panel. – Andrew Thompson Dec 04 '12 at 23:52
  • If you are really new to GUI programming take a look at the Java Docs, for they provide really good info on different methods for Swing components. – turnt Dec 07 '12 at 03:01