java swing 中JTable实现指定单元格为下拉框
2019-11-05 16:01:57来源:博客园 阅读 ()
java swing 中JTable实现指定单元格为下拉框
利用自定义的CellEditor实现第四列第二行为下拉框,本列其余行为文本框 利用默认的DefaultCellEditor设置第五列整列为下拉框 package mypackage; import java.awt.BorderLayout;import java.awt.Component;
import java.awt.Dimension;
import java.util.EventObject; import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
/**
* 自定义celleditor实现
* 指定单元格设置下拉框,其他单元格设置文本框
*
*/
public class JBoxTestCell extends AbstractCellEditor implements
TableCellEditor {
int row;
private JTable table;
private JPanel p1;
private JComboBox jbox;
private Object val = null;
private JTextField jt_0=new JTextField();
private JTextField jt_2=new JTextField();
public JBoxTestCell() {
p1 = new JPanel(new BorderLayout());
p1.setPreferredSize(new Dimension(300, 30));
String[] interest1 = { "rap", "手指舞", "理发","冲天炮"};
jbox = new JComboBox(interest1);
jbox.setPreferredSize(new Dimension(200,25));
jbox.setSelectedIndex(0);
p1.add(jbox, BorderLayout.CENTER);
} @Override
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public void showSave() {
jbox.setVisible(true);
} public Object getCellEditorValue() {
if(row==1) {
String v1 = jbox.getSelectedItem().toString();
return v1;
}
String result="";
switch(this.row) {
case 0 : result = jt_0.getText().trim(); break;
case 2 : result = jt_2.getText().trim(); break;
}
return result;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
this.table = table;
this.row = row;
val = value;
JTextField result = new JTextField();
switch(this.row) {
case 0 : result = jt_0; break;
case 1 : return this.jbox;
case 2 : result = jt_2; break;
}
return result;
}
} package mypackage; import java.awt.Dimension;
import java.util.Vector; import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableColumnModel;
import java.awt.Font;
/**
* 测试类
* 实现第四列第二行为下拉框,其余行为文本框
* 第五列一列为下拉框
*/ public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 484, 361);
frame.getContentPane().add(panel);
frame.setVisible(true);
String[][] str= {{"小贾","13","160","帽子",""},{"阿P","16","165","rap",""},{"小李","19","158","做头发",""}};
String[] title= {"姓名","年龄","身高","爱好1","爱好2"};
JTable table = new JTable(str,title);//测试表格
table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
JScrollPane scrollPane_1 = new JScrollPane(table);//滚动组件
scrollPane_1.setBounds(0, 0, 484, 361);
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane_1);
JBoxTestCell jc = new JBoxTestCell();//第四列第二行为下拉框,其余行为文本框
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(3).setCellEditor(jc);
String[] str2 = { "rap", "手指舞", "理发","冲天炮"};
JComboBox jbox= new JComboBox(str2);
jbox.setSelectedIndex(0);
TableColumnModel col = table.getColumnModel();
col.getColumn(4).setCellEditor(new DefaultCellEditor(jbox));//设置第5列一列为下拉框
} } 效果图:
原文链接:https://www.cnblogs.com/jungledove/p/11801982.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash