0

I have a very strange problem with my java application. I basically click the JButton and the new window I want to open opens but with no content showing. Here is what happens.

This is what happens after clicking the button

If I run the class on its own without using a JButton it runs proberly like so. The window working without JButton

Here is the code for the button.

public Create()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 629, 316);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 255, 204));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnBuildData = new JButton("Build Graph");
        btnBuildData.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {

                //CreateTest frame = new CreateTest();
                new CreateTest().setVisible(true);


            }
        });

        btnBuildData.setBackground(Color.WHITE);
        btnBuildData.setForeground(Color.BLACK);
        btnBuildData.setBounds(29, 59, 107, 23);
        contentPane.add(btnBuildData);

This is strange to me because I have used this code for other classes and it works as intended. I have tried many different ways to do the same thing but none of them have worked. Here is some code for the frame I am opening with the button.

public class CreateTest extends JFrame {
    public CreateTest() {
    }
    //Create the connection to Neo4j
    Driver  driver      = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "*****", "*******" ) );
    Session session     = driver.session();
    StatementResult resultVariable;
    Record          recordVariable;

    //Create variables to manage communication with Neo4j
    String  resultString = new String();
    Query   neoQuery = new Query();





    private JTextField progressTextField;


    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                new CreateTest().initUI();



            }
        });
    }

    protected void initUI() 
    {
        final JFrame frame = new JFrame();

        //the form
        frame.setTitle(CreateTest.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        JButton button = new JButton("Click here to add data");
        button.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {


                doWork();
            }
        });




        progressTextField = new JTextField(25);
        progressTextField.setEditable(false);
        frame.getContentPane().add(progressTextField, BorderLayout.NORTH);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    protected void doWork() {
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                CreateTest frame = new CreateTest();
}


            @Override
            protected void process(List<Integer> chunks) {
                progressTextField.setText(chunks.get(chunks.size() - 1).toString());
            }

            @Override
            protected void done() {
                progressTextField.setText("Success: All Nodes have been added ");
            }
        };
        worker.execute();
    }


}

There is a difference between the two windows. One being absolute layout and the other jpanel but I don't think this is the problem. If anyone has any ideas please let me know, any ideas will be appreciated. Thanks.

David Gardener
  • 93
  • 2
  • 13
  • 2
    With `new CreateTest().setVisible(true);` you are creating a new `CreateTest` instance and make it visible.But the constructor of `CreateTest` does nothing special, so you end up with basically an empty frame . – Arnaud Nov 28 '17 at 14:35
  • But the frame does contain content when it is ran without a jbutton – David Gardener Nov 28 '17 at 14:41
  • Yes because you create a new instance in the action listener, and never call `initUI()` on it. – Arnaud Nov 28 '17 at 14:48
  • ^ Basically, when you run the file it calls the main method, when you create an instance of the class it runs the constructor that you call. Your constructor right now are the first 2 lines in the class `public CreateTest() {}` which does nothing. – Touniouk Nov 28 '17 at 14:53
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. 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). .. – Andrew Thompson Nov 28 '17 at 16:04
  • 1
    .. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 4) There seems to be no compelling reason in this instance, to extend `JFrame`. The code should instead instantiate a standard frame and adjust it to need. 5) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Nov 28 '17 at 16:04

1 Answers1

1

Your calling an empty constructor with new CreateTest()

public CreateTest() {
}

It does nothing since your code is outside of it.

James F
  • 130
  • 1
  • 10