我有一个JTable,我可以在其中为用户添加一些属性,如年龄,名称等。这有效,用户被添加到我的arraylist和JTable。 现在我想要的是当我选择JTable行时,能够获取存储在用户的arrayList中的对象,以便我可以修改或删除它们。
以下是我将用户添加到JTable时的代码示例:
private void jButtonAddAUserActionPerformed(java.awt.event.ActionEvent evt) {
User obj=new User();
obj.setName(jTextFieldName.getText());
obj.setAdress(jTextFieldAdress.getText());
obj.setNumCC(Integer.parseInt(jTextFieldNumCC.getText()));
obj.setTele(Integer.parseInt(jTextFieldTele.getText()));
obj.setUserName(jTextFieldUserName.getText());
obj.setPassword(jTextFieldPassword.getText());
DefaultTableModel model=(DefaultTableModel) jTableUsers.getModel();
model.addRow(new Object[]{
jTextFieldName.getText(),
jTextFieldAdress.getText(),
jTextFieldTele.getText(),
jTextFieldNumCC.getText(),
obj.isAdmin
});
usersList.add(obj);
JOptionPane.showMessageDialog(null,"Data inserted correctly.");
jTextFieldName.setText("");
jTextFieldAdress.setText("");
jTextFieldNumCC.setText("");
jTextFieldTele.setText("");
jTextFieldPassword.setText("");
jTextFieldUserName.setText("");
}
编辑: 以下是删除已在工作的用户的代码:
private void jButtonRemoverActionPerformed(java.awt.event.ActionEvent evt){
DefaultTableModel model = (DefaultTableModel) jTableInvestidores.getModel();
User u = userList.get(jTableUsers.getSelectedRow());
userList.remove(u);
model.removeRow(jTableUsers.getSelectedRow());
JOptionPane.showMessageDialog(null,"Data removed.");
}
以下是用于更新仍无法正常工作的用户的代码,我试图从jTextFields更新它:
private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) jTableUsers.getModel();
userList.get(jTableUsers.getSelectedRow());
model.setValueAt(jTextFieldName.getText(), jTableUsers.getSelectedRow(),0);
model.setValueAt(jTextFieldAdress.getText(), jTableUsers.getSelectedRow(),1);
model.setValueAt(jTextFieldPhone.getText(), jTableUsers.getSelectedRow(),2);
model.setValueAt(jTextFieldNumCC.getText(), jTableUsers.getSelectedRow(),3);
User u =userList.get(jTableUsers.getSelectedRow());
JOptionPane.showMessageDialog(null,"Data updated.");
}
任何人都可以给我一些帮助吗?谢谢!
答案 0 :(得分:3)
User u=userList.get(table.getSelectedRow()); //get user for editing
int location=table.getSelectedRow(); //get location in list to maintain order
userList.remove(u); //remove selected user to edit variables
//modify user u
userList.add(location,u); //insert user at previous location in list
model.setRowCount(0); //reset table model
for (int i = 0; i < userList.size(); i++) { //refill table model
User u = userList.get(i); /7get user
Vector<Object> vhelp = new Vector<>(); //create vector to store the values of the variables from user
vhelp.add(/*your data*/); // 1 add per variable that should be displayed in table
model.addRow(vhelp); //add the data to the table model (fills the table with data)
}
你的方法应如下所示:
DefaultTableModel model = (DefaultTableModel) jTableUsers.getModel();
User u = userList.get(jTableUsers.getSelectedRow());
int location=jTableUsers.getSelectedRow();
userList.remove(u);
u.setName(jTextFieldName.getText());
u.setAdress(jTextFieldAdress.getText());
u.setNumCC(Integer.parseInt(jTextFieldNumCC.getText()));
u.setTele(Integer.parseInt(jTextFieldTele.getText()));
//u.isAdmin can't tell what this has to be
userlist.add(location,u);
model.setRowCount(0);
for (int i = 0; i < userList.size(); i++) {
User u = userList.get(i);
Vector<Object> vhelp = new Vector<>();
vhelp.add(u.getName());
vhelp.add(u.getAddress());
vhelp.add(u.getTele());
vhelp.add(u.getNumCC());
vhelp.add(u.isAdmin);
model.addRow(vhelp);
}
JOptionPane.showMessageDialog(null, "Data updated.");
答案 1 :(得分:1)
用户被添加到我的arraylist和JTable中。
不要将数据存储在两个单独的位置。数据应仅存储在 <default-action-ref name="notfound" />
<global-results>
<result name="notfound" type="httpheader">
<param name="error">404</param>
<param name="errorMessage">Oh No You Didn't</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.me.s2ui.AccessException" result="notfound" />
</global-exception-mappings>
<action name="notfound">
<result type="httpheader">
<param name="error">404</param>
<param name="errorMessage">Oh No You Didn't</param>
</result>
</action>
的{{1}}。
因此,您可以创建自定义“用户”对象以包含有关每个用户的数据。然后,您可以创建一个自定义$(function () {
var _URL = window.URL || window.webkitURL;
$(".UploadImage").on("change", function () {
var preview = $(this).attr("data-img");
var file, img;
if ((file = this.files[0])) {
img = new Image();
img.onload = function () {
document.getElementById(preview).src = _URL.createObjectURL(file);
};
img.src = _URL.createObjectURL(file);
}
});
});
来保存“用户”对象,该对象可以由<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="file" data-img="preview1" class="UploadImage" />
<img src="" id="preview1"/>
</div>
<div>
<input type="file" data-img="preview2" class="UploadImage" />
<img src="" id="preview2"/>
</div>
<div>
<input type="file" data-img="preview3" class="UploadImage" />
<img src="" id="preview3"/>
</div>
<div>
<input type="file" data-img="preview4" class="UploadImage" />
<img src="" id="preview4"/>
</div>
显示和访问。
现在我想要的是当我选择JTable行时,能够获取存储在用户的arrayList中的对象,以便我可以修改或删除它们。
查看Table Row Model,了解创建自定义TableModel
的分步方法。它包含从JTable
动态添加,访问和删除对象所需的所有方法。