0

I have started a program in JAVA and SOMETIMES, when I run it or debug it, it shows an empty white window. I have no idea why, but I redebug it and it shows the window correctly. Btw, it has nothing to do with the mysql connect void at the end.

Here is the code:

package com.hinx.client;

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


public class Main {

public static void main(String [] args) 
{
    createWindow();
}


static void createWindow()
{


    //Create panel
    JPanel content = new JPanel();
    content.setLayout(null);
    //Build the frame
    JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 233);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(content);
    frame.setVisible(true);

    //Create username label
    JLabel username = new JLabel("Username:");
    username.setFont(new Font("Arial", Font.BOLD, 15));
    username.setForeground(Color.white);
    username.setBounds(34, 8, 100, 50);

    //Create password label
    JLabel password = new JLabel("Password:");
    password.setFont(new Font("Arial", Font.BOLD, 15));
    password.setForeground(Color.white);
    password.setBounds(36, 85, 100, 50);

    //Create username field
    JTextField usernamet = new JTextField(20);
    usernamet.setBounds(12, 50, 125, 30);
    usernamet.setBorder(javax.swing.BorderFactory.createEmptyBorder());

    //Create password field
    JTextField passwordt = new JTextField(20);
    passwordt.setBounds(12, 125, 125, 30);
    passwordt.setBorder(javax.swing.BorderFactory.createEmptyBorder());

    //Add the login button
    JButton login = new JButton("Login");
    login.setBounds(0, 175, 150, 30);
    login.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

        }
    });

    //Create login panel
    JPanel loginpanel = new JPanel();
    loginpanel.setLayout(null);
    loginpanel.setBounds(0, 0, 150, 400);
    loginpanel.setBackground(Color.gray);

    //Add the items to the loginpanel
    loginpanel.add(username);
    loginpanel.add(password);
    loginpanel.add(usernamet);
    loginpanel.add(passwordt);
    loginpanel.add(login);

    //Add the items to the content panel
    content.add(loginpanel);
}

protected void connect()
{
    String driver = "com.mysql.jdbc.Driver";
    String dbadress = "";
    String dbname = "";
    String username = "";
    String password = "";
    try
    {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(dbadress+dbname, username,password);
        Statement st = conn.createStatement();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
James L.
  • 131
  • 2
  • 3
  • 7
  • 2
    `content.setLayout(null);` Didn't we address that in your **last** question? – Andrew Thompson May 03 '13 at 15:03
  • 2
    Usin null-Layout is really fighting the system. It leads to horrible code, very hard to maintain, impossible bugs etc... Use them appropriately and they will ease your day. – Guillaume Polet May 03 '13 at 15:07
  • 1
    I just realized the frame size is too small to display the components where you have placed them. More fuel to the 'use layouts and `pack()` the GUI' argument. – Andrew Thompson May 03 '13 at 15:13
  • 1
    I'm vote to exact duplicate, [again the same problems, by three answerers described on another post](http://stackoverflow.com/q/16360418/714968) – mKorbel May 03 '13 at 15:27

3 Answers3

2
frame.setVisible(true);

make it the last statement, after you add all the components to the JFrame.


Also, it is generally a best practice to do any swing-related code in the GUI-thread (EDT):

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            createWindow();
        }
    });
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

Swing GUIs should be started on the Event Dispatch Thread. See Initial Threads for more details.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Invoke frame.setVisible(true); at the end of your method ( after adding all the components to panel)

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33