Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel not shown in JFrame

 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for this question. But I'm staring at my code for a week, and not making headway.

I try to create an application that needs to talk to a MySQL database. Small steps make one application. I was working on the GUI and the panels in the frame where displaying as expected. Next I needed to start with the database code. I did something (what?) and saw my JFrame did not display the JPanels any more. And now I can't find out why.

My code.


The main class containing the JFrame:


And finally the class in which me thinks it has gone wrong (in the method createUserPanel). It contained code now in other methods, like createUserPanel, createNamePanel and createPeriodPanel. In doing so I hoped to get better maintainable code, but it turned out to not working at all...


And now some printout to console:


The panels have a dimension when created in their respective methods. But once in the JFrame their (createUserPanel, height is 0) is zero.

I already tried to use old code, but the result was the same. Do you see what is wrong/missing/causing my problems? Otherwise I suspect I to need to blame ET.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see you created "frame" twice in the constructor. The 2nd one (the one returned by createFrame()) will be the frame your app is using.

For the panel, I see you passed in "this" to it, which in your case is the main window frame. Then in the createUserPanel() line 28 you add(...) but to what? I think it should be


I see you use a Properties in the constructor, what's the purpose of that?

Looking at your code, the following can make it more easy to maintain and understand.
* the MyApplication class can "extends JFrame" rather than creating it inside.
* menu bar and its menus/menu items can be separate out to its own class
* each of the smaller panels can also be separate out into its own class
* for buttons, the action listeners again can be separate out = 2 ways a) implements ActionListener or b) extends javax.swing.AbstractAction

Hope this helps.
 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Welcome to the Ranch

Thanks

K. Tsang wrote:I see you created "frame" twice in the constructor. The 2nd one (the one returned by createFrame()) will be the frame your app is using.

You mean?


If you remove the first one and run the app you'l get a null pointer exception.


K. Tsang wrote:For the panel, I see you passed in "this" to it, which in your case is the main window frame. Then in the createUserPanel() line 28 you add(...) but to what? I think it should be


This did not work. But in the MyApplication class I had a getFrame. I eventually used

But this did not pan out.


Actually to display a user name and personnel number in the UserInfoPanel. That part I omitted. Later on I might use info obtained from the login screen to get this data.

K. Tsang wrote:Looking at your code, the following can make it more easy to maintain and understand.
* the MyApplication class can "extends JFrame" rather than creating it inside.
* menu bar and its menus/menu items can be separate out to its own class
* each of the smaller panels can also be separate out into its own class
* for buttons, the action listeners again can be separate out = 2 ways a) implements ActionListener or b) extends javax.swing.AbstractAction

Hope this helps.


Alas. No luck yet. I will pick up your suggestion later when I have my Frame displaying what it should.

Let me philosophize a bit. In MyApplication's constructor I state


Than the creation of the UserInfoPanel should be enough to get it shown in the JFrame ( & "puls hair out"). But, no. That did not work either.

Thanks for the feedback.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe setting up the GUI in the following manner may help. Keeping in mind every component can be its own class.

 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reworked my code so instead of big classes I work with smaller classes. But the problem persisted, until I changed


to


And all of a sudden everything showed up. So, thanks for your help.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it works for you.

In fact your createXXXPanel methods can be another class. But the UserInfoPanel will be the place where you lay these various (sub)panels into place.
 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Glad it works for you.


And I lost it again.

Here is some logging:


All panels I create have a zero dimension. That's not good.



And the first of the methods in which I create JPanels:


And just yesterday it worked. I compiled and ran after every change, except the one where I lost it. It must be enemy action (peeks under desktop).
 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Orlok Nosferatu wrote:
K. Tsang wrote:Glad it works for you.


And I lost it again.


I found a possible explanation in "In Java 1.6, why does adding a Jpanel to another JPanel using the default add() not display the added panel?" the person asking the question states:

my Panel will show.

If I do this:

my JPanel will not show.

I'm now looking into this as a possible solution.
 
Orlok Nosferatu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the reason. No layout manager &
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Orlok Nosferatu wrote:I found the reason. No layout manager &


JPanel default layout is FlowLayout. Unless you set the layout to null or something else. I see you were using GridBagLayout. Not good with grid bag myself so can't comment much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!