• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying an image on button press

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I was wondering if you could help me out. I have been reading some java guides here http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html and was trying to put a bit of what I have learnt into practice but am having some difficulty. Basically, using netbeans IDE I have created a new jFrameform so that I can place swing components in design mode. What I want to create isnt overly complicated but I just cant seem to get it. In design I have simply added a panel and a button. When I press the button I want to display an image I have located at: /resources/images/numbers/1.png. This is my code so far(most of it has been automatically generated from me adding things via design mode:



I assume I need something like below somewhere , do i need to create a draw method? if so how do I call it as it is expecting graphics2d as a parameter, what would I pass into it?



I would be very grateful for any help on this.

Thanks.

 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have realised that I probably need to create a label and give it the the image so I have tried to do this by adding the following code:

I am not getting any errors when I click the button but the image is also not being displayed
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get rid of the IDE. The IDE uses a GroupLayout.The GroupLayout is flexible but it is also complex to use and so it is generally used by IDE's that try to use a single layout for the entire GUI. Take a look at the code that was generated to add a component to a panel.

Now take a look at the code that you used the add the label to the panel:



Your code looks nothing like the code generated since you don't provide any constraints that tells the GroupLayout how to size/position the component.

I always suggest you build the GUI manually and learn how to use Swing and not the IDE.

You can start with the Swing tutorial on Layout Managers which explains how the layout managers work and contains example programs.

Also the general code when adding a component AFTER the GUI is visible is:

 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh excellent, thank you for the response. I do have another question in relation to this before I go and try and do my own GUI from code alone. Basically I have added 5 labels to the frame which are visible but I have created an initComponents2() method which set them to invisible as such:



I then have my code for when the button is pressed which is as follows:



As you have noticed I am duplicating code which im sure is not good practice. I suspect I shouldnt have to create the labelArray twice and populate it twice so I thought I should maybe create and populate it outside of any method but I just get errors cannot find symbol: symbol class labelArray and some extra stuff I can paste if required. My overall code is as follows:



I thought if I simply pasted the code



under the button declaration that the labelArray would be available to use in any of my methods etc hence I wouldnt have to create and populate it each time? Could someone please tell me what way this should be done or what my current issue is.

Thanks.



 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic