栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将唯一的JComboBoxes添加到JTable中的列(Java)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将唯一的JComboBoxes添加到JTable中的列(Java)

重写getCellEditor(…)方法。例如;

import java.awt.*;import java.awt.event.*;import java.util.List;import java.util.ArrayList;import javax.swing.*;import javax.swing.border.*;import javax.swing.table.*;public class TableComboBoxByRow extends Jframe{    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);    public TableComboBoxByRow()    {        // Create the editors to be used for each row        String[] items1 = { "Red", "Blue", "Green" };        JComboBox comboBox1 = new JComboBox( items1 );        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );        editors.add( dce1 );        String[] items2 = { "Circle", "Square", "Triangle" };        JComboBox comboBox2 = new JComboBox( items2 );        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );        editors.add( dce2 );        String[] items3 = { "Apple", "Orange", "Banana" };        JComboBox comboBox3 = new JComboBox( items3 );        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );        editors.add( dce3 );        //  Create the table with default data        Object[][] data =        { {"Color", "Red"}, {"Shape", "Square"}, {"Fruit", "Banana"}, {"Plain", "Text"}        };        String[] columnNames = {"Type","Value"};        DefaultTableModel model = new DefaultTableModel(data, columnNames);        JTable table = new JTable(model)        { //  Determine editor to be used by row public TableCellEditor getCellEditor(int row, int column) {     int modelColumn = convertColumnIndexToModel( column );     if (modelColumn == 1 && row < 3)         return editors.get(row);     else         return super.getCellEditor(row, column); }        };        JScrollPane scrollPane = new JScrollPane( table );        getContentPane().add( scrollPane );    }    public static void main(String[] args)    {        TableComboBoxByRow frame = new TableComboBoxByRow();        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );        frame.pack();        frame.setVisible(true);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号