• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea, Focus

 
Asif Lodhi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks

I slightly modified the focus-traversal code presented in Java Tutorial (http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html) to replace the CTRL+TAB combination with SHIFT-ENTER (because it is easier to type) to move out of JTextArea but the code does not work. The code in the tutorial works for just ENTER key only. Am I making any mistake? Can anyone help me on this? The GUI layout code was mostly generated by NetBeans 4.0.

I can make shift by writing event handlers and key trappers but that would be a rather un-clean way of doing things. If focus traversal can be handled using the new Focus Traversal APIs JDK 1.4 upwards then it should be handled the same way for all controls. Does anyone have anything to say on this? I would appreciate your feedback. I don't understand why the same code doesn't work for JTextArea if I just want to replace "CTRL+TAB" with "SHIFT+ENTER".

Following is the code (between hyphenated lines):
//============================================================================= ============================================
package javaapplication2;
import java.util.Set;
import java.util.HashSet;
import javax.swing.KeyStroke;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.text.MaskFormatter;

public class personAddForm2 extends javax.swing.JFrame {

public personAddForm2() {
Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);

Set downCycleKeys = getFocusTraversalKeys(KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
Set newdownCycleKeys = new HashSet(downCycleKeys);
newdownCycleKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.SHIFT_ DOWN_MASK));
setFocusTraversalKeys(KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS, newdownCycleKeys);

initComponents();
}

private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

lblName = new javax.swing.JLabel();
lblAddress = new javax.swing.JLabel();
jxtAddress = new javax.swing.JTextArea();
lblPhone = new javax.swing.JLabel();
txtPhone = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnClose = new javax.swing.JButton();
txtName = new javax.swing.JFormattedTextField();

getContentPane().setLayout(new java.awt.GridBagLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Test of New I/O");
setAlwaysOnTop(true);
setFont(new java.awt.Font("Arial", 0, 10));
lblName.setText("Name");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(13, 20, 0, 0);
getContentPane().add(lblName, gridBagConstraints);

lblAddress.setText("Address");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(5, 20, 0, 0);
getContentPane().add(lblAddress, gridBagConstraints);

jxtAddress.setColumns(35);
jxtAddress.setRows(4);
jxtAddress.setAutoscrolls(false);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.gridheight = 2;
gridBagConstraints.ipadx = 280;
gridBagConstraints.ipady = 54;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 26);

getContentPane().add(jxtAddress, gridBagConstraints);

lblPhone.setText("Phone");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(8, 20, 0, 0);
getContentPane().add(lblPhone, gridBagConstraints);

txtPhone.setColumns(15);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 4;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 2;
gridBagConstraints.ipadx = 134;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(8, 12, 0, 0);
getContentPane().add(txtPhone, gridBagConstraints);

btnSave.setMnemonic('S');
btnSave.setText("Save");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 6;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 12, 12, 0);
getContentPane().add(btnSave, gridBagConstraints);

btnClose.setMnemonic('C');
btnClose.setText("Close");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 6;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 21, 12, 0);
getContentPane().add(btnClose, gridBagConstraints);

txtName.setColumns(50);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.gridheight = 2;
gridBagConstraints.ipadx = 274;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 12, 0, 26);
getContentPane().add(txtName, gridBagConstraints);

pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new personAddForm2().setVisible(true);
}
});
}

private javax.swing.JButton btnClose;
private javax.swing.JButton btnSave;
private javax.swing.JTextArea jxtAddress;
private javax.swing.JLabel lblAddress;
private javax.swing.JLabel lblName;
private javax.swing.JLabel lblPhone;
private javax.swing.JFormattedTextField txtName;
private javax.swing.JTextField txtPhone;

}
//============================================================================= ============================================


Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!