Weird frame behavior

I am not sure why, but when I run the a JFrame program, nothing displays. I have to resize the window, then everything displays correctly.

I have used pack(), setBounds(), setVisiable()

Where do I make mistakes?
LVL 1
tiger0516Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
can you post your code
0
objectsCommented:
sounds like your adding/removing components without calling revalidate()
0
sciuriwareCommented:
Does it display correctly after the first              setVisible(true);                ??

;JOOP!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Mayank SAssociate Director - Product EngineeringCommented:
>> nothing displays

Meaning that the JFrame is visible (full-size) with no components in it? Or that you see only one tiny window and then you have to resize it? If its the second case, you have probably not set the size of the JFrame itself using setSize ( width, height ) ; like say, setSize ( 800, 600 ) ; Try setting it in the constructor after you add all components to the content-pane. If its the first case - what layout are you using? If it is null layout, try adding a new JLabel ( "" ) to the content-pane after you add everything else.
0
tiger0516Author Commented:
           JFrame frame=new JFrame();
            frame.pack();
                                //frame.setSize(800,500);
            frame.setBounds(10,10,800,500);
            frame.setTitle("Assignment 3");
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setJMenuBar(new MenuBar().createMenuBar());
            frame.setLayout(new FlowLayout());
                                frame.add(new MainPane().getPane());
            frame.setVisible(true);

>Meaning that the JFrame is visible (full-size) with no components in it?
Yes. Full size, nothing displays; have to resize the window(just a little bit) and then everything shows up
0
objectsCommented:
move your call to pack:

    frame.pack();
    frame.setVisible(true);

0
tiger0516Author Commented:
>move your call to pack:

>    frame.pack();
>    frame.setVisible(true);

It brings a window which is minimized. Yes, after I maxmize the window, everything shows up correctly, but there is no difference with my previous case, which also requires me to resize the window.
0
objectsCommented:
sorry didn't see the setBouinds() call, you want to move it

   frame.setBounds(10,10,800,500);
 frame.setVisible(true);
0
objectsCommented:
and get rid of pack() its not doing anything.
0
Mayank SAssociate Director - Product EngineeringCommented:
setBounds () should be used if you have a null layout (i.e., setLayout ( null ) ;, then add your components to the content-pane).
0
hoomanvCommented:
I tested your code just with a little change and then everything have gone well
possibly theres somthing wrong in "new MainPane().getPane()"

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

class Test {
    public static void main(String[] args) {
        JFrame frame=new JFrame();
        frame.pack();
        //frame.setSize(800,500);
        frame.setBounds(10,10,800,500);
        frame.setTitle("Assignment 3");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        //frame.setJMenuBar(new MenuBar().createMenuBar());      // commented by me
        frame.setLayout(new FlowLayout());
        frame.add(new JButton("test"));        // instead of frame.add(new MainPane().getPane());
        frame.setVisible(true);
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
>> frame.add(new MainPane().getPane());

Perhaps it should be frame.getContentPane ().add ( new MainPane ().getPane () ) ;

>> frame.add(new JButton("test"));        

Should also be frame.getContentPane ().add ( new JButton ( "test" ) ) ;
0
hoomanvCommented:
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

       frame.add(child);
 
And the child will be added to the contentPane
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.