Here's the code again because you said you couldn't paste it.
CODE
package utils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
/**
* @author brvmlwlw
*
*/
public class FindDialog extends JDialog implements ActionListener {
private static final long serialVersionUID = 3792644416895234356L;
private JButton btnFind;
private JCheckBox chkCaseSensitive;
private JButton btnCancel;
private JTextField txtFind;
// TODO: will be used to determine if the search is case sensitive or not
// when implemented
// private boolean caseSensitive = false;
private String title = "Find...";
private int returnValue;
private String CMD_CANCEL = "Cancel";
private String CMD_FIND = "Find";
public int OPTION_CANCEL = 0;
public int OPTION_FIND = 1;
public FindDialog(JFrame parent, boolean modal) {
super(parent, modal);
initComponents();
}
private void initComponents() {
setLayout(null);
txtFind = new JTextField();
add(txtFind);
txtFind.setBounds(12, 21, 236, 22);
btnFind = new JButton();
add(btnFind);
btnFind.setText("Find");
btnFind.setBounds(88, 83, 68, 24);
btnFind.addActionListener(this);
btnCancel = new JButton();
add(btnCancel);
btnCancel.setText("Cancel");
btnCancel.setBounds(167, 83, 85, 24);
btnCancel.addActionListener(this);
chkCaseSensitive = new JCheckBox();
add(chkCaseSensitive);
chkCaseSensitive.setText("Case Sensitive");
chkCaseSensitive.setBounds(93, 56, 149, 18);
chkCaseSensitive.addActionListener(this);
setSize(270, 166);
setTitle(title);
}
public String getSearchString() {
return txtFind.getText();
}
/**
* Gets the length of the string that was searched.
*
* @return The length of the string that was searched.
*/
public int getStringLength() {
return txtFind.getText().length();
}
/**
* This is overridden for the purposes of custom keypress functionality.
*/
protected JRootPane createRootPane() {
JRootPane rootPane = new JRootPane();
KeyStroke escapeStroke = KeyStroke.getKeyStroke("ESCAPE");
KeyStroke enterStroke = KeyStroke.getKeyStroke("ENTER");
Action escapeListener = new AbstractAction() {
private static final long serialVersionUID = 4076623498523743687L;
public void actionPerformed(ActionEvent actionEvent) {
windowAction(CMD_CANCEL);
}
};
InputMap inputMap = rootPane
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(escapeStroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", escapeListener);
Action findListener = new AbstractAction() {
private static final long serialVersionUID = 4076623498523743687L;
public void actionPerformed(ActionEvent actionEvent) {
windowAction(CMD_FIND);
}
};
inputMap.put(enterStroke, "ENTER");
rootPane.getActionMap().put("ENTER", findListener);
return rootPane;
} //createRootPane()
/**
* Shows the dialog and waits for user input before returning.
*
* @param s
* The string to be searched.
* @return The index where the search string is found. If no string is found
* then OPTION_NO_FIND is returned (to be implemented). Note: To get
* the length of the string found use getStringLength()
*/
public int showDialog() {
initComponents();
setVisible(true);
// waiting here...
return returnValue;
}
private void windowAction(Object actionCommand) {
String cmd = null;
boolean ret = false; // sets whether to return from dialog
if (actionCommand != null) {
if (actionCommand instanceof ActionEvent) {
cmd = ((ActionEvent) actionCommand).getActionCommand();
} else {
cmd = actionCommand.toString();
}
}
if (cmd == null) {
// do nothing
} else if (cmd.equals(CMD_CANCEL)) {
returnValue = OPTION_CANCEL;
setVisible(false);
ret = true;
} else if (cmd.equals(CMD_FIND)) {
//if (!(txtFind.getText().equals(""))) { // if any text was entered
returnValue = OPTION_FIND;
ret = true;
//}
}
if (ret) //return from the dialog
setVisible(false);
} // windowAction()
@Override
public void actionPerformed(ActionEvent e) {
String action = new String(e.getActionCommand());
// TODO: this code is just a place holder.
if (action.equals(CMD_CANCEL) || action.equals(CMD_FIND)) {
windowAction(action);
}
}
}
JBabineau: The imports didn't show because eclipse folded that part of the code so it wasn't included when I copy/pasted.
The class compiles and runs just fine. In fact it used to run perfectly and the bug that I'm trying to figure out now didn't exist. I must have changed something to create this bug.
I've attached a zip file which contains a jar that gives you a working test of the fileDialog(along with the source). You'll notice that the filedialog always returns "" when getSearchString() is called on it.
Thanks for all the help so far!
-Nathan