Popular combobox within the jtable with database, how to proceed?

0

Currently I populate a normal combobox this way:

this.CadperfilLinha.removeAllItems();
try {
    Class.forName(Auxiliar.AcessoBanco.getDriver());
    Connection con = DriverManager.getConnection(Auxiliar.AcessoBanco.getUrl(), Auxiliar.AcessoBanco.getUser(), Auxiliar.AcessoBanco.getPass());;
    Statement Sent = con.createStatement();
    ResultSet rs = Sent.executeQuery("Select * from Linha");
    CadperfilLinha.addItem("Selecione...");
    while (rs.next()) {
        this.CadperfilLinha.addItem(rs.getString("LINHA"));
    }
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

But now I'm needing a popular one inside the jtable, but I'm not getting it. I have it mounted here by just pulling values typed inside the code and wanted it to use the database. Can someone give me a hand?

String[] values = new String[]{"Ativo", "Desativado"};

TableColumn col = jTable1.getColumnModel().getColumn(2);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {

    public MyComboBoxRenderer(String[] items) {
        super(items);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        setSelectedItem(value);
        return this;
    }
}

class MyComboBoxEditor extends DefaultCellEditor {

    public MyComboBoxEditor(String[] items) {
        super(new JComboBox(items));
    }
}
    
asked by anonymous 08.01.2018 / 13:38

1 answer

3

If you need a JComboBox popular with database data and want to isolate that fill, the best I can recommend is to create a ComboBoxModel and fill the database in the constructor. a ComboBoxModel is nothing more than a template to populate a JComboBox, when you create a combo and do combo.addItem() , what actually happens is the completion of a built-in default template created for the component, this default template is the DefaultComboBoxModel .

An example of how to create could be as below:

class MeuComboModel extends DefaultComboBoxModel {

    private ArrayList<String> itens;
    private Object itemSelected;

    public MeuComboModel() {
        this.itens = new ArrayList<>();
        this.itens.add("Option 1");
        this.itens.add("Option 2");
        this.itens.add("Option 3");
        this.itens.add("Option 4");
    }

    @Override
    public int getSize() {
        return itens.size();
    }

    @Override
    public Object getElementAt(int index) {
        return itens.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        this.itemSelected = anItem;

    }

    @Override
    public Object getSelectedItem() {
        return this.itemSelected;
    }
}

I've created a class inheriting from DefaultComboBoxModel so we need to set as little as possible. In this case I'm populating directly, but just assign the items coming from the bank in the list instead.

To assign the model to the combo, you can either start by passing an instance of it or define it later using the setModel() :

  • Method 1:

    JComboBox combo = new JComboBox(new MeuComboModel());
    
  • Method 2:

    seucombo.setModel(new MeuComboModel());
    

By that time, the combo is already populated, and if the items are strings or some primitive type of java, depending on the model your table uses, just pass that combo to an instance of class this.table.getColumnModel().getColumn(<<indice da coluna>>).setCellEditor(new DefaultCellEditor(comboBox));

With this, the combo is displayed correctly when its cell enters editing mode, the option also selected, as can be seen below:

Ifyou'dliketoreadmoreaboutit,Irecommendtheofficialdocumentationtutorials: How to Use Tables and How to Use Combo Boxes .

    
08.01.2018 / 23:04