two swing issues

Hi,
(1.) I have an open method which is working fine but i actually want to launch the file when it is selected and clicked....
here's what i have so far...

private File openFile(){
            
            JFileChooser fc = new JFileChooser(".");
            fc.setFileFilter(new javax.swing.filechooser.FileFilter()
            {
                  public boolean accept(File file)
                  {
                        if(file.getName().endsWith(".xml")) return true;
                        return false;
                  }
                  public String getDescription()
                  {
                        return "XML";
                  }
            });
            int returnVal = fc.showOpenDialog(this);
            int c = 0;
            if(returnVal != JFileChooser.APPROVE_OPTION){ return null;}
            File file = fc.getSelectedFile();

            return file;
      }

(2) I've got two window.....e.g. Win 1 and Win2.......I can launch win2 from win1 via jbutton....this is fine
but when I click ok in win2 I only want Win2 to close and win1 to stay open.....how can I do this....at the moment i only have a command which will shut down the entire program....


                  if(e.getSource() == ButtonOK)
            {
                  System.exit(0);      
            }
ByrdBrainAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
1. What do you mean by launch?

2.

if(e.getSource() == ButtonOK)
         {
              win2.dispose(0);    
          }
0
ByrdBrainAuthor Commented:
1. What do you mean by launch?

I mean I want the xml file to actually open.....its not opening at the moment!
0
girionisCommented:
To open where? In a text area? Or to load it up in memory?
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

CEHJCommented:
Whoops

>>win2.dispose(0);    

should have been

win2.dispose();    
0
ByrdBrainAuthor Commented:
--------------------Configuration: jdk <Default>--------------------
......\gui\win2.java:100: dispose() in java.awt.Window cannot be applied to (int)
                  win2.dispose(0);
                               ^
1 error
0
CEHJCommented:
To open the xml file in a dedicated program:



Thread launchingThread = new Thread(new Launcher(file.getName());
launchingThread.start();

class Launcher implements Runnable {
      private String fileName;
      
      public Launcher(String fileName) {
            this.fileName = fileName;
      }
      public void run() {
            Runtime().getRuntime().exec("YourXMLProgram.exe " + fileName);
      }    
};
0
CEHJCommented:
>>dispose() in java.awt.Window cannot be applied to (int)

(See correction above)
0
girionisCommented:
To load the xml file into memory take a look here: http://javaalmanac.com/egs/javax.xml.parsers/BasicDom.html
0
ByrdBrainAuthor Commented:
>>dispose() in java.awt.Window cannot be applied to (int)

I now have another error....
 non-static method dispose() cannot be referenced from a static context

but my methods are not declared as static?
            
                           

0
girionisCommented:
Put the code that calls it outside the main method.
0
ByrdBrainAuthor Commented:
>>To load the xml file into memory take a look here

the file is already created and i only want to open it to view it by clicking open?
0
CEHJCommented:
To manipulate the window you need a reference to it, so you must call win2.dispose() where 'win2' is a reference
0
girionisCommented:
Where do you want to view it? Just go through it and append it to the component you want.
0
CEHJCommented:
>>the file is already created and i only want to open it to view it by clicking open?

Jus substitute your viewing program path for YourXMLProgram.exe in my example above
0
expertmbCommented:
>>if(returnVal != JFileChooser.APPROVE_OPTION){ return null;}

try to open the xml editor or any notepad/textpad/editor installed on your system.
0
ByrdBrainAuthor Commented:
>>Where do you want to view it? Just go through it and append it to the component you want.

I just want it to open as you would open a word doc or txt doc......just open the file as is....i'm not trying to load it into my program
0
CEHJCommented:
>>I just want it to open as you would open a word doc or txt doc

Just choose the program you want to use and substitute its name for 'YourXMLProgram.exe' in my example above
0
ByrdBrainAuthor Commented:
>>Just choose the program you want to use and substitute its name for 'YourXMLProgram.exe' in my example above
should i put that method within my Open method that i already have? or separately and then call it?
0
expertmbCommented:
>>if(returnVal != JFileChooser.APPROVE_OPTION){ return null;}
put here
0
CEHJCommented:
I would separate them. i.e.

File f = openFile(); // call your method
if (f != null) {
     // call my code
}
0
CEHJCommented:
>>I would separate them

... as you should not tightly couple two distinct methods
0
ByrdBrainAuthor Commented:
got a couple of errors......

cannot resolve symbol
symbol  : class Launcher
location: class ReportUI
            Thread launchingThread = new Thread(new Launcher(file.getName()));
                                                        ^
cannot resolve symbol
symbol  : method Runtime ()
location: class Launcher
          Runtime().getRuntime().exec("YourXMLProgram.exe " + fileName);
          ^
2 errors
0
expertmbCommented:
>>   Runtime().getRuntime().exec("YourXMLProgram.exe " + fileName);
Runtime.getRuntime().exec("YourXMLProgram.exe " + fileName);

you can try this one also

 if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
 //open the editor of your chcoice.
 
}
0
CEHJCommented:
Launcher should be included as an inner class in ReportUI

>>Runtime().getRuntime().exec("YourXMLProgram.exe " + fileName);

Sorry - typo there should be:

Runtime.getRuntime().exec("YourXMLProgram.exe " + fileName);
0
ByrdBrainAuthor Commented:
>>To manipulate the window you need a reference to it, so you must call win2.dispose() where 'win2' is a reference

this is outside main but i still have the error....also where win2 is a reference in win2 is within a similar method as follows...

      public void actionPerformed(ActionEvent e){
            if(e.getSource() == ButtonOK)
            {
                  win2.dispose();
            }            
}
0
CEHJCommented:
Please post your code (as little as possible)
0
ByrdBrainAuthor Commented:
this is how i call win2 (known as missing) from win1

public void actionPerformed(ActionEvent e){
      if(e.getSource() == ButtonMissing)
      {
       Missing mFrame = new Missing();
              mFrame.setLocation(300,100);
          mFrame.setVisible(true);
            }

then in Missing()
JButton ok...is declared....and then outside Missing()....
I've got...
      public void actionPerformed(ActionEvent e){
            
            //Conditions if Missing or Compare buttons are clicked
            if(e.getSource() == ButtonOK)
            {
                  //System.exit(0);
                  Missing.dispose();      
            }
      }

this is all i've got in my main for this window...
public static void main(String args[]){
            Missing mFrame = new Missing();
            mFrame.setLocation(300,100);
            //mFrame.setSize(400,400);
            mFrame.setVisible(true);
            
      }

0
CEHJCommented:
>>Missing.dispose();    


As mentioned before, you are not using the reference here, you're using the class name. Use the reference
0
ByrdBrainAuthor Commented:
mFrame is my reference?

mFrame.dispose(); is giving error.....Cannot resolve symbol
0
ByrdBrainAuthor Commented:
i've even tried this....

      if(e.getSource() == ButtonOK)
            {
                  
                  Missing me = new Missing();
                  //System.exit(0);
                  me.dispose();
                  
            }
it compiles and runs but then the button doesn't close the window
0
CEHJCommented:
>>mFrame is my reference?

Yes. You should make mFrame an instance variable of the class that has those event handlers
0
CEHJCommented:
>>i've even tried this....

Creating yet another window is not going to help. Just create the one and assign it to the instance variable. It doesn't necessarily have to be shown as soon as it's created
0
ByrdBrainAuthor Commented:
if i dont create another window, i have this problem....
I'm probably not creating instances properly?

: cannot resolve symbol
symbol  : variable mFrame
location: class Missing
                  mFrame.dispose();
0
CEHJCommented:
class ReportUI {
    private JFrame missing;

    public ReportUI() {
        missing = new JFrame();
    }

    // etc

}

0
ByrdBrainAuthor Commented:

i've tried the following...compiles but the button doesn't do anything.....

class Missing {
    private JFrame missing;

    public ReportUI() {
        missing = new JFrame();
    }

    public void actionPerformed(ActionEvent e){
                  
            //Conditions if Missing or Compare buttons are clicked
            if(e.getSource() == ButtonOK)
            {
                  missing.dispose();
                  
            }

}
0
ByrdBrainAuthor Commented:
i think i need to declare Launcher but I'm not sure where,.....can you suggest something please?
here's what i've got...

            if(file!=null){
      
            Thread launchingThread = new Thread(new Launcher(file.getName()));
            launchingThread.start();

class Launcher implements Runnable {
     private String fileName;
     
     public Launcher(String fileName) {
          this.fileName = fileName;
     }
     public void run() {
          Runtime.getRuntime().exec("YourXMLProgram.exe " + fileName);

     }    
};
}
0
CEHJCommented:
>>compiles but the button doesn't do anything.....

.. which suggests that the listener has not been added to it properly

>>but I'm not sure where,.....can you suggest something please?

As i mentioned earlier, make it an inner class of the class from which you want to launch the xml file
0
ByrdBrainAuthor Commented:
>>which suggests that the listener has not been added to it properly
i've tested the listener with System.exit(0); and this works fine so the listener is not a problem

as for the other issue.....I'm not sure by what you mean by an inner class....
do i need to declare 'Launcher' somewhere??

thanks
0
JohnnyAffaCommented:
Byrd,
instead of trying to destroy the second window, try using setVisible(false);  that way when you click the button a subsequent time it doesnt have to create the second window object again!

and i dont think you really need a thread to launch you second window.  simply add an action listener to the button eg

JFrame window2 = new JFrame();

// set attirbutes for window2 but DONT display it until you click the button

 JButton button = new JButton();
button.addActionListener(this);

public void actionPerformed(ActionEvent event)
{
   if ( event.getSource() == button )
          window2.setVisible(true);
}

............

youll have to add a windowlistener to window2 so that when you click close window, it only hides it an NOT destroy it.

as for the opening of the xml file, like the others have said, you havent told your program how to view it.  its not like window98 where it knows that when you click on say, .html it open IE.  as the others have pointed out you need a program to view its contents, hence the

try
{
Runtime program = Runtime.getRuntime().exec("C\\Windows\\notepad.exe " + [your xml file name]);
}
catch (Exception a)
{
   System.out.println(a);
}

i hope that clears things up for you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>>>
>>which suggests that the listener has not been added to it properly
i've tested the listener with System.exit(0); and this works fine so the listener is not a problem
>>>>

Well this is a contradiction in terms. If System.exit(0) works in the event handler and the listener is therefore not a problem, then any code other than System.exit(0) should also be called, yet you say the 'button doesn't do anything' ...

>>I'm not sure by what you mean by an inner class....

This shows what an inner class is:

public class Outer {
    // methods of Outer
    class Inner {
        // methods of Inner
    }
}
0
CEHJCommented:
>>instead of trying to destroy the second window, try using setVisible(false);  

That's OK, depending on what ByrdBrain wants to do - he may not want to do anything other than get rid of it for evermore ...

>>and i dont think you really need a thread to launch you second window

The thread is there for executing another program with Runtime and not really much to do with windows really
0
ByrdBrainAuthor Commented:
>>That's OK, depending on what ByrdBrain wants to do - he may not want to do anything other than get rid of it for >>evermore ...

this window will have info on it so if user clicks OK to close the window then it should close but it can still be reopened from win1 if need be!

>>window2.setVisible(true);
this compiles without error but does nothing at runtime when OK button is clicked....same problem as before with dispose();


0
CEHJCommented:
Use setVisible then

>>this compiles without error but does nothing at runtime when OK button is clicked

then the listener has almost certainly not been added properly
0
ByrdBrainAuthor Commented:
ButtonOK = new JButton("OK");
            ButtonOK.addActionListener(this);
            Buttons.add(ButtonOK);
0
ByrdBrainAuthor Commented:
hey I got it sorted....
all i needed was...
      if(event.getSource() == ButtonOK)
            {
               setVisible(false);        
            }

so thats problem 2 sorted....thanks!

            
0
CEHJCommented:
Plenty of help given ...
0
girionisCommented:
:)
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.