Pyrite
Pyrite

Reputation: 33

How to put a JButton inside a JTextField (Java)?

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);

    }
}

Upvotes: 3

Views: 12586

Answers (6)

gbiz123
gbiz123

Reputation: 158

This is working in 2024. Essentially you put a JButton and JTextField inside of a JPanel with BoxLayout. Then you set the size of the JTextfield to fill up the rest of the JPanel.

    private JPanel createButtonAndTextFieldCombo(JButton button, JTextField textField) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, 0));
        panel.setBackground(textField.getBackground());
        panel.setBorder(textField.getBorder());
        textField.setMinimumSize(new Dimension(panel.getWidth(), panel.getHeight()));
        textField.setBorder(BorderFactory.createEmptyBorder());
        panel.add(textField);
        panel.add(button);
        return panel;
    }

Upvotes: 0

d3st2k
d3st2k

Reputation: 3

Well I probably think you got your answer, but for others who want to do this easily, cuz I think the other answers are a bit too complicated. So, what you need to do is, when you create the JTextField you create the JButton as well. Well see the code for yourself:

JButton button = new JButton();
button.setBounds(50, 5, 50, 25);
button.setBackground(Color.black);
JTextField textField = new JTextField();
textField.setBounds(20, 60, 100, 35);
textField.setBackground(Color.white);
textField.add(button);

And its that easy, I used setBounds() on the button cuz I can place it anywhere I want, as for the textField you can use a frame/panel layout aswell, but this was for just demonstrating how it works.

Upvotes: 0

Killgnom
Killgnom

Reputation: 150

Because Pyrite has not posted his final solution, here is mine:

my_button = new JButton("x");
JFormattedTextField my_textfield = new JFormattedTextField("Nr.");
my_textfield.setBorder(javax.swing.BorderFactory.createEmptyBorder());

JPanel textfield_with_button = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Border lowered_bevelborder = BorderFactory.createLoweredBevelBorder();
textfield_with_button.setBorder(lowered_bevelborder);

textfield_with_button.add(my_textfield);
textfield_with_button.add(my_button);

enter image description here

Upvotes: 2

Nate W.
Nate W.

Reputation: 9259

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.

Upvotes: 4

camickr
camickr

Reputation: 324207

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

Upvotes: 6

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68172

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.

Upvotes: 4

Related Questions