2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3454162
  • 45
  • 1
  • 7
  • 2
    Show us your code. Without that it's hard to help you – Markus Apr 01 '14 at 05:51
  • What layout are you using??.. I guess you are not using setBounds() method correctly – Stunner Apr 01 '14 at 05:52
  • As @Markus said, Post your code..If possible attaching screenshot would be better... – Stunner Apr 01 '14 at 05:53
  • 1
    There's a border trick you can use, no code to hand, or you could just use the BuddySupport API from SwingLabs, SwingX library, for [example](http://stackoverflow.com/questions/20578568/java-swing-listen-an-action-in-a-text-field-of-a-form/20578601#20578601) and [example](http://stackoverflow.com/questions/21495886/adding-sign-into-the-textfield-when-the-user-enter-in-the-field/21496102#21496102) – MadProgrammer Apr 01 '14 at 05:55
  • Or [ComponentBorder](http://tips4java.wordpress.com/2009/09/27/component-border/) – MadProgrammer Apr 01 '14 at 05:59
  • sorry for late reply guys I just saw all your comments and answers..... I used BorderLayout ............ .......thanks to all of you guys ........... eitanfar's code helped me out ............... :) – user3454162 Apr 01 '14 at 19:03
  • well here is my code: JTextField field=new JTextField(); field.setSize(250, 30); field.setColumns(10); field.setLocation(5, 30); field.setLayout(new BorderLayout()); JButton button=new JButton("B"); button.setSize(50, 20); button.setCursor(Cursor.getDefaultCursor()); button.addActionListener(this); button.setBackground(Color.WHITE); button.setBounds(50, 50,50, 50); field.add(button,BorderLayout.EAST); – user3454162 Apr 01 '14 at 19:21

3 Answers3

5

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This is the best way to do this. Don't mess with inserting a button as a child component of a text field, it's very difficult to get it right, and has virtually no benefits. This answer is very complete and explanatory, I would have voted it up twice if I could :-) Way to go ! – ethanfar Apr 01 '14 at 06:37
0

As people have noted above, it might have helped to see the code, especially the Layout manager. However, you might try the following (if you haven't yet):

  1. Call setColumns http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html#setColumns(int)

  2. Call setPreferredSize /setMaximumSize/setMinimumSize depending on your layout manager. But I'd try to avoid this solution because it's pixel-level maintenance.

Regards

Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 01 '14 at 06:29
0

As an alternative solution you can use a Component Border, which allows you to use the button as a Border so it appears within the text field.

camickr
  • 321,443
  • 19
  • 166
  • 288