这篇文章主要为大家展示了“在表格中Swing如何增加列表框”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“在表格中Swing如何增加列表框”这篇文章吧。
Swing增加下拉列表框
在表格的一列中Swing增加下拉列表框,首先获得列表对象,在设置表列编辑,将下拉列表框作为默认列编辑对象设置到表列编辑对象中
importjava.awt.BorderLayout; importjava.awt.Dimension; importjavax.swing.JFrame; importjavax.swing.JPanel; importjavax.swing.JScrollPane; importjava.awt.Rectangle; importjavax.swing.JTable; importjavax.swing.table.TableColumn; importjavax.swing.DefaultCellEditor; importjavax.swing.JComboBox; importjavax.swing.JCheckBox; publicclassJComboBoxSecondextendsJFrame{ JPanelcontentPane; JScrollPanejScrollPane1=newJScrollPane(); Object[][]objContent={{"Tom","Hanks","Java","24","true"},{"Ben","John", "Java","25","true"},{"Michael","Fred","Java","26","true"}, {"Steve","Bell","Java","27","true"}, {"Jim","Lan","Java","29","true"} }; Object[]objHead={"名字","姓氏","技能","年龄","是否属实"}; JTablejTable1=newJTable(objContent,objHead); JComboBoxjComBo=newJComboBox(); JCheckBoxjCheck=newJCheckBox(""); publicJComboBoxSecond(){ try{ setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); }catch(Exceptionexception){ exception.printStackTrace(); } } /** *Componentinitialization. * *@throwsjava.lang.Exception */ privatevoidjbInit()throwsException{ contentPane=(JPanel)getContentPane(); contentPane.setLayout(null); setSize(newDimension(467,401)); setTitle("FrameTitle"); jScrollPane1.setBounds(newRectangle(11,20,442,337)); jComBo.addItem("Java"); jComBo.addItem("Orcale"); jComBo.addItem("Linux"); jComBo.addItem("Unix"); jComBo.addItem("C#"); TableColumntableCol=jTable1.getColumnModel().getColumn(2); tableCol.setResizable(false); TableColumntableColCheck=jTable1.getColumnModel().getColumn(4); tableCol.setCellEditor(newDefaultCellEditor(jComBo)); tableColCheck.setCellEditor(newDefaultCellEditor(jCheck)); contentPane.add(jScrollPane1); jScrollPane1.getViewport().add(jTable1); } }
以上是“在表格中Swing如何增加列表框”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。