问题描述
我是JAVA的新手,我正在尝试制作JTable.我想要的是,每当用户从Jtable的comboBox中进行选择时,Jtable都会将当前日期放在comboBox旁边的单元格中.我编写了代码,但是代码无法更新单元格的值.这是我的代码(我现在只想让Jtable在第1行第4列插入"a").
I am new to JAVA and I am trying to make a JTable. What I want is that whenever a user make a selection from the comboBox in the Jtable, the Jtable will put the current date in the cell next to the comboBox. I wrote a code, but the code cannot upate the cell value. Here is my code(I just want the Jtable to insert "a" at row 1 column 4 for right now).
public class GChamber extends JPanel {
private boolean DEBUG = true;
ListSelectionModel listSelectionModel;
public GChamber() {
...
JTable table1 = new JTable(new Table1());
listSelectionModel = table1.getSelectionModel();
table1.setSelectionModel(listSelectionModel);
...
TableColumn TestName=table1.getColumnModel().getColumn(3);
final JComboBox comboBox= new JComboBox();
//Setup comboBox. The data of comboBox is from another Jtable.
for(int i=0;i<table2rowlength;i++)
{
comboBox.addItem(Listofdiease[i]);
}
comboBox.addActionListener(new ActionListener()
TestName.setCellEditor(new DefaultCellEditor(comboBox));
{
@Override
public void actionPerformed(ActionEvent e) {
String userinput = (String)comboBox.getSelectedItem();
Table1 temp=new Table1();
for(int i=0;i<table2rowlength;i++)
{
if (userinput.equals(Listofdiease[i])) {
temp.setValueAt("a", 1, 4);
}
}
}
});
....
}
public class Table1 extends AbstractTableModel {
String[] cName1 = {"1","2","3","4","5", "6"};
Object[][] data1 = {
{"CC040-2", new Integer(1),"", "","",""},
{"CC040-2", new Integer(2),"Rowing", "", "",""},
{"CC040-2", new Integer(3),"Knitting", "", "",""},
{"CC040-2", new Integer(4),"Speed reading", "", "",""},
{"CC040-2", new Integer(5),"Pool", "", "",""},
{"CC040-2", new Integer(6),"Pool", "", "",""},
{"CC040-2", new Integer(7),"Pool", "", "",""},
{"CC040-2", new Integer(8),"Pool", "", "",""},
{"CC040-2", new Integer(9),"Pool", "", "",""},
{"CC040-2", new Integer(10),"Pool", "", "",""},
{"CC040-2", new Integer(11),"Pool", "", "",""},
{"CC040-2", new Integer(12),"Pool", "", "",""},
{"CEA003", new Integer(13),"Pool","", "",""},
{"CEA003", new Integer(14),"Pool", "", "",""},
{"CEA003", new Integer(15),"Pool", "", "",""},
{"CEA003", new Integer(16),"Pool", "", "",""},
{"CEA004", new Integer(17),"Pool", "", "",""},
{"CEA004", new Integer(18),"Pool", "", "",""},
{"CEA004", new Integer(19),"Pool", "", "",""},
{"CEA004", new Integer(20),"Pool", "", "",""},
};
public int getColumnCount() {
return cName1.length;
}
public int getRowCount() {
return data1.length;
}
public String getColumnName(int col) {
return cName1[col];
}
public Object getValueAt(int row, int col) {
return data1[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data1[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data1[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
public void addTableModelListener(TableModelListener l) {
}
}
有没有人可以告诉我为什么此代码不起作用以及如何解决?
Is there anyone can tell me why this code does not work and how to fix it?
推荐答案
您也许可以在此处.如果从属列是不可不可编辑的,则只需根据组合选择从TableModel
返回所需的结果.如果从属列 是可编辑的,请使用此处所示的任一方法.
You may be able to use one of the approaches suggested here. If the dependent column is not editable, simply return the desired result from your TableModel
based on the combo selection. If the dependent column is editable, use either approach shown here.
这篇关于如何让JTable侦听用户的选择并更新单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!