Search code examples
javajlabeljtextfield

It's possible to add a JTextField to a JLabel?


So,I have a JLabel and a JTextField,it's there a possible way to add the JTextField to JLabel,can you give me some examples? I know there are lots of alternatives but I want to do it this way.

JTextField text=new JTextField();
text.setText("this is textfield");
text.setBounds(20,0,100,20);
//now the jlabel
JLabel label=new JLabel();
label.setOpaque(true);
label.setBackground(Color.blue);
label.add(text);//this is what I tryed

Solution

  • I would suggest a JPanel instead of a JLabel. If you are up to create a GUI it is the easiest way, to cut everything into smaller panels you can finaly join.

    JTextField text=new JTextField();
    text.setText("this is textfield");
    text.setBounds(20,0,100,20);
    JPanel panel=new JPanel();
    panel.addLayout(new FlowLayout());
    panel.setBackground(Color.blue);
    panel.add(text);
    

    this should work fine I hope