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增加下拉列表框,希望对大家有用
【编辑推荐】