I would like to know if there is a way to generate a JLabel
or JTextField
in the position where you click, in JPanel
or JFrame
.
Is it possible?
I would like to know if there is a way to generate a JLabel
or JTextField
in the position where you click, in JPanel
or JFrame
.
Is it possible?
Yes, it is possible:
setLayout(null)
on target component. MouseListener
to the component. mouseClicked
event:
add
method. setBounds
. Here is a simple example I did:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TextFieldGeneratorTest {
private static int contador = 0;
public static void main(String[] args) {
//janela
final JFrame janela = new JFrame("Test JTextField Generation");
//posicionamento sem nenhum layout, isto é, absoluto
janela.getContentPane().setLayout(null);
//adiciona listener do mouse
janela.getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
//adiciona dinamicamente no clique
final JTextField textField = new JTextField("Caixa " + ++contador);
textField.setBounds(event.getX(), event.getY(), 200, 30);
janela.getContentPane().add(textField);
}
});
//exibe a janela
janela.setSize(600, 600);
janela.setVisible(true);
}
}