aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Best Swing Component For Icons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Best Swing Component For Icons" Watch "Best Swing Component For Icons" New topic
Author

Best Swing Component For Icons

Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190

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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best Swing Component For Icons