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.
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.