Search code examples
javaswingjbuttonjtextfield

How to add button in text field?


I'm creating a text field in java using swing components. I want to make a search text field like one appears in Mozilla or other browsers.

I have added a button in text field. I have set border layout of JTextField. everything is working fine but whenever large text is written in text field (as it reaches the given size of text field) it goes behind the button. As everyone of you must have seen, this does not occur in search bars. Text must not go behind the button rather there must be some gap between button and text.

Does anyone know how to do that?


Solution

  • Maybe start with something like this:

    enter image description here

    The blinking cursor is positioned at the far right of the text field.

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    class ButtonsInTextField {
    
        JPanel gui = new JPanel(new GridBagLayout());
        JTextField textField;
    
        ButtonsInTextField(int cols) {
            JPanel textFieldWithButtonsPanel = new JPanel(new FlowLayout(
                    SwingConstants.LEADING, 5, 1));
            textField = new JTextField(cols);
            textFieldWithButtonsPanel.add(textField);
    
            addButtonToPanel(textFieldWithButtonsPanel, 8);
            addButtonToPanel(textFieldWithButtonsPanel, 16);
            addButtonToPanel(textFieldWithButtonsPanel, 24);
    
            // WARNING:  Not sensitive to PLAF change!
            textFieldWithButtonsPanel.setBackground(textField.getBackground());
            textFieldWithButtonsPanel.setBorder(textField.getBorder());
            textField.setBorder(null);
            // END WARNING:  
    
            gui.add(textFieldWithButtonsPanel);
        }
    
        private final void addButtonToPanel(JPanel panel, int height) {
            BufferedImage bi = new BufferedImage(
                    // find the size of an icon from the system, 
                    // this is just a guess
                    24, height, BufferedImage.TYPE_INT_RGB);
            JButton b = new JButton(new ImageIcon(bi));
            b.setContentAreaFilled(false);
            //b.setBorderPainted(false);
            b.setMargin(new Insets(0,0,0,0));
            panel.add(b);
        }
    
        public final JComponent getGui() {
            return gui;
        }
    
        public final JTextField getField() {
            return textField;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    ButtonsInTextField bitf = new ButtonsInTextField(20);
                    JOptionPane.showMessageDialog(null, bitf.getGui());
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }