Search code examples
javaswinggraphicsinterfacejtextarea

textArea appear only if you minimize or select the text, how do i fix this?


Im doing my first graphic interface but i faced this problem, the text area textArea appear only if you minimize or select the text. I tried many things but nothing worked, can somebody help?

here is the code:

package lln_urn;

import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class Urn {
    //ATTRIBUTES

    //main frame
    private final JFrame frame;

    //various components
    private final JTextArea explanation_textarea;
    
    //font
    private final Font font = new Font("Segoe", Font.BOLD, 20);


    //CONSTRUCT
    public Urn() {
    
        //setup of the window
        frame = new JFrame("Law of large numbers");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setLayout(new GridLayout(1,1));
        frame.setResizable(false);
        frame.setVisible(true);
    
        //textarea
        explanation_textarea = new JTextArea();
        explanation_textarea.setBounds(5,0,300,50);
        explanation_textarea.setText("In this urn there are 2000 balls");
        explanation_textarea.setEditable(false);
            
        //adding components
        frame.add(explanation_textarea);
    
    }

    public static void main(String[] args) {
        Urn urn = new Urn();
    }

}

the output that i get how it looks after i minimize and take it out

I tried: setting bounds to different size, setting to editable, change the layout, change the background and text color, add the textarea to a panel, update the jdk,

Nothing solved the problem, i have no idea what else to try.


Solution

    1. leave default JFrame layout (BOrderLayout)
    2. do not set textarea bounds - layout manages the objects inside
    3. call frame.setVisible as last