• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difficulties with JFileChooser

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you and greatly appreciate your help.
Jawad
 
Jawad Kakar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic