[英]Use a JCheckBox as DefaultCellEditor for my JTable's cells
I am using this function to create a jTable
, where some special cells have a jComboBox
as editor : 我正在使用此函数创建一个jTable
,其中一些特殊的单元格具有一个jComboBox
作为编辑器:
void fillTable_(){
final JComboBox myEditor = new JComboBox(new String[] {"yes", "no", "maybe"});
String[][] data = new String[10][2];
data[0][0] = "0,0";
data[0][1] = "0,1";
data[1][0] = "1,0";
data[1][1] = "1,1";
data[2][0] = "2,0";
data[2][1] = "2,1";
String[] columnNames = {"Nom", "Valeur"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
jTable1 = new JTable(model){
DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);
@Override
public TableCellEditor getCellEditor(int row, int column){
int modelColumn = convertColumnIndexToModel(column);
int modelRow = convertRowIndexToModel(row);
if(modelColumn == 1 && modelRow == 1){
return myCellEditor;
} else {
return super.getCellEditor(row, column);
}
}
};
}
But the jTable stays empty not even plain text display . 但是jTable保持为空, 甚至不显示纯文本 。 Is there something wrong? 有什么不对?
But the
JTable
stays empty; 但是JTable
保持为空; not even plain text is displayed. 甚至不会显示纯文本。
You need a renderer that corresponds to your editor, as suggested in this example that contains both a ValueRenderer
and a ValueEditor
. 您需要一个与您的编辑器相对应的渲染器,如本示例中所建议的那样,该渲染器同时包含ValueRenderer
和 ValueEditor
。
I just need to put a
JComboBox
in some cells of theJTable
: 我只需要在JTable
某些单元格中放置一个JComboBox
:
As discussed in How to Use Tables: Concepts: Editors and Renderers , "a single cell renderer is generally used to draw all of the cells that contain the same type of data." 如“ 如何使用表:概念:编辑器和渲染器 ”中所述,“通常使用单个单元格渲染器绘制包含相同数据类型的所有单元格。”
I specified both row and column, but nothing is happening 我同时指定了行和列,但没有任何反应
Your code appears to work when I add the new JTable
to a container. 当我将新的JTable
添加到容器中时,您的代码似乎可以正常工作。
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
/** @see http://stackoverflow.com/questions/7200500 */
public class TableTest extends JPanel {
public TableTest() {
this.setPreferredSize(new Dimension(320, 240));
final JComboBox myEditor = new JComboBox(
new String[]{"yes", "no", "maybe"});
String[][] data = new String[10][4];
data[0][0] = "0,0";
data[0][5] = "0,1";
data[1][0] = "1,0";
data[1][6] = "1,1";
data[2][0] = "2,0";
data[2][7] = "2,1";
String[] columnNames = {"Nom", "Valeur"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model) {
DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);
@Override
public TableCellEditor getCellEditor(int row, int column) {
int modelColumn = convertColumnIndexToModel(column);
int modelRow = convertRowIndexToModel(row);
if (modelColumn == 1 && modelRow == 1) {
return myCellEditor;
} else {
return super.getCellEditor(row, column);
}
}
};
this.add(table);
}
private void display() {
JFrame f = new JFrame("TableTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TableTest().display();
}
});
}
}
1) create JTable , TableModel , JComboBox , then put that together as example from official tutorial 1)创建JTable , TableModel , JComboBox ,然后将其汇总为官方教程中的示例
2) for Boolean Component isn't required create own TableCellEditor , JTable by default supporting String, Integer, Double, Boolean, Date and Icon/ImageIcont in the TableCell 2)对于布尔组件,不需要创建自己的TableCellEditor ,默认情况下,JTable在TableCell中支持String,Integer,Double,Boolean,Date和Icon / ImageIcont
another examples for am... here , here from this forum here , here , and here 另一个例子为上午...... 在这里 , 在这里从这个论坛在这里 , 在这里 ,并在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.