Results 1 to 3 of 3
- 04-05-2012, 01:37 PM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
How to set foucs on first label after a user press cancel
Hi,
I am trying to set foucs on a label in the following code; i am able to set it when the user enters the values first time; i also want to set focus on the same label after the user presses cancel
Output:-Java Code:/*<applet code = "sumApplet.class" width="320" height="250"></applet>*/ public class sumApplet extends java.applet.Applet implements java.awt.event.ActionListener, java.awt.event.FocusListener { java.awt.Label firstNum, secondNum, result; java.awt.TextField f, s, r; java.awt.Button sum, cancel; public sumApplet() { setLayout(null); firstNum = new java.awt.Label("First Number"); secondNum = new java.awt.Label("Second Number"); result = new java.awt.Label("result"); f = new java.awt.TextField(); s = new java.awt.TextField(); r = new java.awt.TextField(); r.setEditable(false); sum = new java.awt.Button("Sum"); cancel = new java.awt.Button("Cancel"); add(firstNum); firstNum.setBounds(10, 30, 60, 20); add(f); f.setBounds(90, 30, 20, 20); add(secondNum); secondNum.setBounds(10, 70, 80, 20); add(s); s.setBounds(100, 70, 20, 20); add(result); result.setBounds(10, 100, 60, 20); add(r); r.setBounds(90,100,40,20); add(sum); sum.setBounds(10, 140, 40, 30); add(cancel); cancel.setBounds(60, 140, 60, 30); sum.addActionListener(this); cancel.addActionListener(this); f.addFocusListener(this); } public void actionPerformed(java.awt.event.ActionEvent e) { java.awt.Button b = (java.awt.Button) e.getSource(); try { if(b.getLabel().equals("Sum")) { String s1 = f.getText(); String s2 = s.getText(); int sum = Integer.parseInt(s1) + Integer.parseInt(s2); String toResult = " "+ sum; r.setText(toResult); } else { f.setText(null); s.setText(null); r.setText(null); } } catch(java.lang.NumberFormatException nfe) { System.out.println("NumberFormatException" + " " + nfe.getLocalizedMessage()); } } public void focusGained(java.awt.event.FocusEvent fg) { f.setFocusable(isActive()); } public void focusLost(java.awt.event.FocusEvent fl) { //f.lostFocus(e, what) } }

Please help.
Thanks in advance
- 04-05-2012, 02:19 PM #2
Re: How to set foucs on first label after a user press cancel
Moved from New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 02:23 PM #3
Re: How to set foucs on first label after a user press cancel
Read through the methods of JLabel, including the methods it inherits from superclasses, looking for any methods that might request the focus. Read the API carefully so you make the right choice -- don't use methods whose use is discouraged.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Cancel button for exiting app
By phearless in forum AWT / SwingReplies: 1Last Post: 01-13-2011, 07:20 PM -
Struts 2 cancel button
By kirtichopra2003 in forum Advanced JavaReplies: 0Last Post: 09-29-2009, 03:40 PM -
Cancel Particular scheduled task
By hjava in forum New To JavaReplies: 0Last Post: 08-11-2009, 11:10 PM -
How should I cancel this thread ?
By playwin2 in forum Threads and SynchronizationReplies: 7Last Post: 08-27-2008, 06:20 PM -
Press any key to continue/press enter
By JT4NK3D in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks