0

I have to create a text field, a text area, and two buttons, but I am stuck on the text field because I cant get it to appear when I run my program. My JFrame keeps appearing empty.

public class SentanceBuilder extends JFrame {
    private JTextField textField = new JTextField(50);
    private JTextArea textArea = new JTextArea(200,200);
    private JButton Submit = new JButton();
    private JButton Cancel = new JButton();

public SentanceBuilder(){
   textField.setVisible(true);
   textField.setLocation(50, 50);
   this.textField();
   this.setSize(400, 300);
   this.setLocationRelativeTo(null);
   this.setVisible(true);
   }

public void textField(){


    String textContent = textField.getText();

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3380752
  • 1
  • 1
  • 2
  • `textField.setLocation(50, 50);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Oct 15 '14 at 23:24
  • `private JTextArea textArea = new JTextArea(200,200);` You really want this text area to be 200 columns by 200 rows? I don't have a monitor big enough to render that! I suspect you thought those were pixels values.. – Andrew Thompson Oct 15 '14 at 23:26

1 Answers1

3

You never add the textField variable to a container such as a JPanel that is held by the top-level window, such as a JFrame. In fact, in your code, you add nothing to your JFrame!

If this were my GUI, I'd

  • Create my JTextField
  • Create a main JPanel to hold my components.
  • Add it and other components to the main JPanel via the JPanel's add(...) method.
  • Add the main JPanel to the JFrame's contentPane via the JFrame's add(...) method.
  • Call pack() and then setVisible(true) on the JFrame, but only after adding all components to it, not before.
  • Read the Swing tutorials since this beats guessing every time. You can get links to the tutorials at the Swing Tag Info link.

e.g.,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyFoo extends JFrame {
   private static final long serialVersionUID = 1L;
   private JTextField textField = new JTextField(10);
   private JButton button = new JButton("Foo Button");

   public MyFoo() {
      super("My JFrame");
      // so Java will end when GUI is closed
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      // code to be called when button is pushed
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            // TODO code that runs when button is pushed.
         }
      });

      // panel to hold everything
      JPanel mainPanel = new JPanel();
      // add all to the panel
      mainPanel.add(new JLabel("Text Field:"));
      mainPanel.add(textField);
      mainPanel.add(button);

      // add the panel to the main GUI
      add(mainPanel);
   }

   // start up code
   private static void createAndShowGui() {
      JFrame mainFrame = new MyFoo();
      mainFrame.pack();
      mainFrame.setLocationRelativeTo(null);
      mainFrame.setVisible(true);
   }

   public static void main(String[] args) {
      // call start up code in a Swing thread-safe way
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373