Results 1 to 2 of 2
  1. #1
    Java Junior is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default JTextFields with labels

    Hello all, Can anyone tell me how I can include a label for my JTextBoxes, so for example inside the first textbox it says "Enter first number" then when I click inside the box the text disapears? Or if that is too long to do then how about a little label obove the textbox saying "first number" then obove the second box saying "second number" or something along those lines :)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Calc extends JApplet {
        JTextField int1, int2, out;
        JButton plus;
        public void init(){
            int1 = new JTextField(8);
            int2 = new JTextField(8);
            out = new JTextField(15);
            plus = new JButton("+");
            JPanel panel = new JPanel();
            JPanel panel2 = new JPanel();
            add(panel, BorderLayout.CENTER);
            add(panel2, BorderLayout.SOUTH);
            plus.addActionListener(new ButtonHandler(this));
    class ButtonHandler implements ActionListener{
        Calc theApplet;
        ButtonHandler(Calc app){
            theApplet = app;
        public void actionPerformed(ActionEvent e) {
                int integer1 = Integer.parseInt(theApplet.int1.getText());
                int integer2 = Integer.parseInt(theApplet.int2.getText());
                    theApplet.out.setText(String.valueOf(integer1 + integer2));
        }catch(NumberFormatException invalid){
                theApplet.out.setText("Must use numbers");
    Last edited by Java Junior; 10-24-2012 at 11:17 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: JTextFields with labels

    camickr's Text Prompt Java Tips Weblog was written to address this very need.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JTextFields not updating
    By daxserver in forum AWT / Swing
    Replies: 7
    Last Post: 05-28-2012, 10:38 PM
  2. JTextFields
    By wired-in=p in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 12:14 AM
  3. Problems Using JTextFields
    By jrJava in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 01:55 AM
  4. Problem with JTextFields not null
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:17 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts