我有一个 jtable,它显示一个包含自己的文本文件。文本文件中的每一行都是一个用户记录。我添加了一个编辑按钮来编辑表格上的每个选定行。我使用此代码,但我不明白如何执行此操作:
public class AllUser extends AbstractTableModel {
UserInformation uiS = new UserInformation();
String[] col = {"ID", "Fname", "Lname", "Gender", "Date"};
List<UserInformation> Udata = new ArrayList<UserInformation>();
public AllUser() {
BufferedReader br = null;
try {
FileReader fr = new FileReader("xxx.txt");
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
Udata.add(initializeUserInfos(line));
}
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ioe) {
}
}
}
}
private UserInformation initializeUserInfos(String str) {
UserInformation Uinit = new UserInformation();
String[] CellArray = str.split(" ");
Uinit.setID(CellArray[0]);
Uinit.setFname(CellArray[1]);
Uinit.setLname(CellArray[2]);
Uinit.setGender(CellArray[3]);
Uinit.setDate(CellArray[4]);
return Uinit;
}
@Override
public String getColumnName(int colu) {
return col[colu];
}
@Override
public int getRowCount() {
return Udata.size();
}
@Override
public int getColumnCount() {
return col.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
UserInformation uinfoS = Udata.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = uinfoS.getID();
break;
case 1:
value = uinfoS.getFname();
break;
case 2:
value = uinfoS.getLname();
break;
case 3:
value = uinfoS.getGender();
break;
case 4:
value = uinfoS.getDate();
break;
default:
value = "...";
}
return value;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
UserInformation userInfo = new UserInformation();
switch (columnIndex) {
case 0:
userInfo.setID((String) value);
break;
case 1:
userInfo.setFname((String) value);
break;
case 2:
userInfo.setLname((String) value);
break;
case 3:
userInfo.setGender((String) value);
break;
case 4:
userInfo.setDate((String) value);
break;
}
}
public Object getSelectedMember(int row){
UserInformation userInf=new UserInformation();
Object fname=userInf.getFname();
Object lname=userInf.getLname();
Object gender=userInf.getGender();
Object date=userInf.getDate();
return // ?
}
}
我添加了这个getSelectedMember(int row)
方法来做到这一点,但是不正确!
public class UserPage extends JFrame implements ActionListener {
private AllUser userModel;
private JTable uTable;
private JButton editButton;
public UserPage() {
userModel = new AllUser();
uTable = new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels(), BorderLayout.PAGE_START);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
}
public final JPanel buttonPanels() {
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
editButton=new JButton("Edit");
editButton.addActionListener(this);
buttonsPanel.add(editButton);
return buttonsPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== editButton){
int selectedRow=uTable.getSelectedRow();
if(selectedRow>0){
editUser(selectedRow);
}
else{
JOptionPane.showMessageDialog(null, "Select a user");
}
}
}
public void editUser(int row){
userModel.getSelectedMember(row);
new NewUserFrame_Edit().setVisible(true);
}
}
UserInformation
班级:
public class UserInformation {
private String Fname;
private String Lname;
private String ID;
private String Gender;
private String Date;
public String getFname() {
return Fname;
}
public void setFname(String fname) {
this.Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
this.Lname = lname;
}
public String getID() {
return ID;
}
public void setID(String i_d) {
this.ID = i_d;
}
public String getGender() {
return Gender;
}
public void setGender(String gndr) {
this.Gender = gndr;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
this.Date = date;
}
@Override
public String toString() {
return ID + " " + Fname + " "
+ Lname + " " + Gender + " " + Date + "\n";
}
}
对不起,长代码。