Insert component without using the 'setBounds'

1

I'm using a free design in my project and dragging into my jframe the components I want. However, I needed to use a jComboBox with autocomplete and I was guided by this site. Now I wanted to use the class 'AutocompleteJComboBox' and apply it to a jCombobox that I dragged into my frame. Is this possible?

I have tried to put this part of the code in the constructor:

List<String> myWords = new ArrayList<>();
        myWords.add("bike");
        myWords.add("car");
        myWords.add("cap");
        myWords.add("cape");
        myWords.add("canadian");
        myWords.add("caprecious");
        myWords.add("catepult");

        StringSearchable searchable = new StringSearchable(myWords);
        AutocompleteJComboBox combo = new AutocompleteJComboBox(searchable);
        //combo.setBounds(600, 200, 100, 25);
        jPanel3.add(combo);

The point is that I did not want to be using the setBounds method because it's complicated to be looking for the best position and size for the jComboBox. If you do not use this method, the combo does not appear in my jPanel.

Any suggestions on how to apply this autocomplete to a jComboBox that I dragged to my jFrame?

    
asked by anonymous 18.11.2014 / 16:04

1 answer

2

To add a JComboBox without having to define bounds, simply use Layout Manager that does not require them to be defined.

A modern and flexible Layout Manager is MigLayout , it generates little code making it possible for you to make the adjustments directly in your class when instead of relying on the graphical editor (although at first you'd better depend on the graphical editor, until you know the options that this LM offers you).

An example of a JComboBox of type AutoCompleteJComboBox inside a MigLayout:

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

public class Principal extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Principal frame = new Principal();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Principal() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[424px,grow]", "[40px]"));

        List<String> myWords = new ArrayList<>();
        myWords.add("bike");
        myWords.add("car");
        myWords.add("cap");
        myWords.add("cape");
        myWords.add("canadian");
        myWords.add("caprecious");
        myWords.add("catepult");

        StringSearchable searchable = new StringSearchable(myWords);
        AutocompleteJComboBox combo = new AutocompleteJComboBox(searchable);
        contentPane.add(combo, "cell 0 0,growx");
    }
}
    
18.11.2014 / 17:26