gpt4 book ai didi

java - JTable - 在一个单元格中呈现复选框

转载 作者:行者123 更新时间:2023-11-29 03:27:24 25 4
gpt4 key购买 nike

我正在尝试使用 JTable 作为属性编辑器。我想在单个列中使用不同类型的 JComponent。

到目前为止,只要属性具有 boolean 值,我就可以显示复选框。但是,我无法使该复选框可点击并相应地设置值。它只是显示在单元格中,但是当我点击它时,它的值变成了字符串。

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)什么时候开始

When it starts, it looks something like this

2) 当我点击或尝试取消选中它时

When I click or try to uncheck it, it becomes string

我做错了什么?

最佳答案

您可以像提供渲染器一样提供编辑器。扩展 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);
}

关于java - JTable - 在一个单元格中呈现复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com