The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Help with application not loading occasionally
Discuss Help with application not loading occasionally in the Java Help forum on Dev Shed. Help with application not loading occasionally Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 30th, 2013, 07:54 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 39 m 32 sec
Reputation 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
|

January 31st, 2013, 01:49 AM
|
 |
Lord of the Dance
|
|
|
|
|
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.
|

January 31st, 2013, 05:36 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

January 31st, 2013, 11:25 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 39 m 32 sec
Reputation Power: 0
|
|
Quote: | 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:
Original
- 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
|

January 31st, 2013, 11:51 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | 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.
|

January 31st, 2013, 12:35 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 39 m 32 sec
Reputation Power: 0
|
|
Quote: | 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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|