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

如何在Swing的JTable的列中添加不同的JComboBox项

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

如何在Swing的JTable的列中添加不同的JComboBox项

java2s.com上的示例看起来可以正常工作,然后再进行示例(例如,我将JComboBoxes编码为快速示例,并为今天的Swing添加/更改)

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.UIManager.LookAndFeelInfo;import javax.swing.table.*;public class EachRowEditorExample extends Jframe {    private static final long serialVersionUID = 1L;    public EachRowEditorExample() {        super("EachRow Editor Example");        try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {     System.out.println(info.getName());     if ("Nimbus".equals(info.getName())) {         UIManager.setLookAndFeel(info.getClassName());         break;     } }        } catch (UnsupportedLookAndFeelException e) { // handle exception        } catch (ClassNotFoundException e) { // handle exception        } catch (InstantiationException e) { // handle exception        } catch (IllegalAccessException e) { // handle exception        }        DefaultTableModel dm = new DefaultTableModel();        dm.setDataVector(new Object[][]{{"Name", "MyName"}, {"Gender", "Male"}, {"Color", "Fruit"}}, new Object[]{"Column1", "Column2"});        JTable table = new JTable(dm);        table.setRowHeight(20);        JComboBox comboBox = new JComboBox();        comboBox.addItem("Male");        comboBox.addItem("Female");        comboBox.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent e) {     final JComponent c = (JComponent) e.getSource();     SwingUtilities.invokeLater(new Runnable() {         @Override         public void run() {  c.requestFocus();  System.out.println(c);  if (c instanceof JComboBox) {      System.out.println("a");  }         }     }); }        });        JComboBox comboBox1 = new JComboBox();        comboBox1.addItem("Name");        comboBox1.addItem("MyName");        comboBox1.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent e) {     final JComponent c = (JComponent) e.getSource();     SwingUtilities.invokeLater(new Runnable() {         @Override         public void run() {  c.requestFocus();  System.out.println(c);  if (c instanceof JComboBox) {      System.out.println("a");  }         }     }); }        });        JComboBox comboBox2 = new JComboBox();        comboBox2.addItem("Banana");        comboBox2.addItem("Apple");        comboBox2.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent e) {     final JComponent c = (JComponent) e.getSource();     SwingUtilities.invokeLater(new Runnable() {         @Override         public void run() {  c.requestFocus();  System.out.println(c);  if (c instanceof JComboBox) {      System.out.println("a");  }         }     }); }        });        EachRowEditor rowEditor = new EachRowEditor(table);        rowEditor.setEditorAt(0, new DefaultCellEditor(comboBox1));        rowEditor.setEditorAt(1, new DefaultCellEditor(comboBox));        rowEditor.setEditorAt(2, new DefaultCellEditor(comboBox2));        table.getColumn("Column2").setCellEditor(rowEditor);        JScrollPane scroll = new JScrollPane(table);        getContentPane().add(scroll, BorderLayout.CENTER);        setPreferredSize(new Dimension(400, 120));        setLocation(150, 100);        pack();        setVisible(true);    }    public static void main(String[] args) {        EachRowEditorExample frame = new EachRowEditorExample();        frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) {     System.exit(0); }        });    }}

只需添加EachRowEditor类



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

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

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