Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Swing Components Invisible as Startup

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing Components Invisible as Startup

    Hello everyone. I have done a program using swing GUI components, but whenever I run the program the jframe comes up empty. When I minimize and maximize the window or if I click at the bottom of the window (where you place your mouse to resize the frame), the components appear as normal. Even from command line it will start up like this. Has anyone else ever experienced this and can anyone advise me as to what I can do ro solve it?


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Swing Components Invisible as Startup

    Without some code demonstrating the problem its anyone's guess. My guess is that you are adding components after you make the window visible. Add components before (make sure to call pack() as well) or - if you must add them after you make the window visible - call validate().

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing Components Invisible as Startup

    Quote Originally Posted by copeg View Post
    Without some code demonstrating the problem its anyone's guess. My guess is that you are adding components after you make the window visible. Add components before (make sure to call pack() as well) or - if you must add them after you make the window visible - call validate().
    Thank you for replying. My code is just so long that I did not really want to post it until it was really necessary. And it is not necessary because you got it right. I had the frame made and its properties set before I added the components to it. So i just made the frame, added the components to it and set the frame properties. Thanks for you help again.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing Components Invisible as Startup

    Hi. I am still studying first year java. Sorry for posting on an old thread but it came up on a google search.
    I have exactly the same problem as the original post but I was unable to resolve it as he did. In my case I add all the swing components in a class constructor and an instance of this class is instantiated first thing in the main method. I've never had a problem with this before. Here is the code. Appreciate!

    public class WrdCount extends JFrame implements ActionListener
    {
    private StringBuffer sb;
    private static JTextPane textPane = new JTextPane();
    private static JButton cntBtn = new JButton("Count Words");
    private static JLabel cntLbl = new JLabel("Word Count = ");
    private static JTextField cntFld = new JTextField(4);

    private static JPanel mainPnl = new JPanel();
    private static JPanel topPnl = new JPanel();
    private static JPanel bottomPnl = new JPanel();
    private static JScrollPane scrollPane;

    private WrdCount()
    {
    scrollPane = new JScrollPane(textPane);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(400,200));

    //layouts
    topPnl.setLayout(new BorderLayout(50,50));
    topPnl.add(scrollPane, BorderLayout.CENTER);
    bottomPnl.setLayout(new FlowLayout(FlowLayout.CENTER,5,3));
    bottomPnl.add(cntBtn);
    cntBtn.addActionListener(this);
    bottomPnl.add(cntLbl);
    bottomPnl.add(cntFld);
    cntFld.setEditable(false);

    Container c = getContentPane();
    c.setLayout(new BorderLayout(10,50));
    c.add(topPnl,BorderLayout.CENTER);
    c.add(bottomPnl,BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public static void main(String[] args)
    {
    try
    {
    UIManager.setLookAndFeel("javax.swing.plaf.metal.M etalLookAndFeel");
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(null,"Could not set the Look And Feel for this application"
    , "Display Error", JOptionPane.WARNING_MESSAGE);
    }

    WrdCount f = new WrdCount();
    f.setTitle("Word Counter");
    f.setVisible(true);
    f.setSize(600, 400);
    f.setResizable(false);
    f.setLocation(200,200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    }

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing Components Invisible as Startup

    Quote Originally Posted by BiteCruncher View Post
    Hi. I am still studying first year java. Sorry for posting on an old thread but it came up on a google search.
    I have exactly the same problem as the original post but I was unable to resolve it as he did. In my case I add all the swing components in a class constructor and an instance of this class is instantiated first thing in the main method. I've never had a problem with this before. Here is the code. Appreciate!

    public class WrdCount extends JFrame implements ActionListener
    {
    private StringBuffer sb;
    private static JTextPane textPane = new JTextPane();
    private static JButton cntBtn = new JButton("Count Words");
    private static JLabel cntLbl = new JLabel("Word Count = ");
    private static JTextField cntFld = new JTextField(4);

    private static JPanel mainPnl = new JPanel();
    private static JPanel topPnl = new JPanel();
    private static JPanel bottomPnl = new JPanel();
    private static JScrollPane scrollPane;

    private WrdCount()
    {
    scrollPane = new JScrollPane(textPane);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(400,200));

    //layouts
    topPnl.setLayout(new BorderLayout(50,50));
    topPnl.add(scrollPane, BorderLayout.CENTER);
    bottomPnl.setLayout(new FlowLayout(FlowLayout.CENTER,5,3));
    bottomPnl.add(cntBtn);
    cntBtn.addActionListener(this);
    bottomPnl.add(cntLbl);
    bottomPnl.add(cntFld);
    cntFld.setEditable(false);

    Container c = getContentPane();
    c.setLayout(new BorderLayout(10,50));
    c.add(topPnl,BorderLayout.CENTER);
    c.add(bottomPnl,BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public static void main(String[] args)
    {
    try
    {
    UIManager.setLookAndFeel("javax.swing.plaf.metal.M etalLookAndFeel");
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(null,"Could not set the Look And Feel for this application"
    , "Display Error", JOptionPane.WARNING_MESSAGE);
    }

    WrdCount f = new WrdCount();
    f.setTitle("Word Counter");
    f.setVisible(true);
    f.setSize(600, 400);
    f.setResizable(false);
    f.setLocation(200,200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    }
    Ok, try this. Comment the line f.setVisible(true). Make a JPanel named c to replace the container. Remove the line Container c = getcontentpane. So basicaly your top and bottom panels will be in another panel named c. The finally add c to f which is the wrdCount object. f.add(c). Then add the line f.setvisible = true.
    So in other words you are not using the container.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Swing Components Invisible as Startup

    Not sure what you're expecting from the GUI, but for me, the GUI is visible from the offset.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Components that interact with eachother
    By Flamespewer in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2010, 08:27 PM
  2. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM
  3. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 12:46 PM
  4. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM
  5. Replies: 1
    Last Post: April 29th, 2009, 06:26 AM