Scrolling automatically when tabbing through textfield.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alain_Charbonneau
Posted On:   Thursday, June 13, 2002 06:27 AM

I have textfields on a panel and this panel is on a scrollpane. When I tab through the textfields, it doesn't scroll automatically, the cursor disappears on the invisible texfields at the bottom of the scrollpane. How can I make it scroll automatically?

Re: Scrolling automatically when tabbing through textfield.

Posted By:   Mike_Friedrich  
Posted On:   Friday, June 14, 2002 12:17 PM

Install a listener on focus-enter on every textfield and call scrollRectToVisible.


Here is an example:


public class ScrollTest extends javax.swing.JFrame {

public ScrollTest() {
initComponents();
}

private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jTextField6 = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
jTree4 = new javax.swing.JTree();
jLabel7 = new javax.swing.JLabel();
jTextField7 = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
jTextField8 = new javax.swing.JTextField();
jScrollPane4 = new javax.swing.JScrollPane();
jTree3 = new javax.swing.JTree();
jLabel9 = new javax.swing.JLabel();
jTextField9 = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
jTextField10 = new javax.swing.JTextField();
jLabel11 = new javax.swing.JLabel();
jTextField11 = new javax.swing.JTextField();
jScrollPane5 = new javax.swing.JScrollPane();
jTree2 = new javax.swing.JTree();
jLabel12 = new javax.swing.JLabel();
jTextField12 = new javax.swing.JTextField();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
jLabel1.setText("jLabel1");
jPanel1.add(jLabel1);
jTextField1.setText("jTextField1");
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField1);
jLabel2.setText("jLabel2");
jPanel1.add(jLabel2);
jTree1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jScrollPane2.setViewportView(jTree1);
jPanel1.add(jScrollPane2);
jTextField2.setText("jTextField2");
jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField2);
jLabel3.setText("jLabel3");
jPanel1.add(jLabel3);
jTextField3.setText("jTextField3");
jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField3);
jLabel4.setText("jLabel4");
jPanel1.add(jLabel4);
jTextField4.setText("jTextField4");
jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField4);
jLabel5.setText("jLabel5");
jPanel1.add(jLabel5);
jTextField5.setText("jTextField5");
jTextField5.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField5);
jLabel6.setText("jLabel6");
jPanel1.add(jLabel6);
jTextField6.setText("jTextField6");
jTextField6.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField6);
jTree4.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jScrollPane3.setViewportView(jTree4);
jPanel1.add(jScrollPane3);
jLabel7.setText("jLabel7");
jPanel1.add(jLabel7);
jTextField7.setText("jTextField7");
jTextField7.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField7);
jLabel8.setText("jLabel8");
jPanel1.add(jLabel8);
jTextField8.setText("jTextField8");
jTextField8.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField8);
jTree3.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jScrollPane4.setViewportView(jTree3);
jPanel1.add(jScrollPane4);
jLabel9.setText("jLabel9");
jPanel1.add(jLabel9);
jTextField9.setText("jTextField9");
jTextField9.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField9);
jLabel10.setText("jLabel10");
jPanel1.add(jLabel10);
jTextField10.setText("jTextField10");
jTextField10.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField10);
jLabel11.setText("jLabel11");
jPanel1.add(jLabel11);
jTextField11.setText("jTextField11");
jTextField11.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField11);
jTree2.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jScrollPane5.setViewportView(jTree2);
jPanel1.add(jScrollPane5);
jLabel12.setText("jLabel12");
jPanel1.add(jLabel12);
jTextField12.setText("jTextField12");
jTextField12.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jComponentFocusGained(evt);
}
});
jPanel1.add(jTextField12);
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(300, 200));
setLocation((screenSize.width-300)/2,(screenSize.height-200)/2);
}

private void jComponentFocusGained(java.awt.event.FocusEvent evt) {
// Add your handling code here:
jPanel1.scrollRectToVisible(evt.getComponent().getBounds());
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public static void main(String args[]) {
new ScrollTest().show();
}


private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTree jTree1;
private javax.swing.JTextField jTextField2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField jTextField6;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTree jTree4;
private javax.swing.JLabel jLabel7;
private javax.swing.JTextField jTextField7;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField jTextField8;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTree jTree3;
private javax.swing.JLabel jLabel9;
private javax.swing.JTextField jTextField9;
private javax.swing.JLabel jLabel10;
private javax.swing.JTextField jTextField10;
private javax.swing.JLabel jLabel11;
private javax.swing.JTextField jTextField11;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JTree jTree2;
private javax.swing.JLabel jLabel12;
private javax.swing.JTextField jTextField12;
}


Regards, Mike

About | Sitemap | Contact