1

I'm trying to load a ComboBox with different values for each column cell into my JTable but I can't find any way to implement this. This code I have for the first column is as follows:

Database database = Database.getInstance();
ResultSet resultSet = database.query("SELECT Titel from Serie");

while(resultSet.next())
{
    comboBox.addItem(resultSet.getString("Titel"));
}

seriesColumn.setCellEditor(new DefaultCellEditor(comboBox));

And depending on the serie name that is returned, a new query is executed to get all the episodes of the serie, per serie. So they would all be different. Here are the images to give some idea what I mean:

First column (serie)

Second column (Episode)

The second column should now contain the episodes according to the serie of the first column but they're all the same.

Any help would be much appreciated!

Mihael Keehl
  • 335
  • 5
  • 16
  • 1
    Check out: https://stackoverflow.com/questions/4211452/how-to-add-unique-jcomboboxes-to-a-column-in-a-jtable-java/4211552#4211552 for one approach. – camickr Jan 19 '19 at 16:36

1 Answers1

2

Main part of this sample program is the use of custom cell editor EpisodeEditor. It dynamically decides the "episodes" based on the "series" selected in first column.

(I have used a mock data source for this demonstration.)

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.util.*;

public class ComboBoxTable
{
  public static void main(String[] args)
  {
    // Mock data source
    DataSource dataSource = new DataSource();

    JComboBox<String> seriesComboBox = new JComboBox<>();
    for (String s : dataSource.getSeries())
    {
      seriesComboBox.addItem(s);
    }

    JTable table = new JTable(
        new String[][] {{"", ""}, {"", ""}, {"", ""}},
        new String[] {"Series", "Episode"});
    table.getColumn("Series").setCellEditor(new DefaultCellEditor(seriesComboBox));
    table.getColumn("Episode").setCellEditor(new EpisodeEditor(dataSource));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(table));
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}

class EpisodeEditor extends AbstractCellEditor implements TableCellEditor
{
  private DataSource dataSource;
  private JComboBox<String> episodesComboBox = new JComboBox<>();

  EpisodeEditor(DataSource dataSource)
  {
    this.dataSource = dataSource;
  }

  @Override
  public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
                                                        int row, int column)
  {
    String series = (String) table.getModel().getValueAt(row, 0);

    List<String> episodes = dataSource.getEpisodes(series);
    episodesComboBox.removeAllItems();
    for (String ep : episodes)
    {
      episodesComboBox.addItem(ep);
    }
    episodesComboBox.setSelectedItem(value);
    return episodesComboBox;
  }

  @Override
  public Object getCellEditorValue()
  {
    return episodesComboBox.getSelectedItem();
  }
}

class DataSource
{
  List<String> getSeries()
  {
    return Arrays.asList("Prison Break", "Breaking Bad", "Pokemon");
  }

  List<String> getEpisodes(String series)
  {
    switch (series)
    {
      case "Prison Break":
        return Arrays.asList("Break 1", "Break 2", "Break 3");
      case "Breaking Bad":
        return Arrays.asList("Bad 1", "Bad 2", "Bad 3");
      case "Pokemon":
        return Arrays.asList("P1", "P2", "P3");
      default:
        throw new IllegalArgumentException("Invalid series: " + series);
    }
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • You're a genius! Thanks a lot, I didn't think it would change the value on selecting a new value afterwards but it somehow works after implementing it. – Mihael Keehl Jan 19 '19 at 18:19