Java ArrayList toString在println中工作,但在textfield中不工作 table.getSelectionModel().addListSelectionListener(新建ListSelectionListener()){ public void值已更改(ListSelectionEvent事件){ String val1=table.getValueAt(table.getSelectedRow(),1.toString(); String val2=table.getValueAt(table.getSelectedRow(),2.toString(); String val3=table.getValueAt(table.getSelectedRow(),3.toString(); 字符串val4=table.getValueAt(table.getSelectedRow(),4.toString(); ArrayList行=新的ArrayList(); 行。添加(val1); 行。添加(val2); 行。添加(val3); 行。添加(val4); //新的KundeTab().kundeAuswahl.setText(row.toString()); //编辑:这就是解决方案 字符串listString=“”; 用于(字符串s:行){ listString+=s+“”; } KundeTab.kundeAuswahl.setText(列表字符串); //输出工作 System.out.println(row.toString()); } });
为什么我要在sysout中获得输出,而不是在textfield中? 我做错了什么 谢谢你的帮助 编辑:Java ArrayList toString在println中工作,但在textfield中不工作 table.getSelectionModel().addListSelectionListener(新建ListSelectionListener()){ public void值已更改(ListSelectionEvent事件){ String val1=table.getValueAt(table.getSelectedRow(),1.toString(); String val2=table.getValueAt(table.getSelectedRow(),2.toString(); String val3=table.getValueAt(table.getSelectedRow(),3.toString(); 字符串val4=table.getValueAt(table.getSelectedRow(),4.toString(); ArrayList行=新的ArrayList(); 行。添加(val1); 行。添加(val2); 行。添加(val3); 行。添加(val4); //新的KundeTab().kundeAuswahl.setText(row.toString()); //编辑:这就是解决方案 字符串listString=“”; 用于(字符串s:行){ listString+=s+“”; } KundeTab.kundeAuswahl.setText(列表字符串); //输出工作 System.out.println(row.toString()); } });,java,Java,为什么我要在sysout中获得输出,而不是在textfield中? 我做错了什么 谢谢你的帮助 编辑: new KundeTab()引用了另一个类。这门课上有一个JTextField kundeAuswahl。你不能用新的KundeTab做任何事情。我不知道其中一个是什么,但请尝试以下方式: table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void
new KundeTab()引用了另一个类。这门课上有一个JTextField kundeAuswahl。你不能用新的
KundeTab
做任何事情。我不知道其中一个是什么,但请尝试以下方式:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
String val1 = table.getValueAt(table.getSelectedRow(), 1).toString();
String val2 = table.getValueAt(table.getSelectedRow(), 2).toString();
String val3 = table.getValueAt(table.getSelectedRow(), 3).toString();
String val4 = table.getValueAt(table.getSelectedRow(), 4).toString();
ArrayList<String> row = new ArrayList<String>();
row.add(val1);
row.add(val2);
row.add(val3);
row.add(val4);
//new KundeTab().kundeAuswahl.setText(row.toString());
//EDIT: this was the solution
String listString = "";
for (String s : row) {
listString += s + " ";
}
KundeTab.kundeAuswahl.setText(listString);
// output works
System.out.println(row.toString());
}
});
因为它是一个列表,所以需要使用
row.get(index)指定要打印的元素代码>
如果要打印整个列表,请先将其正确转换为常规字符串,然后将其添加到文本字段:
KundeTab() tab = new KundeTab();
tab.kundeAuswahl.setText(row.toString());
mainPanel.add(tab); // or whatever you need to do to make it display
您正在创建KundeTab
的新实例,然后在新文本框中设置文本。然后,此文本框将被垃圾回收
现在,我假设这个方法在KundeTab
类中。在这种情况下,您只需调用:
String listString = "";
for (String s : row)
{
listString += s + "\t";
}
new KundeTab().kundeAuswahl.setText(listString);
我相信删除kundeAuswahl.setText(row.toString())代码>应该可以。我感觉你没有在正确的框中设置文本。kundeAuswahl是我的类KundeTab中的JTextField。这就是我要打印字符串的地方。但是为什么要在新的KundeTab
而不是现有的KundeTab
中打印?我想!但是我怎么能在不创建新文本字段的情况下访问文本字段呢?
KundeTab.this.kundeAuswahl.setText(row.toString());