user3721668 February 2016

Java: differences between run and debug (cause of JTextField)

I'm doing a simple java application for the management of my passwords. I wrote this part of the program:

public class main {

    public static void main(String[] args) {

        JFrame jf = new JFrame("Ttile");
        JPanel jp = new JPanel();
        jp.setLayout(null);

        jf.setVisible(true);
        jf.setSize(1000,1000);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton jb = new JButton("Button");
        jb.setBounds(0,0,120,35);

        JTextField jtf = new JTextField(30);
        jtf.setBounds(100,100,120,30);

        jp.add(jb);
        jp.add(jtf);
        jf.getContentPane().add(jp);
    }
}

The problem is that when I instantiate a JTextField (just this component) the window (or better, the JPanel) doesn't show any other component in run mode, meanwhile in debug mode it works properly. Is it caused by my video card? Or it's just a problem with the current version of Netbeans/Java? I'm confused cause I cannot understand the reason :S.

Note: if I expand the window to fullscreen every component is visible.. Why this initial issue?

Thanks for your help.

Answers


user1803551 February 2016

setVisible(true) should be the last thing you do. Adding components dynamically (that is, after that call) will require you to revalidate the component hierarchy (else you get what you just saw).

Post Status

Asked in February 2016
Viewed 1,904 times
Voted 5
Answered 1 times

Search




Leave an answer