RenegadeAndy
RenegadeAndy

Reputation: 5690

Grouping swing objects

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

Upvotes: 1

Views: 2233

Answers (6)

aioobe
aioobe

Reputation: 420951

Create a class MyObj and let it extend JPanel. In the constructor of MyObj you call setLayout(new BorderLayout()) or whatever layout you prefer. Then do for instance add(pic, BorderLayout.NORTH); add(label1, BorderLayout.WEST); add(label2, BorderLayout.EAST);.

Then you should be able to do window.add(new MyObj(pic, label1, label2)).

import java.awt.*;

class MyObj extends JPanel {
    public MyComponent(ImageIcon pic, String label1, String label2) {
        setLayout(new BorderLayout());
        add(new JLabel(label1), BorderLayout.NORTH);
        add(new JLabel(pic), BorderLayout.CENTER);
        add(new JLabel(label2), BorderLayout.SOUTH);
    }
}

public class FrameTest {
    public static void main(String[] args) {
        JFrame jf = new JFrame("Demo");
        jf.add(new MyObj(new ImageIcon("duke.jpg"), "Label 1", "Label 2"));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }
}

Produces

enter image description here

Upvotes: 4

OscarRyz
OscarRyz

Reputation: 199215

Something like this ?

image with two labels http://img297.imageshack.us/img297/5223/capturadepantalla201005i.png

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 ));        

    }
}

Upvotes: 2

TacB0sS
TacB0sS

Reputation: 10266

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.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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.

Upvotes: 4

camickr
camickr

Reputation: 324098

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.

Upvotes: 1

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56595

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

JPanel panel = new JPanel(new SomeLayoutYouLike());
panel.add(..);
panel.add(..);

Upvotes: 1

Related Questions