Results 1 to 2 of 2
Thread: JTextFields with labels
- 10-24-2012, 10:06 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 13
- Rep Power
- 0
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.
- 10-25-2012, 01:41 AM #2
Re: JTextFields with labels
camickr's Text Prompt « Java Tips Weblog was written to address this very need.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
JTextFields not updating
By daxserver in forum AWT / SwingReplies: 7Last Post: 05-28-2012, 10:38 PM -
JTextFields
By wired-in=p in forum New To JavaReplies: 1Last Post: 01-30-2012, 12:14 AM -
Problems Using JTextFields
By jrJava in forum New To JavaReplies: 2Last Post: 02-03-2011, 01:55 AM -
Problem with JTextFields not null
By romina in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:17 AM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks