Solved

two swing issues

Posted on 2004-08-19
49
162 Views
Last Modified: 2013-12-29
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);      
            }
0
Comment
Question by:ByrdBrain
  • 21
  • 18
  • 5
  • +2
49 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11839915
1. What do you mean by launch?

2.

if(e.getSource() == ButtonOK)
         {
              win2.dispose(0);    
          }
0
 

Author Comment

by:ByrdBrain
ID: 11839927
1. What do you mean by launch?

I mean I want the xml file to actually open.....its not opening at the moment!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11839928
To open where? In a text area? Or to load it up in memory?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11839939
Whoops

>>win2.dispose(0);    

should have been

win2.dispose();    
0
 

Author Comment

by:ByrdBrain
ID: 11839946
--------------------Configuration: jdk <Default>--------------------
......\gui\win2.java:100: dispose() in java.awt.Window cannot be applied to (int)
                  win2.dispose(0);
                               ^
1 error
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11839947
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11839953
>>dispose() in java.awt.Window cannot be applied to (int)

(See correction above)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11839966
To load the xml file into memory take a look here: http://javaalmanac.com/egs/javax.xml.parsers/BasicDom.html
0
 

Author Comment

by:ByrdBrain
ID: 11839971
>>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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 50 total points
ID: 11839977
Put the code that calls it outside the main method.
0
 

Author Comment

by:ByrdBrain
ID: 11839981
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11839986
To manipulate the window you need a reference to it, so you must call win2.dispose() where 'win2' is a reference
0
 
LVL 35

Expert Comment

by:girionis
ID: 11839987
Where do you want to view it? Just go through it and append it to the component you want.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11839995
>>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
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 11840010
>>if(returnVal != JFileChooser.APPROVE_OPTION){ return null;}

try to open the xml editor or any notepad/textpad/editor installed on your system.
0
 

Author Comment

by:ByrdBrain
ID: 11840011
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840034
>>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
 

Author Comment

by:ByrdBrain
ID: 11840041
>>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
 
LVL 6

Expert Comment

by:expertmb
ID: 11840056
>>if(returnVal != JFileChooser.APPROVE_OPTION){ return null;}
put here
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11840065
I would separate them. i.e.

File f = openFile(); // call your method
if (f != null) {
     // call my code
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840074
>>I would separate them

... as you should not tightly couple two distinct methods
0
 

Author Comment

by:ByrdBrain
ID: 11840101
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
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 11840119
>>   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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840121
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ByrdBrain
ID: 11840127
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840162
Please post your code (as little as possible)
0
 

Author Comment

by:ByrdBrain
ID: 11840239
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840306
>>Missing.dispose();    


As mentioned before, you are not using the reference here, you're using the class name. Use the reference
0
 

Author Comment

by:ByrdBrain
ID: 11840695
mFrame is my reference?

mFrame.dispose(); is giving error.....Cannot resolve symbol
0
 

Author Comment

by:ByrdBrain
ID: 11840722
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840729
>>mFrame is my reference?

Yes. You should make mFrame an instance variable of the class that has those event handlers
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11840746
>>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
 

Author Comment

by:ByrdBrain
ID: 11840773
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11840831
class ReportUI {
    private JFrame missing;

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

    // etc

}

0
 

Author Comment

by:ByrdBrain
ID: 11841089

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
 

Author Comment

by:ByrdBrain
ID: 11841154
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11841652
>>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
 

Author Comment

by:ByrdBrain
ID: 11842014
>>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
 
LVL 3

Accepted Solution

by:
JohnnyAffa earned 150 total points
ID: 11842041
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11842088
>>>>
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11842153
>>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
 

Author Comment

by:ByrdBrain
ID: 11842212
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11842280
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
 

Author Comment

by:ByrdBrain
ID: 11842290
ButtonOK = new JButton("OK");
            ButtonOK.addActionListener(this);
            Buttons.add(ButtonOK);
0
 

Author Comment

by:ByrdBrain
ID: 11842510
hey I got it sorted....
all i needed was...
      if(event.getSource() == ButtonOK)
            {
               setVisible(false);        
            }

so thats problem 2 sorted....thanks!

            
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11859488
Plenty of help given ...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11879192
:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879193
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
Java - Why doesn't this JFrame work 3 18
GUI builder for Eclipse? 8 11
Cipher Configuration on Apache HTTPD 4 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now