Creating a fancy search bar with Swing components - java

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.

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.

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

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

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

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("");
}
});

Related

Adding Second Border to JButton

Here's the issue: When I try to add a border to a JButton via setBorder(), the normal background styling of the button:
disappears, to be replaced by what is essentially a clickable JLabel:
Basically, what I'd like to do is add a colored border around the current default border. If there's another process other than using setBorder() that would work, I would be interested in hearing about it.
Also, I should add that I cannot subclass or override methods of the graphics elements, as this needs to be inserted as a standalone tool in a far larger code repository.
Edit: Specifying question better
You should create a compound border. You can do this:
JButton myButton = new JButton("BUTTON TEXT");
myButton.setBorder(BorderFactory.createCompoundBorder(myButton.getBorder(), BorderFactory.createLineBorder(Color.RED));
This will preserve the look/feel of the button and will add a red border.

JComponent Titled Border

I am making a simple swing application and I want to add some titled borders to my components. The border on both of my JScrollPanes work fine, but the JTextField and the JButtons don't. Allow me to share some screen shots.
I just have simple code for this. i.e
TitledBorder border = new TitledBorder("Border");
convert.setBorder(border); //convert is the JButton
I don't see why it would not work for one thing, and work for the other. Can anyone help me out?
A JTextField and JButton both use a Border already. So the titled border works but it changes the appearance of the component because you lose the default Border.
I also agree that normally you don't use a TitledBorder for an individual component but I suppose you could try to use a CompoundBorder to see if it looks any better:
CompoundBorder border = new CompoundBorder(titledBorder, button.getBorder());
button.setBorder( border );
but then the problem with the above approach is that you lose dynamic repainting of the border when you press/release the mouse on the button.

Making the board GUI for a Chinese Chess program

I'm writing a Chinese Chess program in Java and would much appreciate some guidance on designing/implementing the GUI.
The board is to be divided into a 9x10 grid, with an "image" of the chess piece occupying each cell. The cells also need to be 'registered' when clicked so I know which piece was clicked.
1) I'm thinking GridLayout for the layout manager for the JPanel representing the board. How do I add an image to each component?
public void paintComponent(Graphics g) {
Image dog = new ImageIcon("dog.png").getImage();
add(dog)
}
Does not work as dog is not a Component.
2) How would I register for clicks in each cell?
Use a JLabel containing an Icon. Then add the label to the grid layout. Read the Swing tutorial on How to Use Icons for more information.
Also read the section on How to Write a Mouse Listener for listening to clicks on the label.
Or you could use a JButton with an Icon and then use:
button.setBorderPainted(false);
so you don't see the action of clicking the button. Then you would use an ActionListener. The tutorial also has a section on using an ActionListener.
Yes, GridLayout seems appropriate for this use.
See the constructor JButton(Icon).
See this answer for an example that carves up an existing image tile set for use in JLabel and JButton instances.

Draw something on JButton's right and top corner

I am trying to create JButton such as there must be a number painted on the top and right corner of JButton.
For example, in case of notification buttons there is a message in the button, how is that possible? Can the help be taken of paint method to draw the label?
there are three ways, by using
GlassPane
JLayer (Java7) based on JXLayer(Java6)
JButtton (all JComponents) is container too, there is easy and possible use standard LayoutManagers (only JFrame == BorderLayout and JPanel == FlowLayout have got implemented LayoutManager in API directly), then basically everything is possible
JButton and any JComponent extend Container class, so you should be able to add elements into JButton as if it were a simple panel. So in your case you can add a JLabel with your text into a button.
Also consider implementing Icon to decorate the button; ColorIcon is a simple example. You can use the color to signify buttons that are likely to need attention, and you can use drawString() specify a number.

applet button filling area

I'm creating an applet and having problems with the positioning as size of my buttons. I've added two buttons, but the "OK" button seems to position and size itself correctly, but the "CLEAR" button fills the entire applet area behind the "OK" button. Any suggestions as to what is the problem?
#Override
public void init()
{
super.init();
setSize(J_WIDTH, J_HEIGHT);
setLayout(new BorderLayout());
btn_OK = new Button("OK");
btn_CLEAR = new Button("CLEAR");
btn_OK.setBounds(50, 450, 75, 50);
btn_CLEAR.setBounds(125, 50, 75, 50);
add(btn_OK);
add(btn_CLEAR);
btn_OK.addActionListener(this);
btn_CLEAR.addActionListener(this);
}
When using a BorderLayout, you should specify a location where you want to place the component. If you don't, the default is BorderLayout.CENTER. Also, each position can only contain one component. So when you call add(btn_OK), the OK button is added to the center of the panel. But then you replace it with the Clear button by calling add(btn_CLEAR);.
In addition, each position in the BorderLayout takes up a certain amount of space. The component at that position will stretch to fill that space. In particular, the CENTER takes up all remaining space not used by the other positions.
I think that BorderLayout is not what you want here. Check out the Visual Guide to Layout Managers for more information on each LayoutManager. You can also follow the rest of the tutorial trail for details about how to implement each of them.
You should also bookmark and familiarize yourself with the Java API docs. These are an essential tool for every Java programmer and will help you answer many questions on your own.

Categories

Resources