Dynamically Create Jlabels

Asked

Viewed 1,191 times

1

I have to create, from a click of a button, a JLabel dynamically. So that these honeys are organized in an order, assigning a name to that JLabel.

The user can create as many Abels as he wants...

How do I do it?

In my action, have it:

String name = "cor";

    int x = 0;
    int y = 0;
    JLabel label = new JLabel();
    label.setName(name);
    label.setBounds(x, y, 150, 150);

But it doesn’t add. Where I’m going wrong?

  • JLabel label = new JLabel();. Then just set a position, a size, a text and add the in the parent control.

  • how would it look in the code? this would all be inside the add button?

  • Where you want, whether you will be in the button action or create a method you choose. A question: you are using some layout?

  • JLabel label = new JLabel(text); 
 label.setName(name); 
 label.setBounds(x, y, 50, 50); ?

  • 1

    missing add to the frame in question, teuPanel.add(label); something like this, if you’re adding in real time, you may need to update the frame, teuPanel.revalidate(); teuPanel.repaint(); teuPanel.pack();

1 answer

2


Well, I managed to do what follows. By clicking the button, Abels are added to the screen.

import java.swing.List;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CriaBotoes {

    private final JFrame jf;
    private final List<JLabel> lista;

    public static void main(String[] args) {
        EventQueue.invokeLater(CriaBotoes::new);
    }

    public CriaBotoes() {
        jf = new JFrame("Teste");
        jf.setBounds(10, 10, 700, 700);
        jf.setLayout(null);

        JButton bt = new JButton("Novo label");
        jf.add(bt);
        bt.setBounds(10, 10, 100, 30);

        lista = new ArrayList<>();

        bt.addActionListener(e -> adicionarLabel());
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setVisible(true);
    }

    public void adicionarLabel() {
        int n = lista.size() + 1;
        String name = "cor " + n;
        JLabel label = new JLabel(name);
        jf.add(label);
        label.setBounds(10, n * 20 + 20, 150, 20);
        lista.add(label);
    }
}
  • Victor this doubt is precisely to be applied in this question: http://answall.com/questions/138609/simular-function-ofa processed

  • [off-topic] @Victor Stafusa these expressions ( CriaBotoes::new e -> adicionarLabel()) belong to the right Java 8!?

  • @jsantos1991 yes, it’s a lambda expression, from java 8.

  • @jsantos1991 The first is a case of method Reference (or to be more exact constructor Reference) and the second is a lambda. Yes, these are new Java 8 expressions. The equivalent in Java 7 would be new Runnable() { @Override public void run() { new CriaBotoes(); }} and new ActionListener() { @Override public void actionPerformed(ActionEvent e) { adicionarLabel(); }} - As it turns out, these new features of language make it much less verbose.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.