Design my own button in JFileChooser

Hi
I want my button in JFileChooser to give me the path to the directory that is selected. The thing is that I want my "save button" to do what my cancel button does. How do I do that. I cant press the "save button" if i dont select a file or a catolog containing other catalogs. Is there someting I can use like "CUSTOM_DIALOG" or something...all I want is for my program to listen for that button beeing pressed..
As I said...pressing the cancel button executes the operation I want my save button to do...
Heres my code:
public class BrowserWindow extends JFrame
public BrowserWindow()
Container con = this.getContentPane();
          final JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = jfc.showDialog(this, "Save BackUp");
switch (result)
case JFileChooser.APPROVE_OPTION:
System.out.println(jfc.getCurrentDirectory());
break;
case JFileChooser.CANCEL_OPTION:
System.out.println(jfc.getCurrentDirectory());
break;
case JFileChooser.ERROR_OPTION:
break;
}

Done. Your test case works fine if you replace getCurrentDirectory() with
getSelectedFile() for the APPROVE_OPTION branch in your code. If you still
disagree with me, then please provide exact information on operating system and
java version.
import java.awt.*;
import javax.swing.*;
public class BrowserWindow extends JFrame {
    public BrowserWindow() {
        Container con = this.getContentPane();
        final JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = jfc.showDialog(this, "Save BackUp");
        switch (result) {
          case JFileChooser.APPROVE_OPTION:
            System.out.println(jfc.getSelectedFile());
            break;
          case JFileChooser.CANCEL_OPTION:
            System.out.println("Cancelled");
            break;
          case JFileChooser.ERROR_OPTION:
            break;
    public static void main(String[] args) {
        new BrowserWindow().setVisible(true);
}

Similar Messages

  • My own button

    After adding JButtons(with an image only) to a JToolBar, I noticed that
    it is unnoticable when you press it. I wanted to make my own button
    which does about the same the .NET style toolbars do. The result can be found here. If anyone wants to be so freely to test it if it works on
    your toolbars?
    Thanks
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.*;
    import java.awt.geom.*;
    public class KButton extends JButton implements MouseListener
         BufferedImage bimg;
         Graphics2D g2;
         String toolTipText;
         Image image;
         int
              height, width,
              x = 2,
              y = 2,
              sp = 4; // space extra to add to sizes and stuff
         private Color fillColor = new Color(204,204,204);;
         private boolean
              isPressed = false,
              isEntered = false,
              isExited = false;
         public KButton(ImageIcon icon, String toolTipText)
              super(icon);
              this.image = icon.getImage();
              this.height = image.getHeight(this);
              this.width = image.getWidth(this);
              this.toolTipText = toolTipText;
              bimg = new BufferedImage(width + 1, height + 1, BufferedImage.TYPE_INT_RGB);
              g2 = bimg.createGraphics();
              super.setToolTipText(toolTipText);
              super.addMouseListener(this);
              super.setMaximumSize(new Dimension(width + sp,height + sp));
              super.setPreferredSize(new Dimension(width + sp, height + sp));
              super.setBorder(null);
              repaint();
         public void setBackground(Color bgc)
              this.fillColor = bgc;
         public void paint(Graphics g)
              // set the background
              g2.setPaint(fillColor);
              // clear the background
              g2.fillRect(0,0,width+sp, height+sp);
              // paint the icon to graphics 2d
              g2.drawImage(image, 0,0, this);
              // paint a border, or not
              if(isEntered)
                   g2.setPaint(Color.white);
                   g2.draw(new Line2D.Double(0,0, width, 0));
                   g2.draw(new Line2D.Double(0,0, 0, height));
                   g2.setPaint(Color.gray);
                   g2.draw(new Line2D.Double(0,width, width, height));
                   g2.draw(new Line2D.Double(width, 0, width, height));
              else if(isPressed)
                   g2.setPaint(Color.gray);
                   g2.draw(new Line2D.Double(0,0, width, 0));
                   g2.draw(new Line2D.Double(0,0, 0, height));
                   g2.setPaint(Color.white);
                   g2.draw(new Line2D.Double(0,width, width, height));
                   g2.draw(new Line2D.Double(width, 0, width, height));
              // paint the whole lotta image on the button
              g.setColor(fillColor);
              g.clearRect(0,0,width+8,height+8);
              g.drawImage(bimg, x,y, this);
         /* =====================================
         The following gives attention to actions which should be taken when the user
         presses, enters, exits or releases the mouse on the button
         ======================================== */
         public void mousePressed(MouseEvent e)
              isEntered = false;
              isPressed = true;
              x = 3;
              y = 3;
              repaint();
         public void mouseReleased(MouseEvent e)
              isPressed = false;
              x = 2;
              y = 2;
              repaint();
         public void mouseClicked(MouseEvent e)
              isPressed = false;
              isEntered = false;
              repaint();
         public void mouseEntered(MouseEvent e)
              isEntered = true;
              repaint();
         public void mouseExited(MouseEvent e)
              isEntered = false;
              repaint();

    Done. Your test case works fine if you replace getCurrentDirectory() with
    getSelectedFile() for the APPROVE_OPTION branch in your code. If you still
    disagree with me, then please provide exact information on operating system and
    java version.
    import java.awt.*;
    import javax.swing.*;
    public class BrowserWindow extends JFrame {
        public BrowserWindow() {
            Container con = this.getContentPane();
            final JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = jfc.showDialog(this, "Save BackUp");
            switch (result) {
              case JFileChooser.APPROVE_OPTION:
                System.out.println(jfc.getSelectedFile());
                break;
              case JFileChooser.CANCEL_OPTION:
                System.out.println("Cancelled");
                break;
              case JFileChooser.ERROR_OPTION:
                break;
        public static void main(String[] args) {
            new BrowserWindow().setVisible(true);
    }

  • Can i design my own social media widgets  for Facebook, etc

    I would like to design my own social media icon for my widgets (Facebook, pinterest, etc) Is that possible?

    Hi,
    not exactly in Adobe Muse, but you can create an icon using Photoshop easily, I found a tutorial for you,
    http://tutvid.com/photoshop/create-facebook-twitter-buttons-photoshop-tutorial/
    If you meant something else or I f I missed anything, please let me know.

  • I have an iPad 2 and want to be able to design my own templates for invitations, labels, and such. What do I need to do to be able to do this? Apps??

    I have an iPad 2 and want to be able to design my own templates for invitations, labels, and such. What do I need to do to be able to do this? Apps??

    Take a look at these three sites and you might find something in one that will work for you.
    http://appadvice.com/appnn/2011/06/appguide-updated-vector-drawing-apps-ipad
    http://jaevin.com/blog/2011/02/20/ipad-sketching-drawing-apps/
    http://www.designer-daily.com/10-great-ipad-applications-for-creative-people-623 4
    I downloaded iDraw - which is an Adobe Illustrator wannabe for the iPad. You can export to PDF and other formats using iDraw. iDraw also supports file sharing. File Sharing is the iPad/iTunes way to send files back and forth from the iPad to your computer and back again.
    This site will tell you about iDraw.
    http://www.indeeo.com/idraw/
    This is a link to iOS File Sharing which you will find useful as well.
    http://support.apple.com/kb/ht4094
    There are third party apps in the app store that will allow you to use virtually any printer even if you don't have one of the HP AirPrint compatible printers.
    You can even use an app like Pages to create your templates. You can insert art and photos into Pages and it supports file sharing as well. Pages is a Word Processing app and I would be inclined to not use it for your purposes but it will work for some basic designs however it's certainly not ideal for creating artwork.
    http://www.apple.com/ipad/from-the-app-store/pages.html

  • BP_CONT_SEARCH own button and own logic

    Hello,
    i need to implement a own button in the result list of component BP_CONT_SEARCH.
    Which method do i need to redifine to add my own button?
    With this button i need to read additional data to all Contact persons in the result list and export a detailed list per excel.
    What is the easiest way to get all contact persons (for example ID or GUID) in the result list?
    Thank you
    Kind regards
    Manfred

    For button creation, code in DO_PREPARE_OUTPUT of the result IMPL class after redefining. Once done. On-Click event name should be created in the same IMPL class. This method id where you will code the logic.
    Since the netity would be available from the context node RESULT, you will have the collection for all the entities. STRUCT.BP_GUID  will give you the necessary GUID of the concerned contact person.
    Rg,
    Harshit

  • Own Button in VA01/02/03 for calling an own FM to show some Information

    Hi,
    I'm looking for an EXIT to create an own button in VA01/02/03 to trigger an own FM.
    This FM shell used to give additional Information for the Materials wich are in this sales-Order.
    I don't want to update sap-standart code. I only want to use EXIT or BADI.
    Regards, Dieter

    Dieter
    This enhancement is not possible with Standard SAP..
    Thanks
    Amol Lohade

  • How to create own button for closed captioning

    Hi,
    I have created my own buttons for play, pause, exit etc. But I do not know how to create button to show closed captioning.  I tried few hours but I give up. What I need to do?:
    First clik - closed captioning will appear
    Second clik - closed captioning will disapear.
    And so on appear/disapear/appear/disapear
    Help please!
    Poul

    Have a look at: Toggle Shape buttons - Captivate 6 - Captivate blog
    Replace the system variable cpCmndMute by cpCmndCC and you'll have the choice between different scenarios.

  • Extending ContentBySearchWebPart with own buttons

    Dear all,
    I want to extend the Content Search Web Part (CSWB) with own buttons/tabs to refine the query. My idea was to derive a custom web part from the CSWB, add these buttons to the web part area and to extend the query in the button's event.
    My actual problem is to place the button to the web part area. I used the CreateChildControls() method to add my buttons (to be exact RadioButton) to the Controls collection, but the buttons will be always attached at the bottom of the web part.
    Here is my code:
    protected override void CreateChildControls()
    base.CreateChildControls();
    //create filter buttons
    for (int i = 0; i < 3; i++)
    var filter = new RadioButton();
    filter.Text = "filter " + i.ToString();
    filter.GroupName = "NewsCSWP_tabs";
    //set input properties
    filter.InputAttributes["class"] = "NewsCSWP_radio";
    //this.Controls.Add(filter);
    this.Controls.AddAt(0, filter);
    And it has the same effect if I use the .Add(...) or .AddAt(...) method. I tried also to call the base.CreateChildControls() after the Controls.Add(), but my buttons still appears at the bottom.
    What is the supported way to achive this?
    Thanks!

    Maybe its  possible, but it is not recommended to do so.
    However it does not make to much sense.
    Just run your servlet as a portal compoent with an entry as native-Servlet in the pc descriptor.
    Thats a 5 minute trip.

  • Just purchased Mac Book,   VERY  new to Mac world,   "ouch",,  I am looking to design my own website,,  is there a program built in that will allow this,  if so,, what is it called, and where can I find it???   Thanks,

    Just purchased Mac Book,   VERY  new to Mac world,   "ouch",,  I am looking to design my own website,,  is there a program built in that will allow this,  if so,, what is it called, and where can I find it???   Thanks,

    There are many ways and applications that you can use to build a web site. Which will be best for you will depend on the site you want to build and your level of expertise. Many experienced web developers just use a text editor, often one with specialized features for web programming (BBEdit, for example), but there are programs that can offer additional help, up to and including "drag and drop". Without knowing more about your needs, it's difficult to offer specific advice, but here are some possibilities:
    iWeb - came with your Mac. Easy to build but rather limiting in what you can do.
    RapidWeaver - more flexible and capable than iWeb but harder to use; sort of an "inbetweener" application
    Dreamweaver - powerful but expensive and a steep learning curve
    There are of course a number of other options. If you can provide more detail about what you wish to build and your level of experience, someone can probably make more targeted recommendations.
    Regards.

  • How to add my own button in the system toolbar?

    Hi,
    Is it possible to add my own button in the system toolbar which is linked to one of my own menu item ? I did a little bit search in the forum, all said it is not possible. Can anyone confirm it?
    Thanks,
    Lan

    Hi Lan,
    Your finding is correct. The toolbar is not open to UI API. We do not have any methods to change anything there.
    You may check this if you haven't: Browse Buttons in B1
    Thanks,
    Gordon

  • Up button in JFileChooser (Windows L&F)

    If you keep clicking the Up button in JFileChooser, eventually it'll go to My Computer, then Desktop, the My Documents, and then keep alternating between Desktop and My Documents.
    Question is, is there a rational for this? Wouldn't it be more intuitive to stop at some "topmost" destination folder, being Desktop or My Documents? Going "up" and "down" seems confusing, at least to the end user.

    How is 'Desktop' a virtual directory when the desktop folder actually exists in C:\<Users|Documents and Settings>\<User>\Desktop?
    I would call that a code issue unless my definition of 'virtual folder' is different than yours.
    From what I know, the Windows file chooser usually does this when going up directories:
    Current directory>Possibly a few more directories>%SystemDrive%\>My Computer>Desktop
    As in that example, everything should go back to the Desktop (even My Documents) in the end. And I would guess that the JFileChooser does (or should) follow that path to blend in better with other Windows apps.

  • How to change the text in the Cancel button of JFileChooser ?

    Hello,
    In french the standart text in the Cancel button of the JFileChooser class is "Annuler". For a good reason which will be too long to explain, I need to change it by another term.
    I tried to create a super class and redefine the CANCEL_SELECTION string, but this does not work. I can't either change it directly because it has the final attribute.
    Where does JFileChooser get this word from ? ( I guess it is not hard coded for multilanguage support purpose).
    Any help would be appreciated
    Gege

    I believe the standard Look and feel implementations look in UIManager for a string called FileChooser.cancelButtonText.
    Therefore you might be able to change it after you've loaded your LnF by using the following code:
    UIManager.put("FileChooser.cancelButtonText", "---change me---");Note that this would change all file choosers in your application.
    If you only want to update one type of file chooser you could extend, for example, MetalFileChooserUI and set its cancel button text by overriding installStrings. You'd then need to install this UI on the file chooser you want to change.
    There may be an easier way to do this!
    Hope this helps.

  • The default button in JFileChooser

    Is it possible to make either the 'save' or 'cancel' button the default button (the one that will be pressed when the user presses the 'enter' key after the dialog is shown) in a save-dialog ?
    It will probably involve setting the focus to either one of these buttons, but it is unclear how I can do that.
    Thanks in advance.

    public static JButton searchByText(Component comp, String[] texts) {
        if (comp instanceof JButton) {
            JButton btn = (JButton) comp;
            String text = btn.getText();
            for(int j=0; j<texts.length; ++j)
                if (text.equals(texts[j]))
                    return btn;
            return null;
        if (comp instanceof Container) {
            Container cont = (Container) comp;
            for(int j=0, ub=cont.getComponentCount(); j<ub; ++j) {
                JButton result = searchByText(cont.getComponent(j), texts);
                if (result != null)
                    return result;
        return null;
    }You can call this method, passing it your JFileChooser and:
    String[] approves = {"Open", "Save"};

  • The Bar that has the Live View button, design, code, split button disappeared! HELP!

    In dreamweaver Cs4, there is this new bar which has the Code, Design, Split buttons, also the Live view and Live Code buttons. Well it Dissapeared!! and i loooked everywere i cant find it, its just gone!
    i clicked alll the possible buttons on window and looked in google but i cant find anything
    if someone knows please help!
    i attached a pic

    Thank you so much!!! i knew it would be something so simple  

  • New Design & Browser Back Button

    hi,
    am I the only one or is someone else experiencing a problem with the browser's BACK button since we have the new SDN design?
    If I click the BACK button there is an attempt to load something but the same page stays there. Only if I kind of double-klick it (klick it twice very quickly) I get back.
    IE 6.0.2900 for XP SP2, UPDATE 2
    regards,
    anton

    verified that and that's correct.
    But - the page doesn't correctly reload after I press the submit button to logon.
    It looks as I weren't logged on (no name in the header, logon box still visible) until I hit the refresh button.
    Hitting the refresh button then it doesn't stop loading (Status: Retrieving data from www.sapteched.com) thoug my name is there, the logon box gone and everything else in place too
    Firefox 1.5.0.4
    anton
    Message was edited by: Anton Wenzelhuemer

Maybe you are looking for

  • 15" Macbook Pro (2007) will no longer boot in any manner. (Please, help.)

    Evening, everyone. I could beat my head against the wall for all the frustration my MBP is giving me! I really hope some kind soul will be able to help me. I will try to be methodical in explaining the whole story of what I have done and what appears

  • Info showing incorrect hard drive space

    Inside the finder the hard drive is showing about 23 gigs used after deleting user file (5g applications, 12 gig library, 6 gig system, 15 megs user) and creating new one (in order to sell) but get info on the hard drive icon is showing 52 gigs used.

  • Re: What happened to the 24/7 helpdesk?

    The broadband helpline open 24/7 is 08001114567 Non on of the numbers you posted is for residential services  phone faults 0800 800 151  sales 0800 800 150  If you want to say thanks for a helpful answer,please click on the Ratings star on the left-h

  • Renaming on import issue.

    I am attempting to rename my files on import with LR4. I am selecting the "Custom Name-Original file number" option, but not getting the result I want. For a file with the number 0123, from the camera, when I put SMITH in the custom text box I should

  • Instance of Objects

    I try to save an instance of an object by creating a new object with the object's values at the time. I add the new object into a vector. When the value of the object changes, why does the new object in the vector also change? How could I store the i