Java JTable-仅在一个单元格中呈现复选框
我正在尝试使用JTable作为属性编辑器。我希望在单个列中使用不同类型的JComponents 到目前为止,我可以在属性有布尔值时显示复选框。但是,我无法使该复选框可单击并相应地设置值。它只是显示在单元格中,但当我单击它时,它的值变成字符串Java JTable-仅在一个单元格中呈现复选框,java,swing,jtable,jcheckbox,tablecelleditor,Java,Swing,Jtable,Jcheckbox,Tablecelleditor,我正在尝试使用JTable作为属性编辑器。我希望在单个列中使用不同类型的JComponents 到目前为止,我可以在属性有布尔值时显示复选框。但是,我无法使该复选框可单击并相应地设置值。它只是显示在单元格中,但当我单击它时,它的值变成字符串 import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.event.TableModelEvent; import javax.swing.event.TableMod
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class Main extends JFrame {
public static void main(String[] args) {
Main main = new Main();
}
public Main() {
this.setBounds(55, 5, 400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
this.setVisible(true);
this.validate();
}
private void init() {
JTable table = new JTable() {
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
if (column == 1) {
if (row == 1) {
Class cellClass = getValueAt(row, column).getClass();
return getDefaultRenderer(Boolean.class);
}
}
return super.getCellRenderer(row, column);
}
};
table.setModel(new PropertyModel(new Property(true, 1234)));
getContentPane().add(table);
}
public class Property {
private Integer height;
private boolean visible;
public Property(boolean visible, Integer height) {
super();
this.visible = visible;
this.height = height;
}
public Integer getHeight() {
return height;
}
public boolean isVisible() {
return visible;
}
public void setHeight(Integer height) {
this.height = height;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
@Override
public String toString() {
return "Property [visible=" + visible + ", height=" + height + "]";
}
}
public class PropertyModel extends AbstractTableModel {
private String HEIGHT = "Height";
private String VISIBLE = "Visible";
private Property property;
private String[] columnNames = { "Name", "Value" };
private Object[][] data = { { HEIGHT, "", },
{ VISIBLE, new Boolean(false) } };
public PropertyModel(Property property) {
super();
this.property = property;
initializeData();
this.addTableModelListener(new CustomPropertyTable());
}
@Override
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int c) {
return columnNames[c];
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* Initializes the data as per the world object values.
*/
private void initializeData() {
data[0][1] = property.getHeight();
data[1][2] = property.isVisible();
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == 0) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
public class CustomPropertyTable implements TableModelListener {
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
String propertyName = (String) model
.getValueAt(row, column - 1);
if (propertyName.equals(HEIGHT)) {
String propertyValue = (String) model.getValueAt(row,
column);
property.setHeight(Integer.valueOf(propertyValue));
} else if (propertyName.equals(VISIBLE)) {
String propertyValue = (String) model.getValueAt(row,
column);
Boolean visible = Boolean.valueOf(propertyValue);
property.setVisible(visible);
}
System.out.println(property);
}
}
}
}
截图:
1) 开始的时候
2) 当我单击或尝试取消选中它时
我可能做错了什么?您可以使用与渲染器相同的方式提供编辑器。扩展
getCellEditor
。例如,根据发布的代码:
@Override
public TableCellEditor getCellEditor(int row, int column) {
if (column == 1) {
Object value = getValueAt(row, column);
if (value != null)
return getDefaultEditor(value.getClass());
}
return super.getCellEditor(row, column);
}
可以使用与渲染器相同的方式提供编辑器。扩展
getCellEditor
。例如,根据发布的代码:
@Override
public TableCellEditor getCellEditor(int row, int column) {
if (column == 1) {
Object value = getValueAt(row, column);
if (value != null)
return getDefaultEditor(value.getClass());
}
return super.getCellEditor(row, column);
}
如果有,真的不需要getColumnClass@mKorbelOP需要类似于属性表的行为,其中单个列可能具有不同的类型。例如,第一行是一个字符串,第二行是一个布尔值。aa我看到+1,感谢您的注意,我认为(非常确定同步)需要重写,如果存在,则实际上不需要getColumnClass@mKorbelOP需要类似于属性表的行为,其中单个列可能具有不同的类型。例如,第一行是一个字符串,第二行是一个布尔值。aa我看到+1,感谢您的注意,我认为(非常确定同步)需要重写