Kleopatra said:
the problem with your code is, that you don't follow the solution (issue
2 in my original answer) I outlined. In particular, your custom model
does call super.setSelectedIndex(..) unconditionally instead of doing so
only if no inputVerifier objected.
Actually, I am aware of that. I tried it the way you originally
suggested but found that I could ONLY leave the field by clicking on a
tab if the control has "27" entered in it. Since the shouldYieldFocus
method returns false in that case, I am unable to move from one
control to another on the same tab.
My test application, using your original suggestions, that illustrates
this problem is at the end of this posting.
thanks for your help
....
Krick
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Application1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanelTab1 = new JPanel();
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JPanel jPanelTab2 = new JPanel();
JPanel jPanel2 = new JPanel();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextFieldPopup = new JTextField();
public Application1() {
try { jbInit(); } catch(Exception e) { e.printStackTrace(); }
}
private void jbInit() throws Exception {
this.setSize(new Dimension(400, 300));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
jLabel1.setText("Type '27' here and then click Tab2");
jTextField1.setPreferredSize(new Dimension(50, 21));
jLabel2.setText("Type 27 here and then click in the textField
above");
jTextField2.setPreferredSize(new Dimension(50, 21));
// set a new model with a custom setSelectedIndexmethod
jTabbedPane1.setModel(new DefaultSingleSelectionModel() {
public void setSelectedIndex(int index) {
Frame frm = (Frame)SwingUtilities.windowForComponent(jTabbedPane1);
Component compWithFocus = KeyboardFocusManager.
getCurrentKeyboardFocusManager().
getFocusOwner();
if(compWithFocus != null) {
System.out.println("jTabbedPane1_setSelectedIndex, "+
"currentFocusOwner: " +
compWithFocus.getClass().getName());
}
int currSelectedTabIndex = getSelectedIndex();
if(currSelectedTabIndex >=0) {
JPanel currSelectedTabPanel = (JPanel)jTabbedPane1.
getComponentAt(currSelectedTabIndex);
if(currSelectedTabPanel != null) {
if(compWithFocus instanceof JComponent) {
JComponent comp = (JComponent)compWithFocus;
boolean isChild = isChild(currSelectedTabPanel, comp);
InputVerifier iv = comp.getInputVerifier();
if (isChild && iv!=null && !iv.shouldYieldFocus(comp)) {
// Pop up the message dialog.
String title = "Additional information is needed for
27";
JOptionPane.showConfirmDialog(frm, jTextFieldPopup,
title,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null);
}
}
}
}
super.setSelectedIndex(index);
}
});
// add an input verifier
jTextField1.setInputVerifier(new InputVerifier() {
public boolean verify(JComponent comp) {
return (!"27".equals(((JTextField)comp).getText()));
}
public boolean shouldYieldFocus(JComponent input) {
System.out.println("jTextField1_InputVerifier_shouldYieldFocus");
return (verify(input));
}
});
// add an input verifier
jTextField2.setInputVerifier(new InputVerifier() {
public boolean verify(JComponent comp) {
return (!"27".equals(((JTextField)comp).getText()));
}
public boolean shouldYieldFocus(JComponent input) {
System.out.println("jTextField2_InputVerifier_shouldYieldFocus");
return (verify(input));
}
});
// this focus listener is just here for diagnostic purposes
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(FocusEvent e) {
System.out.println("jTextField1_focusLost");
}
});
// this focus listener is just here for diagnostic purposes
jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(FocusEvent e) {
System.out.println("jTextField2_focusLost");
}
});
contentPane.add(jTabbedPane1, BorderLayout.CENTER);
jPanelTab1.add(jPanel1, null);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField1, null);
jPanelTab1.add(jPanel2, null);
jPanel2.add(jLabel2, null);
jPanel2.add(jTextField2, null);
jTabbedPane1.add(jPanelTab1, "Tab1");
jTabbedPane1.add(jPanelTab2, "Tab2");
}
public static boolean isChild(Container cont, Component comp) {
Component p = null; // parent
for (p=comp.getParent(); p!=null && p!=cont; p=p.getParent())
continue;
return (p != null);
}
public static void main(String[] args) {
new Application1().setVisible(true);
}
}