Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,310 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 869 people online right now. Registration is fast and FREE... Join Now!




Find dialog is driving me nuts

 
Reply to this topicStart new topic

Find dialog is driving me nuts

brvmlwlw
9 Dec, 2008 - 05:29 PM
Post #1

New D.I.C Head
*

Joined: 17 Oct, 2008
Posts: 13



Thanked: 1 times
My Contributions
I've got a find dialog that I created as part of a larger text editor which doesn't seem to work. The problem is that the find dialog will always return "" (empty string) when it should return whatever the user entered. I've stepped through this thing many times and what I've found is that in the method (I'll mark it with a comment, the method is called getSearchString()) that calls txtFind.getText(). getText() always returns "" and I have no idea why! mad.gif I'd be very appreciative of any help. Thanks!

java

package utils;

import java.awt.event.ActionEvent;

/**
* @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(); //calling class gets the search string here... but it returns ""
}

/**
* 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)) {
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);
}
}
}


User is offlineProfile CardPM
+Quote Post


janotte
RE: Find Dialog Is Driving Me Nuts
9 Dec, 2008 - 05:47 PM
Post #2

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
Does the code you posted compile for you?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Find Dialog Is Driving Me Nuts
9 Dec, 2008 - 05:49 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,955



Thanked: 673 times
Dream Kudos: 200
My Contributions
Hi,

don't know how you cut & pasted your code but I cannot paste it back to test it

You have a strange way to implement JDialog.. without a static method you'll have to create it frst and then display it

Both your constructor and the ShowDialog method call initComponents() you will have a lot of JComponent laying around

And what you think

CODE

public int showDialog() {          
   initComponents();          
   setVisible(true);        
    // waiting here...        
    return returnValue
}


that the code will be waiting for user interaction there...
User is online!Profile CardPM
+Quote Post

JBabineau
RE: Find Dialog Is Driving Me Nuts
9 Dec, 2008 - 05:55 PM
Post #4

D.I.C Head
Group Icon

Joined: 5 Dec, 2008
Posts: 65



Thanked: 6 times
My Contributions
QUOTE(janotte @ 9 Dec, 2008 - 05:47 PM) *

Does the code you posted compile for you?


It does if you import the following
CODE

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;



User is offlineProfile CardPM
+Quote Post

brvmlwlw
RE: Find Dialog Is Driving Me Nuts
9 Dec, 2008 - 07:00 PM
Post #5

New D.I.C Head
*

Joined: 17 Oct, 2008
Posts: 13



Thanked: 1 times
My Contributions
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


Attached File(s)
Attached File  Archive.zip ( 12.36k ) Number of downloads: 2
User is offlineProfile CardPM
+Quote Post

brvmlwlw
RE: Find Dialog Is Driving Me Nuts
10 Dec, 2008 - 03:00 PM
Post #6

New D.I.C Head
*

Joined: 17 Oct, 2008
Posts: 13



Thanked: 1 times
My Contributions
I found the problem. It seems the method initComponents() was being called twice. Once in the constructor and once in showDialog(). Any ideas as to why this would cause the problem I described earlier?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Find Dialog Is Driving Me Nuts
10 Dec, 2008 - 07:04 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,955



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(brvmlwlw @ 10 Dec, 2008 - 03:00 PM) *

I found the problem. It seems the method initComponents() was being called twice. Once in the constructor and once in showDialog(). Any ideas as to why this would cause the problem I described earlier?


Hey !!! That was my post

QUOTE

Both your constructor and the ShowDialog method call initComponents() you will have a lot of JComponent laying around

do you bother reading back the solutions we proposed ?



User is online!Profile CardPM
+Quote Post

brvmlwlw
RE: Find Dialog Is Driving Me Nuts
10 Dec, 2008 - 10:28 PM
Post #8

New D.I.C Head
*

Joined: 17 Oct, 2008
Posts: 13



Thanked: 1 times
My Contributions
Er.. I'm not sure how I missed that.... blink.gif

But thanks anyways! smile.gif
User is offlineProfile CardPM
+Quote Post

g00se
RE: Find Dialog Is Driving Me Nuts
11 Dec, 2008 - 02:46 AM
Post #9

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
Err.. i don't know if you posted the wrong code, but the code that you did post doesn't actually do anything in its events except set int return values ...
User is offlineProfile CardPM
+Quote Post

brvmlwlw
RE: Find Dialog Is Driving Me Nuts
11 Dec, 2008 - 01:49 PM
Post #10

New D.I.C Head
*

Joined: 17 Oct, 2008
Posts: 13



Thanked: 1 times
My Contributions
That's because the find dialog only needs to do 2 things: 1. return a value indicating what the user clicked(find/cancel) and 2. the string that the user entered.

Here's the code used to call the find dialog:

java

private void showFindDialog() {
FindDialog fd = new FindDialog(this, true);

int left = this.getLocation().x + (this.getWidth() / 2) - (fd.getWidth() / 2);
int top = this.getLocation().y + ((this.getHeight() / 2) - (fd.getHeight() / 2));

fd.setLocation(new Point(left, top));
fd.setVisible(false);
int returnVal = fd.showDialog();

// search string found
if (!(returnVal == fd.OPTION_CANCEL)) {
tc.getTabbedPane().findString(fd.getSearchString());
}

fd.dispose();
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 07:20PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month