Results 1 to 3 of 3
  1. #1
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Post Beginning Swing Question

    Hey guys, i have a general question about swing. Sometimes when i add something to my JFrame, nothing will show up when i run it, until i resize it. here is some code from a test where i was just messing around with Menu bars.

    Java Code:
    package texteditor;
    import javax.swing.*;
    import java.awt.event.KeyEvent;
    
    public class WindowFactory {
        
        public void createWindow(){
            JFrame frame = new JFrame("Notepad");
            frame.setSize(600,600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            
            JMenu file = new JMenu("File");
            file.setMnemonic(KeyEvent.VK_U);
            file.add(new JMenuItem("Save"));
            
            JMenu sub = new JMenu("Sub");
            sub.setMnemonic(KeyEvent.VK_U);
            sub.add(new JMenuItem("blah"));
            file.add(sub);
            
            JMenuBar bar = new JMenuBar();
            bar.add(file);
            
            frame.setJMenuBar(bar);
        }
    }
    When i run this, the entire frame looks blank, but when i resize it, the menu bar pops up instantly. Sorry if this is a newbie question. Thanks for the help guys

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You may be calling setVisible before adding all your components. Don't do that. Instead add your components, call pack on the JFrame and then setVisible(true).

  3. #3
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    OHHHH. that makes so much sense. I knew it would be an easy fix, and something simple like that. Thanks!

Similar Threads

  1. JDBC with Swing question
    By hacker9116 in forum JDBC
    Replies: 4
    Last Post: 04-22-2011, 05:03 PM
  2. Swing timers question?
    By intel i7 in forum AWT / Swing
    Replies: 5
    Last Post: 12-25-2010, 12:39 AM
  3. Swing question
    By Psyclone in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 10:50 PM
  4. JFrame/Swing Question
    By Moncleared in forum AWT / Swing
    Replies: 9
    Last Post: 01-16-2009, 08:16 PM
  5. Swing Question, can you help?
    By Silentstormz in forum AWT / Swing
    Replies: 4
    Last Post: 09-21-2008, 03:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •