defectivehalt
defectivehalt

Reputation: 2532

Creating a fancy search bar with Swing components

I'm trying to come up with an elegant recreation of the search bar component in Thunderbird. The clear button doesn't appear until there is text in the box, so that screen-shot is a bit inaccurate.

Should I use a layered pane and get some buttons to float above the box? (but then getting the text to not appear below the buttons would be hacky)

Maybe just put buttons at the ends of the search bar and have it somehow blend in?

Any ideas or maybe a style reconsideration is welcome, thank you.

Upvotes: 2

Views: 8298

Answers (5)

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

This code adds a label with given icon to the right of the JTextPane. One thing to work on: don't let the text go under the label. You can use setMargin(), but it shifts the label too.

    JTextField searchField = new JTextField(30);
    searchField.setLayout(new BorderLayout());
    JLabel label = new JLabel(icon);
    label.setCursor(Cursor.getDefaultCursor());
    searchField.add(label, BorderLayout.LINE_END);
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            searchField.setText("");
        }
    });

Upvotes: 0

camickr
camickr

Reputation: 324108

You might be able to use the Text Prompt for the "Search all text" display.

Upvotes: 1

01es
01es

Reputation: 5410

For building a very similar component I've used JXLayer (for drawing the buttons) in conjunction with IntelliHints from JIDE OSS project (for implementing a drop down list of values).

Upvotes: 0

I82Much
I82Much

Reputation: 27326

Check out JideSoft's Common Layer and the Overlayable class.

Demos

Upvotes: 0

StanislavL
StanislavL

Reputation: 57381

What about a white panel with a border and a JTextField without borders inside. Two buttons (or more) in the west and east. Button will appear/hide depending on the text field content.

Upvotes: 2

Related Questions