University of Cincinnati logo and link  
Swing Components in Java
 
  UC ingot 
  • Labels simply hold and display text.  They do not accept user input.
    • You often use JLabels with text fields, because text fields do not automatically come with labels.
    • Many people use a JTextField as a label and simply disable it so that users cannot enter information.  This is not good practice.  Users might think that they have to do something to enable the JTextField so that they can change the value!  If you are going to leave a text field disabled,  use a JLabel instead.
    • You can change the text of a label programmatically with the setText(String s) method.  You just can't allow the user to do so.  You might want to change the text on a status label, for example.
  • Constructing a label is pretty simple.

  • JLabel lblStatus = new JLabel("Off");
    pnlStatus.add(lblStatus);
  • As of J2SE 1.3, you can even use HTML, as long as you surround the text with <HTML> tags.