#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Solved: Help with application not loading occasionally


    I really need help with an error I've been trying to fix for the past 2 days. I'm using a CardLayout and switching between different JPanels. I'm Pulling the JPanel into the Main JPanel which is shown to the user.

    I'm having an issue because occasionally my application will load but no GUI components will be shown. Other times when I load the application, it loads perfectly. I really can't figure it out.

    I'm using Tomcat Juno IDE to develop the application, when the app loads and doesn't show anything I see a quick glimpse of an error occurring in the console, but it disappears immediately after and I don't know any way to reveal it.

    I have created a quick code example if anyone can provide any help. I don't understand why it's working occasionally.
    Code:
    public class TestClass { 	 	
    
    JFrame MainScreenFrame; // Declare main JFrame
    JPanel MainPanel = new JPanel(); // Panel used to set the current layout of the frame to new 
    
    JPanel 	CardLayout cards = new CardLayout(); 
    
    JPanel AddEmployeePanel = new JPanel(newGridBagLayout());
    JPanel ShowEmployeePanel = new JPanel(newGridBagLayout());
    
    public TestClass(){  		
    MainScreenFrame = new JFrame("TestApplication"); // Create new MainFrame Panel used when opening up the application and set the title 		
    
    MainScreenFrame.setVisible(true);
    MainScreenFrame.setSize(900, 650);
    MainScreenFrame.setLocationRelativeTo(null);
    MainScreenFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JFrame MainScreenFrame.setResizable(false); // Dont allow user to resize frame.
    
    MainPanel.setLayout(cards); // actually set the current layout of MainPanel // E.g. of how im adding components
    
    JLabel enterEmployeeInfo = new JLabel("Hmmm?");
    enterEmployeeInfo.setFont(new Font("Arial", Font.BOLD, 16)); 
    
    // SetLayout method used to set gridbagconstraints
    AddEmployeePanel.add(enterEmployeeInfo, c); // c defines gridbagconstraints
    JLabel lolol = new JLabel("whatever");
    lolol.setFont(new Font("Arial", Font.BOLD, 16)); 
    // SetLayout method used to set gridbagconstraints
    ShowEmployeePanel.add(lolol, c); // c defines gridbagconstraint 
    
    MainPanel.add(AddEmployeePanel, "ADD");
    MainPanel.add(ShowEmployeePanel, "SHOW");
    MainScreenFrame.add(MainPanel);
    cards.show(MainPanel, "ADD"); 	} 	 	
    
    public static void main(String[] args) { 		
    new TestClass(); // Call this class  	
    }}
    [Sorry about indentation Is there no Java Syntax Tag?

    I'm sure it's really easy, but I haven't been learning Java too long. Thank you.
    Last edited by CraigR15; January 31st, 2013 at 12:59 PM. Reason: Now solved
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    How do you start you program? if you dont see the console, it sounds you are stating it from your editor.
    have you tried to open a command prompt and start you program that way?

    When posting code, the indention will be how you make it, code tags only preserve your format.
    For syntax highlighting, you can use [highlight=java] my code [/highlight] instead of the code tags.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    One possible problem is calling setVisible() BEFORE adding any components to the frame.
    Try calling it AFTER the components that you want to see have been added.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    One possible problem is calling setVisible() BEFORE adding any components to the frame.
    Try calling it AFTER the components that you want to see have been added.

    I havn't used the setVisible at all throughout the application. All components are visible all the time. Is this bad? Any more strain on memory resources?

    Thank you, I will use Java code tags from now on. How do you start the program from the command line?

    I have managed to solve the problem and it turned out to be very easy. I was adding the JPanels add & show to the mainpanel before adding the mainpanel to the frame. I solved it by doing this:

    E.g.

    java Code:
     
    MainScreenFrame.add(MainPanel); // Add before JPanels 
     
    MainPanel.add(AddEmployeePanel, "ADD");
    MainPanel.add(ShowEmployeePanel, "SHOW");
     
    cards.show(MainPanel, "ADD"); // then show the main panel
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I havn't used the setVisible at all
    Code:
    MainScreenFrame.setVisible(true);
    Did you do a Find for setVisible? I found the above in the posted code.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Code:
    MainScreenFrame.setVisible(true);
    Did you do a Find for setVisible? I found the above in the posted code.

    Sorry I thought you was talking about the a GUI component being set to visible which I didn't think was possible, not the frame.

    Cheers

IMN logo majestic logo threadwatch logo seochat tools logo