tags:

views:

58

answers:

6

Hey.

I want to make an object I can add to my java swing application.

The object when instantiated would contain an image and 2 labels - is there a way to do this using java swing?

If there is - can you point me at an example.

I.e i want

Myobj icon = new MyObj(pic, label , label);

window.addComponent(icon);

Cheers

Andy

+3  A: 
aioobe
+1  A: 

You can add multiple Swing components to some container component - usually JPanel:

JPanel panel = new JPanel(new SomeLayoutYouLike());
panel.add(..);
panel.add(..);
Bozhidar Batsov
So would i do class MyObj extends Jpanel??
RenegadeAndy
You can subclass it if you truly need to have an object of your class, but in most of the cases one can you the panel instance directly.
Bozhidar Batsov
+1  A: 

Read the section from the Swing tutorial on Using Layout Managers. Use the appropriate layout manager to layout the components as you wish. Then add the compnents to a JPanel.

camickr
+4  A: 

This would typically be done by sublcassing JPanel and, in the constructor creating 3 labels (1 for the image) and adding them to the panel using a suitable layout manager.

Michael Borgwardt
A: 

Hey,

well, the main point of swing is to avoid instantiate your objects with parameters...

for example: (rather not do that unless this vars are imperative to the creation of the object)

MyFrame(Object o1, Object o2...)

for serialization purposes, you would rather use an empty constructor, and to set the external values form out side of the frame(in this case), this way you would never get things mixed up... and avoid much NullPointerException debugging, later on if you would use serialization.

if you want to design components, you should use NetBeans, very simple, very user friendly, allows you to align and locate you labels, as for the ImagePanel.. I had one but I converted it to a scaling image panel.. with scaled layers over it.

If you need, I'll post it here.

Hope this helps,

Adam.

TacB0sS
+2  A: 

Something like this ?

image with two labels

I created a subclass of JPanel and in its constructor I layout the components so it can be used exactly as you thought:

ImageAndLabels demo = new ImageAndLabels("image.png", "labelOne", "labelTwo");
window.add( demo );

Here's the complete source code for this window. May help you to get started.

import javax.swing.*;
import java.awt.Font;
public class ImageAndLabels extends JPanel {
    public static void main( String [] args ) {
        JFrame frame = new JFrame("image and labels");

        frame.add( new ImageAndLabels("./logo.png", // logo
           "Grouping swing objects",                // label 1
           "<html>Hey.<br>"                         // label 2
           +"I want to make an object I can add to my java swing application.<br>"
           +"The object when instantiated would contain an image and 2 labels - "
           +"is there a way to do this using java swing?</html>") );

        frame.pack();
        frame.setVisible( true );
    }


    public ImageAndLabels( String imageURL, String textOne, String textTwo ) {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add( new JLabel( new ImageIcon(imageURL )));
        add( new JLabel( textOne ){{
            setFont( new Font("Arial",  Font.BOLD, 20));
        }});
        add( new JLabel( textTwo ));        

    }
}
OscarRyz