如何在 TableModelEvent 中获取 JTable
How to get JTable in TableModelEvent
我在 Eclipse 中创建了 Swing GUI,其中我有两个 JTables 和方法 tableChanged。在这种方法中,当我编辑 table 中的某些单元格并将其发送到项目的逻辑层时,我需要从 table 读取数据。问题是如何识别 table ,它调用此方法,然后对编辑的 table 行使用正确的 JSON 结构并发送它。
JTable1:
Object[] columns = {"ID","Jmeno","Prijmeni","Narozeni","Pozice"};
JTable userTable = new JTable(new DefaultTableModel(columns, 0));
scrollPane.setViewportView(userTable);
userTable.setCellSelectionEnabled(true);
userTable.setColumnSelectionAllowed(true);
updateUser();
userTable.getModel().addTableModelListener((TableModelListener) this);
JTable2:
Object[] columns = {"EAN","Cena","Nazev","Oddeleni","Sk.Podminky","VytvorenoUzivatelem"};
JTable zboziTable = new JTable(new DefaultTableModel(columns, 0));
scrollPane.setViewportView(zboziTable);
userTable.setCellSelectionEnabled(true);
userTable.setColumnSelectionAllowed(true);
updateZbozi();
zboziTable.getModel().addTableModelListener((TableModelListener) this);
table更改方法:
@Override
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String[] data = new String[model.getColumnCount()];
if(column >= 0)
{
if(!model.getValueAt(row, column).toString().equals(""))
{
for (int i = 0; i < data.length; i++)
{
data[i] = model.getValueAt(row, i).toString();
}
//here is JSON structure for userTable
String jsonData = "{\"ID\": \"" + data[0] + "\", \"Jmeno\": \"" + data[1] + "\", \"Prijmeni\": \"" + data[2] + "\", \"Datum_Narozeni\": \"" + data[3] + "\", \"Pozice\": \"" + data[4] + "\"}";
WebTarget target = client.target("http://localhost:8080/WSdemo/obchod/editUser");
target.request().post(Entity.entity(jsonData, MediaType.APPLICATION_JSON),String.class);
}
else
System.out.print("Enter valid text!");
}
}
如果您的 TableModel
扩展 AbstractTableModel
,您可以使用 getTableModelListeners()
来识别收听 JTable
。下面的侦听器调用父 Component
的 getName()
方法,但 JComponent
客户端 属性 可能会提供更多自由度。
@Override
public void tableChanged(TableModelEvent e) {
AbstractTableModel model = (AbstractTableModel) e.getSource();
TableModelListener[] listeners = model.getTableModelListeners();
for (TableModelListener listener : listeners) {
if (listener instanceof JTable) {
System.out.println(((JTable)listener).getName());
}
}
}
我在 Eclipse 中创建了 Swing GUI,其中我有两个 JTables 和方法 tableChanged。在这种方法中,当我编辑 table 中的某些单元格并将其发送到项目的逻辑层时,我需要从 table 读取数据。问题是如何识别 table ,它调用此方法,然后对编辑的 table 行使用正确的 JSON 结构并发送它。
JTable1:
Object[] columns = {"ID","Jmeno","Prijmeni","Narozeni","Pozice"};
JTable userTable = new JTable(new DefaultTableModel(columns, 0));
scrollPane.setViewportView(userTable);
userTable.setCellSelectionEnabled(true);
userTable.setColumnSelectionAllowed(true);
updateUser();
userTable.getModel().addTableModelListener((TableModelListener) this);
JTable2:
Object[] columns = {"EAN","Cena","Nazev","Oddeleni","Sk.Podminky","VytvorenoUzivatelem"};
JTable zboziTable = new JTable(new DefaultTableModel(columns, 0));
scrollPane.setViewportView(zboziTable);
userTable.setCellSelectionEnabled(true);
userTable.setColumnSelectionAllowed(true);
updateZbozi();
zboziTable.getModel().addTableModelListener((TableModelListener) this);
table更改方法:
@Override
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String[] data = new String[model.getColumnCount()];
if(column >= 0)
{
if(!model.getValueAt(row, column).toString().equals(""))
{
for (int i = 0; i < data.length; i++)
{
data[i] = model.getValueAt(row, i).toString();
}
//here is JSON structure for userTable
String jsonData = "{\"ID\": \"" + data[0] + "\", \"Jmeno\": \"" + data[1] + "\", \"Prijmeni\": \"" + data[2] + "\", \"Datum_Narozeni\": \"" + data[3] + "\", \"Pozice\": \"" + data[4] + "\"}";
WebTarget target = client.target("http://localhost:8080/WSdemo/obchod/editUser");
target.request().post(Entity.entity(jsonData, MediaType.APPLICATION_JSON),String.class);
}
else
System.out.print("Enter valid text!");
}
}
如果您的 TableModel
扩展 AbstractTableModel
,您可以使用 getTableModelListeners()
来识别收听 JTable
。下面的侦听器调用父 Component
的 getName()
方法,但 JComponent
客户端 属性 可能会提供更多自由度。
@Override
public void tableChanged(TableModelEvent e) {
AbstractTableModel model = (AbstractTableModel) e.getSource();
TableModelListener[] listeners = model.getTableModelListeners();
for (TableModelListener listener : listeners) {
if (listener instanceof JTable) {
System.out.println(((JTable)listener).getName());
}
}
}