首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

JTable -列中每个单元格的不同ComboBox

JTable是Java Swing库中的一个组件,用于显示和编辑表格数据。它提供了一种方便的方式来展示和操作数据,包括对单元格的编辑、排序、过滤等功能。

针对问题中的需求,即JTable中每个单元格的列都包含不同的ComboBox,可以通过自定义TableCellEditor来实现。TableCellEditor是一个接口,用于定义单元格编辑器的行为。可以创建一个继承自DefaultCellEditor的自定义编辑器,并在其中设置ComboBox作为编辑器的组件。

以下是一个示例代码,演示如何在JTable的列中使用不同的ComboBox:

代码语言:txt
复制
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

public class JTableComboBoxExample {
    public static void main(String[] args) {
        // 创建JTable对象
        JTable table = new JTable();

        // 创建表格数据模型
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Column 1");
        model.addColumn("Column 2");

        // 添加数据行
        model.addRow(new Object[]{"Value 1", "Value 2"});
        model.addRow(new Object[]{"Value 3", "Value 4"});

        // 设置数据模型
        table.setModel(model);

        // 创建ComboBox数组,每个列对应一个ComboBox
        JComboBox<String>[] comboBoxes = new JComboBox[table.getColumnCount()];

        // 创建并设置每个列的单元格编辑器
        for (int i = 0; i < table.getColumnCount(); i++) {
            comboBoxes[i] = new JComboBox<>(new String[]{"Option 1", "Option 2", "Option 3"});
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setCellEditor(new DefaultCellEditor(comboBoxes[i]));
        }

        // 显示表格
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }
}

在上述示例中,我们创建了一个JTable对象,并使用DefaultTableModel作为数据模型。然后,我们添加了两行数据,并为每个列创建了一个ComboBox作为单元格编辑器。最后,我们将表格显示在一个JFrame中。

这样,每个列中的单元格都包含一个不同的ComboBox,用户可以通过下拉列表选择不同的选项。

腾讯云提供了一系列云计算相关的产品,例如云服务器、云数据库、云存储等。具体推荐的产品和产品介绍链接地址可以根据实际需求来选择,可以参考腾讯云官方网站获取更详细的信息。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

jTable插件辅助资料

==============================================jTable插件================================================ 【】引入jtable <link rel="stylesheet" type="text/css" href="../jtable/themes/lightcolor/blue/jtable.min.css" /> <script type="text/javascript" src="../jtable/jquery.jtable.min.js"></script> <script type="text/javascript" src="../jtable/localization/jquery.jtable.zh-CN.js"></script> 注:jTable插件需要jquery UI插件。之前要引入jQuery和jQueryUI 【】Servlet生成JSON结果 collegeList=collegeBusiness.getListByAll(); //定义数据返回JSON map Map<String, Object> jsonMap = new HashMap<String, Object>(); jsonMap.put("Result", "OK"); jsonMap.put("Records", collegeList); JSONObject result=JSONObject.fromObject(jsonMap); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); out.println(result.toString()); out.flush(); out.close(); 【】jtable要求的返回格式 {  "Result":"OK",  "Records":[   {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"},   {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"},   {"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"},   {"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"}  ] } 【】当出现异常后的jTable要求的结果 {    "Result":"ERROR",    "Message":"异常信息字符串" } 【】jTable的语法  $('#MyTableContainer').jtable({             //General options comes here             actions: {                 //Action definitions comes here             },             fields: {                 //Field definitions comes here             }             //Event handlers... });      【】jtable初始化 1.定义jTable显示的区域div

2.在JS中初始化jTable //定义部门表格 $('div#departmentmaincontent').jtable({            title: '部门列表',            selecting: true, //Enable selecting            multiselect: false, //not Allow mu

04
领券