gpt4 book ai didi

java - JTable 单元格编辑器错误

转载 作者:行者123 更新时间:2023-12-01 17:37:03 26 4
gpt4 key购买 nike

我写了一个JTable的单元格编辑器。现在它有一个错误。我希望它在第一次加载时显示默认值。编辑完成后,会显示用户输入的内容。运行应用程序,您就会了解该错误。

如何解决?

谢谢。

package com.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class PropPageTest extends JPanel {

private static final long serialVersionUID = 1555593265606867986L;

public JTableX table;
public DefaultTableModel model;
private String[] col_names = { "Name", "Value" };
private String[] anchor_values = { "CENTER", "NORTH" };
public String[] default_value = {"John", "NORTH"};

public boolean isFirstTimeLoad = true;

private void createGUI() {
setLayout(new BorderLayout());

model = new DefaultTableModel(col_names, 2) {
private static final long serialVersionUID = 478700329843848400L;
public String[] prop_names = { "Name", "Anchor" };

public Object getValueAt(int row, int col) {
if (col == 0) return prop_names[row];
if(isFirstTimeLoad){
isFirstTimeLoad = false;
return default_value[row];
}
else {
return super.getValueAt(row, col);
}
}

public boolean isCellEditable(int row, int col) {
if (col == 0) return false;
return true;
}
};

table = new JTableX(model);

JTextField txtFld = new JTextField();
table.registerCellEditor(0, new DefaultCellEditor(txtFld));

JComboBox cb = new JComboBox(anchor_values);
table.registerCellEditor(1, new DefaultCellEditor(cb));

add(table, BorderLayout.CENTER);
}

public static void main(String[] args) {
JFrame f = new JFrame("test");
f.setSize(300, 350);
final PropPageTest test = new PropPageTest();
f.getContentPane().add(test, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
JButton saveBtn = new JButton("Save");
buttonPanel.add(saveBtn);
f.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
test.table.stopCellEditing();
String[] newArr = new String[2];
newArr[0] = test.model.getValueAt(0, 1)+"";
newArr[1] = test.model.getValueAt(1, 1)+"";
boolean isTrue = Arrays.equals(test.default_value, newArr);

System.out.println("new value --" + test.model.getValueAt(0, 1));
System.out.println("new value --" + test.model.getValueAt(1, 1));
System.out.println("is true --" + isTrue);
}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setLocationRelativeTo(null);
}

public PropPageTest() {
createGUI();
}
}

--另一个文件

package com.swing;


import java.util.HashMap;
import java.util.Set;

import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

public class JTableX extends JTable {
private static final long serialVersionUID = -7180009397735740833L;
private final HashMap<Integer, TableCellEditor> rowEditorMap;

public JTableX(TableModel tm) {
super(tm);
rowEditorMap = new HashMap<Integer, TableCellEditor>();
this.setRowSelectionAllowed(false);
this.setColumnSelectionAllowed(false);
}

public void registerCellEditor(int row, TableCellEditor editor){
rowEditorMap.put(row, editor);
}

public TableCellEditor getCellEditor(int row, int col) {
TableCellEditor tmpEditor = null;
if (rowEditorMap != null) tmpEditor = rowEditorMap.get(row);

if (tmpEditor != null) return tmpEditor;
return super.getCellEditor(row, col);
}

public void stopCellEditing() {
if (rowEditorMap != null) {
Set<Integer> keySet = rowEditorMap.keySet();
for (Integer i : keySet) {
TableCellEditor tmpEditor = rowEditorMap.get(i);
if (tmpEditor != null) {
tmpEditor.stopCellEditing();
}
}
}
}
}

最佳答案

你不能这么做。 JTable(或代码的任何其他部分)可以在需要时调用 getValueAt。该方法应该返回表实际包含的内容。如果您最初希望表中存在默认值,请将这些默认值存储在模型中。

当模型中的数据发生变化时,它必须触发 TableModelEvent。当调用 setValueAt 时,这是由 DefaultTableModel 自动完成的。

关于java - JTable 单元格编辑器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261824/

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