vishesh
vishesh

Reputation: 41

How to create an array of JLabels in Java to be printed to a JFrame

I m trying to make an array of labels. Each label has a differented value which come out of a function. I don't know the exact number of labels to be used. I mean there could be any number of values to be printed. Please help me do this.

Upvotes: 4

Views: 48229

Answers (5)

Dr. Varun Tiwari
Dr. Varun Tiwari

Reputation: 1

import java.awt.*;
public class frame4array extends Frame
{
Checkbox c1[]; /* Same as checkbox and TextField now you add an array of Label in 
Frame and Applet also and if you want to create an array of swing components now write 
same as JTextField jt[];*/
TextField t1[];
int i;
frame4array(String p)
{
super(p);
c1=new Checkbox[2];
t1=new TextField[2];
for(i=0;i<2;i++)
{
t1[0]=new TextField();
t1[0].setBounds(200, 50, 150, 30);
t1[1]=new TextField();
t1[1].setBounds(200, 80, 150, 30);
c1[0]=new Checkbox("Singing");
c1[0].setBackground(Color.red);
c1[0].setBounds(430,200,120,40);
c1[1]=new Checkbox("Cricket",true);
}
for(i=0;i<2;i++)
{
add(t1[i]);
add(c1[i]);
}
setFont(new Font("Arial",Font.ITALIC,40));
}
public static void main(String s[])
{
frame4array f1=new frame4array("hello");
f1.setSize(600,500);
f1.setVisible(true);
}

}
/* run and enjoy */

Upvotes: 0

Peter Lang
Peter Lang

Reputation: 55524

If possible, don't use separate JLabels, but a JList, which will take care of layout and scrolling if necessary.

Java-Tutorial - How to us a List:

alt text
(source: sun.com)

Upvotes: 2

Vatsal Srivastava
Vatsal Srivastava

Reputation: 1

You can actually make an array of any Swing Component, since every Swing Component is basically composite data type. Try this:

javax.swing.JTextField[] array = new javax.swing.JTextField[number_of_elements];

Upvotes: 0

fasseg
fasseg

Reputation: 17761

easy just have one method return an array or some collection of JLabels and add all of them to your JComponent (e.g. a JPanel)

class MyPanel extends JPanel{

    public MyPanel(){
        super();
        showGUI();
    }

    private JLabel[] createLabels(){
        JLabel[] labels=new JLabel[10]
        for (int i=0;i<10;i++){
            labels[i]=new JLabel("message" + i);
        }
        return labels;
    }

    private void showGUI(){
        JLabel[] labels=createLabels();
        for (int i=0;i<labels.length();i++){
            this.add(labels[i]);
        }
    }
}

Upvotes: 7

Riduidel
Riduidel

Reputation: 22292

Are you kiddin ? Well, in case you're serious, first take a look at some of the Java APIs, like JLabel, JPanel, and some of the language elements.

Then you'll be able to do something like (I'm sure my code won't compile)

public static JPanel getLabels(int count) {
    JPanel panel = new JPanel(new FlowLayout());
    for(int i =0; i<count; i++) {
        panel.add(new JLabel(theFunctionThatCannotBeNamedHere(i)));
    }
    return panel;
}

Notice that theFunctionThatCannotBeNamedHere is the function you talked about.

Upvotes: 1

Related Questions