This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Difficulties with JFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Difficulties with JFileChooser" Watch "Difficulties with JFileChooser" New topic
Author

Difficulties with JFileChooser

Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
How to access the ok, cancel button of JFileChooser WITHOUT using showOpenDialog(Component) method.
Thank you
Jawad
[ October 28, 2002: Message edited by: Jawad Kakar ]
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
For what do you need the buttons?
You can change the text/mnemonic/tooltip with various API methods in JFileChooser, and you can addActionListeners to the buttons as well.
What exactly do you want to do?
Bill
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Bill,
Thank you for reply.
I want to have 2 JRadioButton on top and a FileChooser on the bottom of a JPanel which all goes inside a JFrame(I have more items in JFrame, but when JFileChooser is visible only JRadioButton is visible on the top of JFileChooser)
What I am doing NOW!!
// add 2 JRadioButton to the panel
...
// add JFileChooser to the panel
JFileChooser chooser = new JFileChooser();
localPane.add(chooser);
//Things looks fine(i.e I have 2 radio button on top and JFileChooser on the bottom of the panel)
Now I want to set Listeners to "Open" and "Cancel"
button of JFileChooser.
This is how but does NOT work !!!
chooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(chooser.APPROVE_SELECTION.equalsIgnoreCase("ApproveSelection")){
System.out.println(chooser.getSelectedFile()+" ok");// line 1
}
if(chooser.CANCEL_SELECTION.equalsIgnoreCase("CancelSelection")){
System.out.println(chooser.getSelectedFile()+" cancel");// line 2
}
}
});
When I click on cancel or ok button both if statements gets executed, so this way I don't know which button was clicked on.(i,e Open or Cancel).

Note: I don't want to use showOpenDialog(Component), because it will pop a JFileChooser inside a JDailog in a separate dialog.
Thank you for your time
Jawad
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
First of all, the if statements you have in your code are comparing a static String to a constant String. Neither of these values ever change, so they will always evaluate to the same thing.
Try overriding the following methods that get called when the approve or cancel buttons get pressed from the UI, respectively.
public void approveSelection()
public void cancelSelection()
No need to add listeners, just do what you want in these methods.
Bill
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Thank you and greatly appreciate your help.
Jawad
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Bill,
This is how I used "approveSelection() and cancelSelection()" methods.

public class StartupDialog extends JFrame {
private JFileChooser chooser = new FileChooser();
public void approveSelection(){
System.out.println("approveSelection() is called");
}
public void cancelSelection(){
System.out.println("cancelSelection()is called");
}
//this is how I call these methods
chooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cancelSelection();
approveSelection()
}
});
}// end of class
But when I click on Open or Cancel button both methods (cancelSelection() and approveSelection())gets called.
Please help.
Thank you
Jawad
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88

chooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cancelSelection();
approveSelection()
}

If you add a listener that directly calls both of those methods, then of course both will get called. :roll:
Try removing that whole bit of code that I quoted above. The buttons should automatically call one of those two functions when pressed. Moreover, each button will call only one method, and the right one at that.
Try that
Bill
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Bill,
I did the way to have suggested but still it does not work.
Do I have to implements some kind of interface?
That is how I do it now.
public class StartupDialog extends JFrame {
private JFileChooser chooser = new FileChooser();
public StartupDialog(){
approveSelection();
cancelSelection();
}
public void approveSelection(){
System.out.println("approveSelection() is called");
}
public void cancelSelection(){
System.out.println("cancelSelection()is called");
}

}// end of class
When I click on the buttons, neither of the method gets called.
Thank you once again
Jawad
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Bill,
I used a different approach and it worked, I could not make work with overriding
public void approveSelection()
public void cancelSelection()
I used
chooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
String command = evt.getActionCommand();
if(command.equals(JFileChooser.APPROVE_SELECTION)){
System.out.println("Ok was pressed");
file = chooser.getSelectedFile();
}
else if(command.equals(JFileChooser.CANCEL_SELECTION)){
System.out.println("Cancel was pressed");
dispose();
System.exit(0);
}
}
});
Thank you for your help.
Jawad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difficulties with JFileChooser
 
Similar Threads
File browser crash course
Simple file browser !!!!!!
Is there any Browse button control in Swing ??
How to disable JFileChooser's "create new folder "icon
how to write coding for browse button in java swing?