如何将 JComboBox 添加到 JTable 单元格?

发布于 2024-08-17 22:22:05 字数 73 浏览 2 评论 0原文

我正在尝试将 JComponent 添加到 JTable 单元格。我应该实现 CellRenderer 还是 CellEditor?

I am trying to add JComponents to JTable Cells. Do I Implement CellRenderer or CellEditor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-08-24 22:22:05

您需要的是一个自定义编辑器,它将返回 JComboBox(或您想要使用的任何组件)。您应该查看 Sun JTable 教程,它包含一个有关如何使用 JComboBox 作为编辑器的示例。如果您也想使用 JComboBox 作为渲染器,本教程也适用。

What you need is a custom editor which will return the JComboBox (or whatever component you want to use). You should check the Sun tutorial for JTable, it contains an example on how to use a JComboBox as an editor. If you want to use JComboBox as a renderer as well, the tutorial applies to that too.

渡你暖光 2024-08-24 22:22:05

您还可以使用 DefaultCellEditor 来完成此操作通过将 JComboBox(或 JCheckBox 或 JTextField)的实例传递给构造函数。

You could also do it with the DefaultCellEditor by passing in an instance of a JComboBox (or JCheckBox or JTextField) to the constructor.

花开浅夏 2024-08-24 22:22:05

1- 创建一个 JCombobox 并向其中插入所需的信息,如下所示:

JComboBox<String> sport = new JComboBox<String>();
sport.addItem("foot");
sport.addItem("hand bool");
sport.addItem("****");

2- 创建一个 JTable 并为此表设置表格模式,如下所示:

Vector<String> title = new Vector<String>
title.add("id");
title.add("sport");
Vector<Vector<String>> rows = new Vector<Vector<String>>();
rows.addItem("1");
rows.addItem("2");

JTable table = new JTable(rows, title);

3-您可以将 JComboBox 放入 JTable Cells 中,如下所示:

table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(sport));

1- Create a JCombobox and insert into it the information you want, like this:

JComboBox<String> sport = new JComboBox<String>();
sport.addItem("foot");
sport.addItem("hand bool");
sport.addItem("****");

2- Create a JTable and set a Table Mode to this table, something like:

Vector<String> title = new Vector<String>
title.add("id");
title.add("sport");
Vector<Vector<String>> rows = new Vector<Vector<String>>();
rows.addItem("1");
rows.addItem("2");

JTable table = new JTable(rows, title);

3- You put the JComboBox in JTable Cells like this:

table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(sport));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文