Hello all,

I've got an assignment due on thursday that is very easy, but I have one bug that is driving me nuts.

The assigment is to just create a basic GUI with 1 label, a text box, and a few check boxes, so it's not that hard. The problem is that it compiles fine with no errors or anything, but when I run it, nothing is showing up in the window unless I use the mouse to click the lower right corner of the window and resize it. Then the items show up.

It has been about 2 years since I've taken CS I (Got all the other stuff out of the way :mrgreen: ) but now, I'm a little rusty...

Here is my code:

/*
 * Tiny App
 *
 */

import javax.swing.*;
import java.awt.*;

public class TinyApp
{
    public static void main(String[] args)
    {
        //  Set-up JFrame
        JFrame jfWindows = new JFrame("Search Box");
        jfWindows.setSize(400, 150);
        jfWindows.setVisible(true);
        
        //    Set up Container for controls
        Container cp = jfWindows.getContentPane();
        cp.setLayout(new BorderLayout());

        //  Pane to hold the search box and label
        JPanel jpSearch = new JPanel();
        jpSearch.setLayout(new FlowLayout(FlowLayout.CENTER));
        cp.add(jpSearch, BorderLayout.NORTH);

        //    Search label
        JLabel lblSearch = new JLabel("Enter key phrase: ");
        jpSearch.add(lblSearch);

        //  Search text box
        JTextField jtfSearch = new JTextField(20);
        jpSearch.add(jtfSearch);

        //  Bottom Panel
        JPanel jpYear = new JPanel();
        jpYear.setLayout(new FlowLayout(FlowLayout.CENTER));
        cp.add(jpYear, BorderLayout.SOUTH);

        //  Create Year Checkboxes
        JCheckBox jcb1998 = new JCheckBox("1998");
        jpYear.add(jcb1998);
        JCheckBox jcb1999 = new JCheckBox("1999");
        jpYear.add(jcb1999);
        JCheckBox jcb2000 = new JCheckBox("2000");
        jpYear.add(jcb2000);
        JCheckBox jcb2001 = new JCheckBox("2001");
        jpYear.add(jcb2001);
    }
}

Thank you in advance for any help. This but is driving me nuts. I have even shown it to a couple of the TA's in the CS lab and they are a bit baffled by it.

Thank you,

Paul Allen (Antiparadigm)

After you have finished adding child components (ie the end of the program) you need to add the following:

jfWindows.pack();

I would also suggest adding:

jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);

The default is HIDE_ON_CLOSE, which leaves the process running until you explicitly exit the IDE/Runtime Environment.

You could also try adding all your components and THEN showing the JFrame.

JFrame jfWindows = new JFrame("Search Box");
...
//add components here...
...
jfWindows.setVisible(true);

That was right on DavidRyan.

The line:

jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);

Has to be implemented in an action listener right?

The line:

jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);

Has to be implemented in an action listener right?

You can just put it in main once you've created the JFrame. It's just a call to a method, afterall ;)

EXIT_ON_CLOSE use to have to be placed in an ActionListener. Not anymore though.

Thank you both your help really staightend me out.

Just one quick note:

jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);

was having problems with EXIT_ON_CLOSE. After a little research, I found I had to use:

jfWindows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Again, Thank you so much for your help!

Paul Allen (antiparadigm)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.