Duplicate :
|
|
Relates :
|
|
Relates :
|
Hardware Platform : Intel Pentium II PC OS Version : Window NT4.0 SP 5 JDK: 1.3 RC1 Various cases of confused focus are found in programs calling JTextField requestFocus method and popping modal dialogs. A simple example that demonstrates multiple cursors is attached at the end. - Launch app and without filling anything in the 3 JTextFields, click Ok button and 3 separate JOptionPane dialogs will be shown. Each JOptionPane actually requestFocus() for the respective JTextFields. - Once over, notice that all 3 fields have cursors and worse still, I can only enter data into the 3rd field. The 1st two fields no longer response to my mouse clicks, tabulation and keyboard inputs. Note: In the source codes, if requestFocus() is called before the JOptionPane, the multiple cursors behave differently in that only the middle JTextField does not responds. -------------------- TestCursor.java ------------------------------------ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestCursor extends JPanel { public JTextField field1; public JTextField field2; public JTextField field3; public TestCursor() { field1 = new JTextField( 10 ); field2 = new JTextField( 10 ); field3 = new JTextField( 10 ); add( field1 ); add( field2 ); add( field3 ); JButton btn = new JButton( "Ok" ); btn.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { if( field1.getText().trim().length() == 0 ) { showError( "Field1 is mandatory" ); field1.requestFocus(); } if( field2.getText().trim().length() == 0 ) { showError( "Field2 is mandatory" ); field2.requestFocus(); } if( field3.getText().trim().length() == 0 ) { showError( "Field3 is mandatory" ); field3.requestFocus(); } } }); add( btn ); } public void showError( String message ) { JOptionPane.showMessageDialog( this, message ); } public static void main( String[] args ) { JFrame frame = new JFrame(); frame.setSize( new Dimension( 500, 100 ) ); frame.getContentPane().add( new TestCursor() ); frame.show(); } } Name: ks88420 Date: 08/30/2000 java version "1.3.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C) Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode) In a JFrame splitted by a JSplitPane, when the second panel is shown after two dialogs in response to the first panel's action and one of the components (say, BB) on the second panel request focus, the previous component on the first panel that had the focus still "keeps" the focus (say, AA), and BB has another focus. Dual focuses appear in the same window/frame. However, the focus on AA does not respond to any event, and the actual focus has moved to BB. This weird behavior can be reproduced by running the attached testing program. Steps: 1) Compile and run it. 2) In the textfield of the first panel (AA), type in something, and then press "Return" key or click on the "Search" button. 3) A message box is shown. Choose "Override". 4) Another message box is shown. Again, choose "Override". 5) The second panel is shown Now you can see two focuses appear in this window. Notes: 1) For simplism, Here I use the same class for the first and second panel. They can be different. 2) Even if I disable all the components in the first panel (restore the two line from comments), the problem still appear. 3) The problem does not occur when this is only one message box before the second panel is shown. I noticed that the focus is reset onto AA right after the first message box is released (in the windowActivated event). However, why showing one message box or dialog does not bring up this problem, but showing two does? 4) After the problem occurs, the actual focus cannot return to the first panel. The program I used: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class FocusTest extends JFrame { JScrollPane workPane; static PSearch panel0, panel1; JSplitPane split_V1; JPanel emptyPane; public FocusTest() { super("Focus Test"); workPane=new JScrollPane(); panel0=new PSearch(); emptyPane=new JPanel(); JLabel lblEmpty=new JLabel("Empty Pane"); lblEmpty.setFont(new Font("SansSerif",Font.BOLD,18)); emptyPane.add(lblEmpty); split_V1=new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel0,emptyPane); getContentPane().add(split_V1, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if(JOptionPane.showConfirmDialog(null, "Do you really want to exit?", "Focus Test", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION) { System.exit(0); } } public void windowActivated(WindowEvent we) { FocusTest.panel0.txtSearch.requestFocus(); } public void windowOpened(WindowEvent we) { FocusTest.panel0.txtSearch.requestFocus(); } }); } public static void main(String s[]) { FocusTest window = new FocusTest(); window.setSize(450, 450); window.setVisible(true); } protected class PSearch extends JPanel implements ActionListener { protected JTextField txtSearch=new JTextField(10); protected JLabel lblSearch=new JLabel("Enter search string: "); protected JButton btnSearch=new JButton("Search"); public PSearch() { btnSearch.setActionCommand("Search"); btnSearch.setMnemonic(KeyEvent.VK_S); lblSearch.setLabelFor(btnSearch); btnSearch.addActionListener(this); txtSearch.setActionCommand("Search"); txtSearch.addActionListener(this); txtSearch.addFocusListener(new FocusAdapter(){ public void focusGained(FocusEvent e){ txtSearch.selectAll(); } }); //Layout the components add(lblSearch); add(txtSearch); add(btnSearch); } public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("Search") && FocusTest.panel1==null) { this.txtSearch.requestFocus(); DlgException ex=new DlgException("This is a test message!"); if (ex.show()==JOptionPane.YES_OPTION) { //Do something here, then show another messagebox or dialog. ex=new DlgException("Another message..."); ex.show(); //Disable the components in panel0 does not help. //for(int i=0;i<this.getComponentCount ();i++) // this.getComponent(i).setEnabled (false); FocusTest.panel1=new PSearch(); split_V1.setBottomComponent (FocusTest.panel1); split_V1.repaint(); FocusTest.panel1.txtSearch.requestFocus (); } } } } protected class DlgException { private String msg=null; private String[] options={"Override", "Cancel"}; public DlgException(String msg) { this.msg =msg; } public int show(){ return JOptionPane.showOptionDialog(null, msg, "Exception message", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null ); } } } (Review ID: 108724) ======================================================================
|