SWING中组件的使用

来源:互联网 发布:衣柜设计软件用哪款好 编辑:程序博客网 时间:2024/06/10 07:22

在表格中添加下拉列表:

Vector rowdata=new Vector();//建立一个表模型DefaultTableModel dtm=new DefaultTableModel(rowdata,columnNames);//通过表模型初始化一个表JTable table=new JTable(dtm);//向表中添加一行为空值的行dtm.addRow(new Vector());//初始化一个滚动面板JScrollPane jspanel = new JScrollPane();//将表添加到滚动面板中jspanel.add(table);//将表中滚动面板中设置可见jspanel.setViewportView(table);//初始化下拉列表JComboBox cb=new JComboBox(value1);//将表的第一列设置为下拉列表TableColumn column = table.getColumnModel().getColumn(0);//构造一个表单元格的默认编辑器DefaultCellEditor editor = new DefaultCellEditor(cb);//指定开始编辑所需的单击次数editor.setClickCountToStart(2);//设置编辑此列中单元格时所用的编辑器column.setCellEditor(editor);//将滚动面板添加到窗口中this.add(jspanel,BorderLayout.CENTER);

表中添加下拉列表如上图所示。


 重绘表,有刷新的效果,单是与之前的表模型不是同一个了。

//用新的行 Vector(dataVector)替换当前的 dataVector 实例变量。//dataVector表中的数据,columnIdentifiers表头dtm.setDataVector(dataVector, columnIdentifiers);


//将窗口设置为居中

  frame.setLocationRelativeTo(null);