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

    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);
            out.setEditable(false);
            plus = new JButton("+");
            JPanel panel = new JPanel();
            JPanel panel2 = new JPanel();
            add(panel, BorderLayout.CENTER);
            add(panel2, BorderLayout.SOUTH);
            plus.addActionListener(new ButtonHandler(this));
            panel.add(int1);
            panel.add(int2);
            panel.add(out);
            panel2.add(plus);
        }
    }
    
    class ButtonHandler implements ActionListener{
        Calc theApplet;
        ButtonHandler(Calc app){
            theApplet = app;
        }
    
        public void actionPerformed(ActionEvent e) {
            try{
                int integer1 = Integer.parseInt(theApplet.int1.getText());
                int integer2 = Integer.parseInt(theApplet.int2.getText());
                if(e.getSource()==theApplet.plus)
                    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 Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: JTextFields with labels

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

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

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
  •