Код | import javax.swing.*; import java.awt.*; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent;
/** * User: mgarin Date: 24.10.2010 Time: 19:33:45 */
public class ScrollToVisibleField extends JFrame { public ScrollToVisibleField () throws HeadlessException { super ();
final JPanel fields = new JPanel (); fields.setLayout ( new GridLayout ( 100, 1, 5, 5 ) ); getContentPane ().add ( new JScrollPane ( fields ), BorderLayout.CENTER );
for ( int i = 0; i < 100; i++ ) { fields.add ( new JTextField( "" + i ) { { addFocusListener ( new FocusAdapter() { public void focusGained ( FocusEvent e ) { fields.scrollRectToVisible ( e.getComponent ().getBounds () ); } } ); }} ); }
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); setSize ( 100, 500 ); setLocationRelativeTo ( null ); setVisible ( true ); }
public static void main ( String args[] ) { new ScrollToVisibleField (); } } |
|