Java JTable-列中每个单元格的不同组合框
我试图将每个列单元格具有不同值的组合框加载到我的JTable中,但我找不到任何实现方法。我对第一列的代码如下:Java JTable-列中每个单元格的不同组合框,java,swing,jtable,Java,Swing,Jtable,我试图将每个列单元格具有不同值的组合框加载到我的JTable中,但我找不到任何实现方法。我对第一列的代码如下: Database database = Database.getInstance(); ResultSet resultSet = database.query("SELECT Titel from Serie"); while(resultSet.next()) { comboBox.addItem(resultSet.getString("Titel")); } ser
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));
根据返回的剧集名称,将执行一个新的查询,以获取每个剧集的所有剧集。所以他们都会不一样。以下是一些图片,让您了解我的意思:
第二栏现在应该包含第一栏的系列,但它们都是一样的
任何帮助都将不胜感激 此示例程序的主要部分是使用自定义单元编辑器
eposodeEditor
。它根据第一列中选择的“系列”动态决定“情节”
(我在本演示中使用了模拟数据源。)
import javax.swing.*;
导入javax.swing.table.TableCellEditor;
导入java.util.*;
公共类ComboxTable
{
公共静态void main(字符串[]args)
{
//模拟数据源
DataSource DataSource=新数据源();
JComboBox seriesComboBox=新JComboBox();
对于(字符串s:dataSource.getSeries())
{
序列通讯盒附加项;
}
JTable表=新的JTable(
新字符串[][{{,“,”},{,“,”},{,“,”},
新字符串[]{“系列”,“插曲”});
table.getColumn(“Series”).setCellEditor(新的DefaultCellEditor(seriesComboBox));
table.getColumn(“插曲”).setCellEditor(新的插曲编辑器(数据源));
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 getContentPane().add(新的JScrollPane(表));
f、 立根(300200400300);
f、 setVisible(真);
}
}
类EpisodeEditor扩展AbstractCellEditor实现TableCellEditor
{
私有数据源;
私有JComboBox eposodescombobox=新JComboBox();
情节编辑器(数据源数据源)
{
this.dataSource=数据源;
}
@凌驾
public java.awt.Component getTableCellEditorComponent(JTable表、对象值、布尔值、,
整数行,整数列)
{
字符串系列=(字符串)table.getModel().getValueAt(行,0);
列表剧集=dataSource.getscents(系列);
字母组合框。removeAllItems();
用于(字符串ep:剧集)
{
附加项(ep);
}
集合选择项(值);
返回组合框;
}
@凌驾
公共对象getCellEditorValue()
{
返回eposodescombobox.getSelectedItem();
}
}
类数据源
{
列表getSeries()
{
返回数组.asList(“越狱”、“破坏”、“口袋妖怪”);
}
剧集列表(字符串系列)
{
开关(系列)
{
“越狱”案:
返回数组.asList(“断开1”、“断开2”、“断开3”);
“坏掉”一案:
返回数组.asList(“坏1”、“坏2”、“坏3”);
案例“口袋妖怪”:
返回数组。asList(“P1”、“P2”、“P3”);
违约:
抛出新的IllegalArgumentException(“无效序列:+序列”);
}
}
}
检查:一种方法。你是个天才!非常感谢,我不认为它会在以后选择一个新值时改变值,但它在实现后不知何故会起作用。
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);
}
}
}