Search code examples
javaswingjtextfieldjtextarea

JTextField and JTextArea randomly not working when program is run


I just learning java now and I am trying to figure out what is wrong, but every time I run the code I get a different result. It is a program that I am writing more or less to figure out how to use java, but some of the objects are not appearing the way they should be.

package main;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
//import java.awt.event.*;

public class Main extends JFrame {

 JButton button = new JButton();
 JTextField textField = new JTextField();
 JTextArea textArea = new JTextArea();
 int buttonClicked;

public Main() {
    Toolkit tk = Toolkit.getDefaultToolkit(); // creates toolkit
    Dimension screenSize = tk.getScreenSize(); // sets screen dimensions
    Dimension frameDim = new Dimension(400, 400); // sets frame dimensions
    int xPos = (screenSize.width / 2) - (frameDim.width / 2); // sets xPos
    int yPos = (screenSize.height / 2) - (frameDim.height / 2); // sets yPos

    this.setSize(frameDim); // sets jframe size
    this.setVisible(true); // sets jframe visible
    this.setLocation(xPos, yPos); // sets jframe location to xPos and yPos
    this.setResizable(false); // sets Resizable to false
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exits on close
    this.setTitle("This is a Frame");

    JPanel panel = new JPanel(); // creates a panel
    this.add(panel); //adds panel to JFrame

    JLabel label = new JLabel("I'm a label"); // creates a label with text
    label.setText("I say something"); // changes the text in the label
    label.setToolTipText("this is a label"); // sets the tool-tip
    panel.add(label); // adds the label to the panel

    JButton button = new JButton("I am a button"); // creates a button
    button.setText("I am still a button"); // changes text on the button
    button.setBorderPainted(true); // adds border (default)
    button.setContentAreaFilled(true); // adds area inside border (default)
    button.setToolTipText("It's a button"); // sets the tool-tip
    panel.add(button); // adds the button to the panel

    JTextField textField = new JTextField("words", 15); // creates textField
    textField.setColumns(10); // sets textField size
    textField.setText(""); //changes textField text
    textField.setToolTipText("this is a textField"); // sets the tool-tip
    panel.add(textField); //adds textField to the panel

    JTextArea textArea = new JTextArea(15, 20);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    panel.add(textArea);
    JScrollPane scrollBar = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    panel.add(scrollBar);
    this.add(panel);


}

public static void main(String[] args) {
    new Main();
//      new TestTextArea();
}
}

the problem is that when I run this is pretty much a shot in dark of whether or not the textArea and textField will appear in the window. Sometimes, but very rarely, the button and label don't even appear... I can't think of a solution.

I am running java 7 update 60 if this helps, also it did not work on update 55 either


Solution

  • This is explained in every Swing tutorial, but I'll repeat it again. You shouldn't call setVisible(true) before all the elements are added. That should be the last thing you do, just after calling this.pack().

    Moreover, Swing components should never be used outside of the event disptach thread. You're doing everything in the main thread. Read http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

    And finally, the panel should be added once, and not twice, to the frame.