swing - Is this Correct way to add FocusListener to JTextFields in java? -


I found hundreds of code in my application in Java and JTextFields and I want to add FocusListener The horizontal alignment set of text on all of these and add Focus listener to each of these textfields, so, I made this method and this is working great but I only Want to know whether this is the right way or there is something wrong with it or I have some rules of OAP I'm going against not?

Here is the code

  Public Zero CreateFocusListenerForFields (JTextField txt) {txt.setHorizontalAlignment (JTextField.RIGHT); Txt.addFocusListener (New Focus Lister) {@Override Public Zero Focus (FocusEvent E) {} @ Override Public Zero FocusStatus (Focus Event E) {if (number number (txt.getText ()) {txt.setBackground (new Color (254,157,157)); txt.requestFocus ();} and {txt.setBackground (Color.white);}}}); }  

and apply this method to my textfield

  CreateFocusListenerForFields (MyTextField);  

Now when I run the code, it works great just wants to know whether it is correct and if not, there are other ways to do alignment and focus list on hundreds of fields. ? Thank you for your kind advice.

Again, my bias focus is to use an input input instead of listener, if only because of the input Werner produces a high level, and it always seems safe to use in place of low level (close to metal) where applicable.

An example of both:

  import java.awt.Color; Import java.awt.event.FocusAdapter; Import java.awt.event.FocusEvent; Import javax.swing *; Import javax.swing.text.JTextComponent; Public class testfield verification {public static last color ERROR_COLOR = new color (254,157,157); Private stable final end COLS = 8; Private Jeppel homepayel = new zpn (); Private JTextField verifiedField = new JTextField (COLS); Private Jetextuffed Focus Checked Field = New Jetextuff (COL); Private color default background = faucet; Public TestField Verification () {verifiedField.setInputVerifier (New MyVerfier (this)); Focus Focusfield. Adfocalsister (new My Focus Test (this)); MainPanel.add (new JLabel ("InputWifeFair:")); MainPanel.add (verifiedField); MainPanel.add (new JLabel (with "Focus Listener:")); MainPanel.add (focusCheckedField); } Public Boolean verified text (string text) {try {Integer.parseInt (text); Back true; } Hold (Number Format Expected NFE) {return false; }} Public Zero setFieldBackground (JComponent component, Boolean verified) {if (defaultBackground == null) {defaultBackground = component.getBackground (); } Color bg = verified? Default background: ERROR_COLOR; Component.setBackground (bg); } Public JComponent getMainPanel () {mainPanel Return; } Make Private Static Voices Andshoes () {Jeffre Frame = New Gefram ("Maververfire"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.getContentPane () Add (New TestFieldVerification (.) GetMainPanel ()); Frame.pack (); Frame.setLocationRelativeTo (zero); Frame.setVisible (true); } Public static zero main (string [] Args) {SwingUtilities.invokeLater (new runnab) (Public Zero Run) {createAndShowGui ();}}); }} Class MyVerfier InputVerifier {Personal TestFieldVerification GUI; Public Merger (TestField Verification) {this.gui = GUI; } @ Override Public Boolean should have Yield Focus (JComponent Input) {GUI.setFieldBackground (input, super. Shieldfile (input)); Return super Shield Yield Focus; } @ Override Public Boolean Validation (JComponent Input) {String Text = (JTextComponent Input) .getText (); Return GUI.verifyText (text); }} Class MyFocusCheck FocusAdapter {Personal TestFieldVerification GUI; Public MyFocusCheck (TestFieldVerification GUI) {this.gui = GUI; } @ Override Public Zero Focus Lole (Focus Event E) {JTextComponent textComp = (JTextComponent) e.getSource (); String text = text comp. Gate text (); Boolean verified = GUI.verifyText (text); Gui.setFieldBackground (text comp, verified); If (! Verified) {textComp.requestFocusInWindow (); }}}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -