• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Best Swing Component For Icons  RSS feed

 
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know what is the best Swing component to arrange and hold Icons in the GIF file format. Would it be better to subclass the JComponent class and use a FlowLayout, or use JTextPane directly? Thanks.
 
Rancher
Posts: 2958
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you haven't given much of a description of your requirement.

"arrange" doesn't tell us HOW you want the icons displayed. Do you want them horizontally, vertically, diagonally, stacked...?

"Should I use FlowLayout or JTextPane", is not a requirement.

A FlowLayout is used to layout components and a JTextPane is used to display text. So I don't know how you expect us to provide an reasonable answer.

If I had to make a wild guess I would use a JList that wraps. Read the Swing tutorial for more information.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know what is the best Swing component to arrange and hold Icons in the GIF file format.
There are two general ways to show images:
1 – put the image in an ImageIcon and add it to a JLabel and add the JLabel to your container. This is easy and care–free; you let java do all the background work. You only have to deal with layout managers to get the layout you want.
2 – render/draw the image inside the overridden paintComponent method of an extension of JComponent or JPanel. Then you add this graphic component to your container. This can be fairly easy but allows much more room for creative display. One area that bears mention is the sizing of the graphic component. The parent layout manager will want to know its size which, since it contains no children, will be reported as either (0,0) or a default minimum, (10,10) for JPanel. So you provide a size hint in one of two ways:
1 — call the setPreferredSize method on the component
2 — override the getPreferredsize method to return the size you want; often the size of the image.

Depending on how you choose to load your images java will support gif, jpg and png files. ImageIO will load these plus, in j2se 1.5+, bmp and wbmp file extensions.

Would it be better to subclass the JComponent class and use a FlowLayout, or use JTextPane directly
JTextPane is generally for embedding components in text.
If you want a component, vis–a–vis graphic, approach try a JPanel with suitable layout manager (depending on what you want) and add JLabels with ImageIcons.

Some helpful resource links for more information:
How to Use Icons
Lesson: Working with Images
Lesson: Performing Custom Painting
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!