• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Click a button to display an image. Trouble with add panel to a frame. Help !!

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try to create a GUI that when you click a button a image is display. Here is my approach. I create two panel call "imagePanel" and "btnPanel". I add the button in the btnPanel, then add btnPanel to the frame. I try to do the same with "imagePanel", but the program complaint. It pass the compile error, but when I run it, it give error "java.lang.NullPointerException". I create the "imagePanel" inside an dispatching event which is actionPerformed(). I have a feeling that you cant create a GUI inside an dispatching event. Here is my code.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thang, I tried your program. and I did 2 things that could not produce NullPointerException. Either declare your imagePanel or put getContentPane().add(imagePanel) inside the actionPerformed() method. But either way I can't see the JFrame when I run it. Hope this helps.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are adding the imagePanel before you are creating it, and this cannot be done. It has nothing to do with GUI or creating GUI in events and everything to do with basic OOPs practice. Objects must be constructed before you can use them.

Also, on a side note: you never pack or setVisible(true) your JFrame.

Also, from a GUI standpoint, I'd recommend that you try using a CardLayout.

For instance:
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much. It worked. However, since I am a newbie to Java Swing, as well as programming, I want to ask you couple questions about your code. First of all, is there a particular reason that you declare IMAGE_PANEL, BUTTON_PANEL... to be static variables. I understand that declaring a variable to be static so that the variable is not attach to a particular objects but rather to a class as a whole. But I think in this case, that is not the reason. Second, can you explain a bit about the differences in setPreferredSize and setSize, I google those two, but the differences are unclear. One last question, If I want to keep the start button after I click it, how do I do it? I know CardLayout make it disappear, I try to implement BoxLayout to keep it, but the result was unsuccessful. Again, thank you, and if any of my question appear to be stupid, please forgive me
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thang Pham wrote:is there a particular reason that you declare IMAGE_PANEL, BUTTON_PANEL... to be static variables.


Those actually aren't variables. If you look closely you'll see that they are declared static final and are all-capitalized -- thus they are constants.

Second, can you explain a bit about the differences in setPreferredSize and setSize


Most layout managers will use the preferredSize value when deciding how to set a rendered component's size, not the size field, and so if you are using layout managers (and I suggest that you do so) it is the preferredSize field that you should set.

If I want to keep the start button after I click it, how do I do it? I know CardLayout make it disappear, I try to implement BoxLayout to keep it, but the result was unsuccessful.


If you want to preserve the start button, then don't have it held by a component that is swapped by the cardlayout. For instance you could have your main JPanel use a BorderLayout, have it hold a JPanel in the BorderLayout.SOUTH position that holds your start button, and hold another JPanel in the BorderLayout.CENTER position that uses a CardLayout that swaps JPanels. The possibilities here are endless.

Again, thank you, and if any of my question appear to be stupid, please forgive me


You're quite welcome. And most of us here believe that the only stupid question is the unasked question. Best of luck.
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If you want to preserve the start button, then don't have it held by a component that is swapped by the cardlayout. For instance you could have your main JPanel use a BorderLayout, have it hold a JPanel in the BorderLayout.SOUTH position that holds your start button, and hold another JPanel in the BorderLayout.CENTER position that uses a CardLayout that swaps JPanels.



I try to implement what you said above, but I could not get it to show up correctly. The image show up when I run the program. The image suppose to only show up after I click the start button

 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I figure it out. The first component that add to the cardlayout is visible when the container first display. So I can add a another image in, and when I implement cardlayout.next(), it will show the next picture.
 
Whatever. Here's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic