How to add to Abstracttablemodel a combobox populated by database information?

Asked

Viewed 110 times

-1

I would like to know if it is possible to implement a combobox in a AbstractTableModel so that it receives information directly from the database for filling. if possible someone could give me an example?

As I always have DefaultTable, I don’t understand much about AbstractTableModel, and hence the question.

1 answer

1

AbstractTableModel is an abstract implementation that serves as a basis for creating a "modeling" of data in a JTable, has nothing to do with combos.

To model data in Jcomboboxes, you can use the class DefaultComboBoxModel, which is also an implementation of data modeling, however it is concrete and usually meets most needs when using combos, especially when filling with primitive types and strings.

If you need something more specific that this implementation won’t solve, you need to create your own implementation, or overwrite the implementation of the method toString() of the class that will represent the object that will popularize the combo, depends on the complexity of what is wanted to do.

In the links below are some examples of implementation:

I often use a generic implementation that I created, so I don’t need to implement a model for each type of created object that I will fill in a combo, but for it to work properly, your bean needs to have the method toString() superscript. At this link there is an example of implementing it.


Rereading the question, I believe that the doubt may also be related to how to add a combo to the editor of some cell/column in a table, and for that, you will not use models, because it is not a change of the fill template, but rather the display of cells in the table, through the interface TableCellRenderer, which contains the methods necessary for you to create a custom way of rendering the cells, and the interface TableCellEditor, which contains the methods needed to create a custom way to render the cell when entering edit mode. If you want to read more about it, I recommend the official documentation tutorials: How to Use Tables and How to Use Combo Boxes.

Browser other questions tagged

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