JTextField not showing up in JPanel on launch

15,809

Solution 1

  • Use Event Dispatch Thread for creating GUI components
  • Do not call setVisible(..) before all components have been added to the JFrame ( this is the above code snippets actual error +1 to @Clark)
  • Do not unnecessarily extend JFrame class
  • Do not call setSize(..), rather simply call JFrame#pack() before setting the JFrame visible
  • Do not call setSize() on JTextField rather look at its constructor: JTextField(String text,int columns)
  • Use appropriate LayoutManagers, see here for some examples: A Visual Guide to Layout Managers

Here is an example I made (basically your code with fixes):

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JavaSwingTextfield {

    private JTextField myTextField;

    public JavaSwingTextfield() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myTextField = new JTextField("Start");
        
        // Add the label to the JFrame
        frame.add(myTextField);
        
        //pack frame to component preferred sizes
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Create UI on EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JavaSwingTextfield();
            }
        });
    }
}

Solution 2

You're adding the JTextfield to the JFrame AFTER you've made the JFrame visible. Just add the JTextField before hand.

Share:
15,809
tazboy
Author by

tazboy

Updated on June 09, 2022

Comments

  • tazboy
    tazboy almost 2 years

    The JTextField is there because when I move the mouse over where it's supposed to be the mouse icon changes to a cursor, then when I click it shows up. But it's invisible at launch. What am I missing?

    public class JavaSwingTextfield extends JFrame {
        private static final long serialVersionUID = 1L;
    
        JTextField myTextField;
    
        public JavaSwingTextfield(){
    
            /***** JFrame setup *****/
    
            // Set the size of the window
            setSize(600,600);
    
            // Make the application close when the X is clicked on the window
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            // Makes the JFrame visible to the user
            setVisible(true);
    
            /***** JFrame setup END *****/
    
    
            /***** JButton setup *****/
    
            // Create a JLabel and set its label to "Start"
            myTextField = new JTextField("Start");
    
            // Set the label's size
            myTextField.setSize(100, 50);
    
            // Put the label in a certain spot
            myTextField.setLocation(200, 50);
    
            // Set a font type for the label
            //Font myFont = new Font("Serif", Font.BOLD, 24);
            //myTextField.setFont(myFont);
    
            // Add the label to the JFrame
            add(myTextField);
    
            /***** JButton setup END *****/
    
        }
    
    
        /***** The main method *****/
        public static void main(String[] args){ 
    
            new JavaSwingTextfield();
    
        }
    
    }
    
  • Andrew Thompson
    Andrew Thompson over 11 years
    "If you don't want to do that, call.." That is a kludgy work around to deeper problems.
  • Clark
    Clark over 11 years
    That's very true. I'll fix that.
  • tazboy
    tazboy over 11 years
    Yep, I didn't know that you have to add all the components to the JFrame and then show the JFrame. Now it shows up. Now I'll have to work with Andrew Thompson's suggestion about sizing because right now the textfield fills the entire window.
  • tazboy
    tazboy over 11 years
    What do you mean by "Do not unnecessarily extend JFrame class"? Thanks.
  • David Kroukamp
    David Kroukamp over 11 years
    @tazboy its a pleasure. and you only extend classes to add functionality.
  • tazboy
    tazboy over 11 years
    I had the misunderstanding, or ignorance, that in order to create a JFrame I needed to extend the class. But I guess, like any other object that's not available in the stock class, I just need to import it. Can you give me one reason I would need to extend using the JFrame class?
  • David Kroukamp
    David Kroukamp over 11 years
    @tazboy if you want to add new functionality to it, like for example this question stackoverflow.com/questions/13065032/… in which the OP wants his JFrame to be resized only after mouse has been released.