gpt4 book ai didi

java - 将表格模型上的选定项目转换为对象

转载 作者:行者123 更新时间:2023-12-01 15:10:00 24 4
gpt4 key购买 nike

我已经创建了组件,这些组件正在放入具有两列的表格模型中,如下所示。

if (!newAcList.isEmpty()) {
for (Acronym acc : newAcList) {
tableModel.addRow(new String[]{acc.getName(), acc.getDefinition()});

}
}

我需要的是,当用户在表格模型上选择一个项目时,它将该项目转换回我的首字母缩略词对象。我正在使用 Listselectionevent 监听器。

这是 valueChanged 选择事件``

            @Override
public void valueChanged(ListSelectionEvent e) {
String selectedAcData = null;
String selectDefData = null;

int[] selectedRow = accTable.getSelectedRows();
int[] selectedColumns = accTable.getSelectedColumns();

for (int i = 0; i < selectedRow.length; i++) {
// for (int j = 0; j < selectedColumns.length; j++) {
selectedAcData = (String) accTable.getValueAt(selectedRow[i], 0);
}
}

最佳答案

您可能想要创建一个实现首字母缩略词的 TableModel 接口(interface)的类。它可能被称为 AcronymTableModel并得到 List<Acronym> 的支持缩略词列表。然后把这个模型放到你的 table 上。

调用accTable.getValueAt(selectedRow[i], 0);在你的valueChanged然后方法将返回 Acronym 的实例。

这是一个简单的例子。

    public class Example {

public static void main(String [] a) {
JFrame f = new JFrame();

JPanel p = new JPanel();

List<Acronym> acronyms = new ArrayList<Acronym>();
acronyms.add(new Acronym("FBI", "Federal Bureau of Investigation"));
acronyms.add(new Acronym("CIA", "Central Intelligence Agency"));

final TableModel tModel = new AcronymTableModel(acronyms);

JTable t = new JTable(tModel);
t.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Acronym a = (Acronym)tModel.getValueAt(e.getFirstIndex(), 0);
System.out.println(a.acronym + ": " + a.definition);
}});

p.add(t);

f.getContentPane().add(p);

f.pack();

f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

f.setVisible(true);
}


}
class Acronym {
String acronym;
String definition;

public Acronym(String a, String d) {
acronym = a;
definition = d;
}
}
class AcronymTableModel implements TableModel {

private List<Acronym> acronyms;

public AcronymTableModel(List<Acronym> acs) {
this.acronyms = new ArrayList<Acronym>(acs);
}

@Override
public int getRowCount() {
return this.acronyms.size();
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int columnIndex) {
switch(columnIndex) {
case 0:
return "Acronym";
case 1:
return "Definition";
}

return null;

}

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class; // Since both columns are simply
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return acronyms.get(rowIndex);
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}

@Override
public void addTableModelListener(TableModelListener l) {
}

@Override
public void removeTableModelListener(TableModelListener l) {
}
}

Java 教程总是很好并且有很好的示例。 http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html

关于java - 将表格模型上的选定项目转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12465077/

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