Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:48:36+00:00 2026-06-17T05:48:36+00:00

In my custom JFileChooser , I want to get the Open button, so I

  • 0

In my custom JFileChooser, I want to get the Open button, so I am using the following code:

private static JButton getOpenButton(Container c) {
  Validator.checkNull(c);

  int len = c.getComponentCount();
  JButton button = null;
  for (int index = 0; index < len; index++) {
    if (button != null) {
      break;
    }
    Component comp = c.getComponent(index);
    if (comp instanceof JButton) {
      JButton b = (JButton) comp;

      if ("Open".equals(b.getText())) {
        return b;
      }
    }
    else if (comp instanceof Container) {
      button = getOpenButton((Container) comp);
    }
  }
  return button;
}

The problem with this code is that it is inefficient (because of recursion) and also can be broken if localization is used (because the word “Open” is hard-coded).

I also want to get the JTextField in which the user can enter the name and path of a file. I am using this code to get this component:

private static JTextField getTextField(Container c) {
  Validator.checkNull(c);

  int len = c.getComponentCount();
  JTextField textField = null;
  for (int index = 0; index < len; index++) {
    if (textField != null) {
      break;
    }
    Component comp = c.getComponent(index);
    if (comp instanceof JTextField) {
      return (JTextField) comp;
    }
    else if (comp instanceof Container) {
      textField = getTextField((Container) comp);
    }
  }
  return textField;
}

Is there a better way I can get the Open button and the JTextField?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:48:37+00:00Added an answer on June 17, 2026 at 5:48 am

    In the constructor of my custom file chooser, I called the setApproveButtonText method and passed in a custom string to use for the Open button. I called this method before I get the Open button using the getOpenButton method below. This way, I am guaranteed to get the Open button on any OS platform and no matter what locale the JVM is using.

    private final String title;
    
    public CustomFileChooser(String title) {
      this.title = title;
      setApproveButtonText(title);
      this.button = getOpenButton(this);
    }
    
    private JButton getOpenButton(Container c) {
      Validator.checkNull(c);
    
      int len = c.getComponentCount();
      JButton button = null;
      for (int index = 0; index < len; index++) {
        if (button != null) {
          break;
        }
        Component comp = c.getComponent(index);
        if (comp instanceof JButton) {
          JButton b = (JButton) comp;
    
          if (this.title.equals(b.getText())) {
            return b;
          }
        }
        else if (comp instanceof Container) {
          button = getOpenButton((Container) comp);
        }
      }
      return button;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Custom button to open calendar is added to Jquery ui datepicker widget using code
I want to get multiple image URI using intent filter without implements a whole
All custom legacy software needs changing, or so say our users. Sometimes they want
My custom capistrano task app:sample fails with the following error message: mnylen ilmo-on-rails $
A custom Dialog box with a RelativeLayout contains a Button widget that won't change
Background Custom properties in Open XML are stored in docProps/custom.xml and look like this
My custom control uses a ScrollViewer and I want to determine when the user
My custom comparer doesn't seem to be working. I want a count of distinct
Creating custom IHttpModules, I have realized that the requests for static files (e.g.: .css
My custom button contains a value that's updated on swipe. But when I tap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.