views:

105

answers:

3

I would like to have a JButton (with a folder icon image) inside a JTextField, like over on the far right of the JTextField, so that when clicked, the button opens up a JFileChooser, and when a file is selected, the path to the file appears inside the JTextField.

I have made this code, but nothing shows up.

public class TextFieldChooser extends JTextField {

    public ImageIcon folderIcon;
    public JButton btnFolder;

    public TextFieldChooser(int columns) {
        super(columns);
        btnFolder = new JButton();
        folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
        btnFolder.setIcon(folderIcon);
        this.add(btnFolder);

    }
}
+3  A: 

You can't don't want to put a button in a text field. You need to break out your intent into several components - 3, in fact.

First you're going to need a parent container, or something that will contain both your text field and also the button; a JPanel should suffice.

Then you need your real components, and by real I mean the ones that actually do something. These are your JTextField and JButton - go ahead and add these to the JPanel. In order to add them and have them appear how you want (with the button in the corner), you're going to need to specify a layout for your JPanel. This layout will define where added components go (visually) inside the JPanel.

Now that you've added those things into your JPanel, you can work only with your JPanel instead of thinking in terms of the contained JTextField and JButton.

Shakedown
Take a look at this screenshot, this program was written in Delphi, and I am remaking it in Java. Delphi obviously had a component that meets this need. Why can't Java?http://screencast.com/t/OTNhOTA2ZGY
Pyrite
You _can_ put a button in a text field; it's just awkward to render correctly.
trashgod
Actually you could probably set the border to 0 and just have both right next to each other making it look like one is inside of the other
TheLQ
@TheLQ: Exactly. `JPanel` is explicitly intended as "a generic lightweight container."
trashgod
+3  A: 

Building on what Shakedown suggested, I think you can get the desired effect relatively easily. What you do is have a JPanel that contains both the text area and, beside it, the button. Next, set the text field to not draw any borders and give the JPanel a bevel border. Now it will look like the button is inside the text area. It might take some fine tuning, but it should work.

Tikhon Jelvis
OK, that worked. Good thinking, and if this is what Shakedown originally intended to mean, then +1 to him too.
Pyrite
And here is a screen shot of my new component panel.http://screencast.com/t/MDdiMTM0MTA
Pyrite
Oh this is the right answer? Nice.... :D
Shakedown
+1  A: 

You may find the Component Border helpfull. It allows you to display a button in the text field by using the Border API.

camickr
+1 In addition to the novel API, this is also a useful exposition on fine-tuning the conventional approach.
trashgod
Sweet, this is great. The border I originally set on the JPanel worked good on Mac and Windows, but on Linux, the border failed. May be this will work better.
Pyrite