Insert component without using setBounds

Asked

Viewed 331 times

1

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

I 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 didn’t want to be using the setBounds method because it’s complicated to be looking for the best position and size for jComboBox. If you do not use this method, the combo does not appear in my jPanel that I want.

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

  • You could show the creation and initialization of your jPanel3?

  • The point is, I didn’t boot anything, I just created new jForm and I’m dragging the components into the design

  • Right, right. But show more of your code, especially everything that involves the variable jPanel3.

  • I’m not using jPanel3 anywhere in the code :s I just tried to add it but it didn’t work :s

  • The solution to your problem (probably) boils down to one thing: the proper choice of a Layout Manager. But to be perfectly honest, I don’t think I quite understand your problem. Do you know how to use Lms? Do you know what you’re using? Your question is Como adicionar uma JComboBox em um JPanel sem usar o setBounds()? ?

  • I’ve never worked much with the netbeans swing, and I’m not really into the Layoutmanagers. But in short yes, I want to put a Jcombobox on my jPanel3 without being by setBounds

Show 1 more comment

1 answer

2

To add a Jcombobox without having to define the Bounds just use a Layout Manager which does not require them to be defined.

A modern and flexible Layout Manager is the Miglayout, it generates little code making it possible for you to make adjustments directly in your class rather than depending on the graphics editor (although it is better for you to depend on the graphics editor, until you know better the options that this LM offers you).

An example of a Jcombobox of the Autocompletejcombobox type 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");
    }
}
  • But with the Autocompletejcombobox class imported into my project, I can’t instead of adding a combo to my jPanel, apply the autocomplete action to a jComboBox that I’ve dragged into my design ?

  • @Hugomachado yes, but you must change the form of instantiation of the object. The reference variable may still be a Jcombobox, but the object must be a Autocompletejcombobox. Example: JComboBox<String> combo = new AutocompleteJComboBox(searchable);. That’s what you want?

  • Let’s imagine that I dragged a combobox to my design and put it as editable and called 'comboAutocomplete'. Valid me in my constructor do: comboAutoComplete = new Autocompletejcombobox(searchable); ?

  • @Hugomachado Yes! As long as your combo is of the type JComboBox<String>, for this you just change manually in your class.

  • Excuse my ignorance but I’m here to learn :) Is it possible for me to change the type of Combobox I’ve dragged into my frame in this case to a Combobox<String> ? if yes where I do it ?

  • @Hugomachado Yes, it is possible. When you drag your combobox to your application, a code will be created in your class, referring to the element you have dragged. But he usually creates the code JComboBox, and you must add the <String> in the code to stay JComboBox<String>. Also, you must exchange everything after the = (will probably be new JComboBox()), for the code I put up: new AutocompleteJComboBox(searchable).

  • I think I got it. I’ll try it this way, thanks for the help :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.