1 19 20 package examples.texteditor; 21 22 24 public class Finder extends javax.swing.JDialog { 25 26 29 public Finder(java.awt.Frame parent, javax.swing.JTextArea textEditor) { 30 super(parent, true); 31 this.textEditor = textEditor; 32 initComponents(); 33 pack(); 34 setLocationRelativeTo(parent); 35 findField.requestFocus(); 36 } 37 38 43 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 45 46 findPanel = new javax.swing.JPanel (); 47 findLabel = new javax.swing.JLabel (); 48 findField = new javax.swing.JTextField (); 49 buttonPanel = new javax.swing.JPanel (); 50 findButton = new javax.swing.JButton (); 51 closeButton = new javax.swing.JButton (); 52 53 getContentPane().setLayout(new java.awt.GridBagLayout ()); 54 55 setTitle("Find"); 56 addWindowListener(new java.awt.event.WindowAdapter () { 57 public void windowClosing(java.awt.event.WindowEvent evt) { 58 closeDialog(evt); 59 } 60 }); 61 62 getAccessibleContext().setAccessibleName("Find Dialog"); 63 getAccessibleContext().setAccessibleDescription("Find dialog."); 64 findPanel.setLayout(new java.awt.GridBagLayout ()); 65 66 findLabel.setLabelFor(findField); 67 findLabel.setText("Find text:"); 68 findPanel.add(findLabel, new java.awt.GridBagConstraints ()); 69 findLabel.getAccessibleContext().setAccessibleDescription("Find text."); 70 71 findField.addActionListener(new java.awt.event.ActionListener () { 72 public void actionPerformed(java.awt.event.ActionEvent evt) { 73 findFieldActionPerformed(evt); 74 } 75 }); 76 77 gridBagConstraints = new java.awt.GridBagConstraints (); 78 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 79 gridBagConstraints.weightx = 1.0; 80 gridBagConstraints.insets = new java.awt.Insets (0, 5, 0, 0); 81 findPanel.add(findField, gridBagConstraints); 82 findField.getAccessibleContext().setAccessibleName("Find Field"); 83 findField.getAccessibleContext().setAccessibleDescription("Find field."); 84 85 gridBagConstraints = new java.awt.GridBagConstraints (); 86 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 87 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 11); 88 getContentPane().add(findPanel, gridBagConstraints); 89 90 buttonPanel.setLayout(new java.awt.GridBagLayout ()); 91 92 findButton.setMnemonic('f'); 93 findButton.setText("Find"); 94 findButton.addActionListener(new java.awt.event.ActionListener () { 95 public void actionPerformed(java.awt.event.ActionEvent evt) { 96 findButtonActionPerformed(evt); 97 } 98 }); 99 100 buttonPanel.add(findButton, new java.awt.GridBagConstraints ()); 101 102 closeButton.setMnemonic('c'); 103 closeButton.setText("Close"); 104 closeButton.addActionListener(new java.awt.event.ActionListener () { 105 public void actionPerformed(java.awt.event.ActionEvent evt) { 106 closeButtonActionPerformed(evt); 107 } 108 }); 109 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.insets = new java.awt.Insets (0, 5, 0, 0); 112 buttonPanel.add(closeButton, gridBagConstraints); 113 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.gridx = 0; 116 gridBagConstraints.gridy = 1; 117 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; 118 gridBagConstraints.insets = new java.awt.Insets (17, 12, 11, 11); 119 getContentPane().add(buttonPanel, gridBagConstraints); 120 121 } 123 127 private void findFieldActionPerformed(java.awt.event.ActionEvent evt) { if (findField.getText().trim().length() > 0) 129 findButton.doClick(); 130 } 132 136 private void findButtonActionPerformed(java.awt.event.ActionEvent evt) { String text = textEditor.getText(); 139 String textToFind = findField.getText(); 140 if (!"".equals(textToFind)) { 141 int index = text.indexOf(textToFind); 142 if (index != -1) { 143 textEditor.setCaretPosition(index); 144 closeDialog(null); 145 } else { 146 java.awt.Toolkit.getDefaultToolkit().beep(); 147 } 148 } 149 } 151 155 private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) { closeDialog(null); 157 } 159 162 private void closeDialog(java.awt.event.WindowEvent evt) { setVisible(false); 164 dispose(); 165 } 167 168 private javax.swing.JPanel buttonPanel; 170 private javax.swing.JButton closeButton; 171 private javax.swing.JButton findButton; 172 private javax.swing.JTextField findField; 173 private javax.swing.JLabel findLabel; 174 private javax.swing.JPanel findPanel; 175 177 private javax.swing.JTextArea textEditor; 178 179 } 180 | Popular Tags |