[英]How to add a JComboBox to a JTable cell?
我正在嘗試將JComponents添加到JTable Cells。 我是否實施CellRenderer或CellEditor?
您也可以通過將JComboBox(或JCheckBox或JTextField)的實例傳遞給構造函數來使用DefaultCellEditor執行此操作。
1-創建一個JCombobox
並在其中插入您想要的信息,如下所示:
JComboBox<String> sport = new JComboBox<String>();
sport.addItem("foot");
sport.addItem("hand bool");
sport.addItem("****");
2-創建JTable
並將表模式設置為此表,如:
Vector<String> title = new Vector<String>
title.add("id");
title.add("sport");
Vector<Vector<String>> rows = new Vector<Vector<String>>();
rows.addItem("1");
rows.addItem("2");
JTable table = new JTable(rows, title);
3-您將JComboBox放在JTable單元格中,如下所示:
table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(sport));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.