Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to test jtextfield exists before attempt to .setText()

Status
Not open for further replies.

KirbyWallace

Programmer
Dec 22, 2008
65
US

Really stuck on this one...

I am about to .setText() on a textfield, but just before I attempt it, I need to check whether the field exists or not: the jFrame form that it is on may or may not be open.

Any ideas?

Thanks
 
I don't get the concept of "existance": if it's declared? initialized? added to the frame? And when is a JFrame opened?

I'd consider your program design overall

Cheers,
Dian
 
This sounds more like you want to know if the JFrame containing the textfield is available. To do that, use the isVisible() method like so..

Code:
if (frame.isVisible()) {
    textField.setText("Here is some text");
}
 
Hey Mikey! (Bet you NEVER hear that one, eh?)

Anyway, thanks, that is exactly what I was looking for. I didn't give a lengthy discussion of the program or source code 'cause I didn't want to bulk it (the message) up.

But the gist of it is the startup class will check for command line params, and if any are missing, will open a jFrame form to get the needed options from the user.

*IF* that happens, then the form has a status textarea on it that I want to dump status info to. But only IF the form was opened. If you supply all needed info on the command line, it does not. And THUS: The form may or may not be open.

So the isVisible() check is quite what I was looking for.

Thanks!
 
Here's the bigger picture, in case you are interested. I'm still going to have a problem, but I think I can work it. The deal is that the frame I want to test for is a private member of another class. I do not think I will be able to reference it. So if you have suggestions there, send them my way.

Code:
public class Main {

    public static void main(String[] args) {

        int i;
        String cInputFile = "";
        String cOutputFile = "";

        // process the command line for requested options.
        for (i = 0; i < args.length; i++) {

            if (args[i].equals("-i")) {
                cInputFile = args[i + 1];
            }
            if (args[i].equals("-o")) {
                cOutputFile = args[i + 1];
            }
        }

        // neither param specified - OPEN FORM TO GET THEM
        if (cInputFile.equals("") && cOutputFile.equals("")) {
            //*********************************************
            //************** LOOK HERE ********************
            //*********************************************
            MainFrame mf = new MainFrame();
            mf.setVisible(true);
        }

        // Input but not output, dump to console
        if (!cInputFile.equals("") && cOutputFile.equals("")) {
            FileToConsole.fileToConsole(cInputFile);
        }

        // input AND output - file copy
        if (!cInputFile.equals("") && !cOutputFile.equals("")) {
           FileToFile.fileToFile(cInputFile,cOutputFile);
        }
    }
}

Then...

Code:
public class MainFrame extends javax.swing.JFrame {


    JFileChooser fc;

    public MainFrame() {

        initComponents();

        fc = new JFileChooser();
        fc.setCurrentDirectory(new File("."));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);


    }

    :
    : GENERATED CODE/SWING LAYOUTS - not included here
    :
    :

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //*********************************************
                //************** LOOK HERE ********************
                //*********************************************
                new MainFrame().setVisible(true);
            }
        });
    }

}


And Finally... The form invokes fileToConsole, just the same as if the Main class had called it directly, but if the form is open, I want it to uptade the text area. But, FileToConsole is one class, and the frame was created in another. I'm going to have trouble figuring out how to reference it.


Code:
public class FileToConsole {

    public static void fileToConsole(String cFile) {

        try {

            BufferedReader input =  new BufferedReader(new FileReader(new File(cFile)));

            String line = null;

            while (( line = input.readLine()) != null) {
                 System.out.println(line);
                 
                //*********************************************
                //************** LOOK HERE ********************
                //*********************************************
                 // Add the same line to the jTextArea on the 
                 // MainFrame Panel, IF it is visible...
                     
            }
        }

        catch (IOException ex) {
            System.out.println("File Not Found");
        }

    }

}

This is actually my first ever Java program. I just don't want to default to the ubiquitous, and lame, "hello world" program. So I thought I'd tackle something a bit broader.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top