1 42 43 package org.jfree.ui; 44 45 import java.awt.event.FocusEvent ; 46 import java.awt.event.FocusListener ; 47 48 import javax.swing.text.JTextComponent ; 49 50 55 public final class JTextObserver implements FocusListener { 56 57 58 private static JTextObserver singleton; 59 60 63 private JTextObserver() { 64 } 66 67 72 public static JTextObserver getInstance() { 73 if (singleton == null) { 74 singleton = new JTextObserver(); 75 } 76 return singleton; 77 } 78 79 84 public void focusGained(final FocusEvent e) { 85 if (e.getSource() instanceof JTextComponent ) { 86 final JTextComponent tex = (JTextComponent ) e.getSource(); 87 tex.selectAll(); 88 } 89 } 90 91 96 public void focusLost(final FocusEvent e) { 97 if (e.getSource() instanceof JTextComponent ) { 98 final JTextComponent tex = (JTextComponent ) e.getSource(); 99 tex.select(0, 0); 100 } 101 } 102 103 108 public static void addTextComponent(final JTextComponent t) { 109 if (singleton == null) { 110 singleton = new JTextObserver(); 111 } 112 t.addFocusListener(singleton); 113 } 114 115 120 public static void removeTextComponent(final JTextComponent t) { 121 if (singleton == null) { 122 singleton = new JTextObserver(); 123 } 124 t.removeFocusListener(singleton); 125 } 126 127 } 128 | Popular Tags |