在表格中Swing增加列表框

开发 后端
本文介绍在表格中Swing增加列表框,首先获得列表对象,在设置表列编辑,将下拉列表框作为默认列编辑对象设置到表列编辑对象中。

Swing增加下拉列表框

在表格的一列中Swing增加下拉列表框,首先获得列表对象,在设置表列编辑,将下拉列表框作为默认列编辑对象设置到表列编辑对象中

  1. importjava.awt.BorderLayout;  
  2. importjava.awt.Dimension;  
  3.  
  4. importjavax.swing.JFrame;  
  5. importjavax.swing.JPanel;  
  6. importjavax.swing.JScrollPane;  
  7. importjava.awt.Rectangle;  
  8. importjavax.swing.JTable;  
  9. importjavax.swing.table.TableColumn;  
  10. importjavax.swing.DefaultCellEditor;  
  11. importjavax.swing.JComboBox;  
  12. importjavax.swing.JCheckBox;  
  13.  
  14. publicclassJComboBoxSecondextendsJFrame{  
  15. JPanelcontentPane;  
  16. JScrollPanejScrollPane1=newJScrollPane();  
  17. Object[][]objContent={{"Tom","Hanks","Java","24","true"},{"Ben","John",  
  18. "Java","25","true"},{"Michael","Fred","Java","26","true"},  
  19. {"Steve","Bell","Java","27","true"},  
  20. {"Jim","Lan","Java","29","true"}  
  21. };  
  22. Object[]objHead={"名字","姓氏","技能","年龄","是否属实"};  
  23. JTablejTable1=newJTable(objContent,objHead);  
  24. JComboBoxjComBo=newJComboBox();  
  25. JCheckBoxjCheck=newJCheckBox("");  
  26. publicJComboBoxSecond(){  
  27. try{  
  28. setDefaultCloseOperation(EXIT_ON_CLOSE);  
  29. jbInit();  
  30. }catch(Exceptionexception){  
  31. exception.printStackTrace();  
  32. }  
  33. }  
  34.  
  35. /**  
  36. *Componentinitialization.  
  37. *  
  38. *@throwsjava.lang.Exception  
  39. */  
  40. privatevoidjbInit()throwsException{  
  41. contentPane=(JPanel)getContentPane();  
  42. contentPane.setLayout(null);  
  43. setSize(newDimension(467,401));  
  44. setTitle("FrameTitle");  
  45. jScrollPane1.setBounds(newRectangle(11,20,442,337));  
  46. jComBo.addItem("Java");  
  47. jComBo.addItem("Orcale");  
  48. jComBo.addItem("Linux");  
  49. jComBo.addItem("Unix");  
  50. jComBo.addItem("C#");  
  51.  
  52. TableColumntableCol=jTable1.getColumnModel().getColumn(2);  
  53. tableCol.setResizable(false);  
  54. TableColumntableColCheck=jTable1.getColumnModel().getColumn(4);  
  55. tableCol.setCellEditor(newDefaultCellEditor(jComBo));  
  56. tableColCheck.setCellEditor(newDefaultCellEditor(jCheck));  
  57. contentPane.add(jScrollPane1);  
  58. jScrollPane1.getViewport().add(jTable1);  
  59. }  

以上是Swing增加下拉列表框,希望对大家有用

【编辑推荐】

  1. SwingUtilities提供两个方法
  2. 浅谈Swing中的渲染器接口
  3. Swing组件中的渲染器Renderer
  4. 浅析Swing线程中的LookupEvent
  5. Swing任务在Swing线程中执行
责任编辑:佚名 来源: 电子工业出版社
相关推荐

2012-01-17 14:01:33

JavaSwing

2010-01-26 09:27:47

Android列表框

2009-07-16 08:53:03

Swing任务Swing线程

2012-01-17 14:09:54

JavaSwing

2024-02-27 09:24:50

PyQt6中列表框树形视图

2022-04-26 16:56:44

CISA漏洞攻击

2012-08-09 11:20:40

Swing

2023-01-04 07:46:10

单选按钮双击效果

2012-03-08 11:23:09

jQuery Mobi

2009-07-17 16:41:48

actionPerfoSwing

2011-05-04 15:21:20

swing

2020-02-12 12:28:29

python列表数据类型

2009-07-10 10:19:57

JFormattedTSwing

2009-07-14 16:07:59

调用JavaFX代码Swing程序

2009-07-17 16:49:18

lookandfeelSwing

2009-07-15 09:59:11

Metal观感Swing

2009-07-16 09:54:44

LookupEventSwing线程

2009-07-17 16:32:34

Timer对象Swing

2012-12-25 10:48:35

IBMdW

2009-08-06 17:34:27

地址栏控件C#记忆功能
点赞
收藏

51CTO技术栈公众号