在我已经创建的JTable中存储数据(写入GUI)时遇到问题。问题是,当我单击GUI中的添加按钮,而不是在JTable中显示它时,它会打开另一个窗口并在那里显示它。所以它补充说,但它没有显示在桌面上。下面是在另一个窗口中显示信息的代码部分:
import java.lang.Object;
import java.io.*;
import java.util.*;
public class MemberManager {
File custFile;
private Member m1 =
new Member("M0001", "Mark", "Abela", "13/05/90", "121 Kent Street", "21469432", "99456209");
private Member m2 =
new Member("M0002", "John", "Sant", "25/11/84", "55 Golden Road", "21226932", "79513625");
private ArrayList<Member> memMemb = new ArrayList<Member>();
public MemberManager(){
memMemb.add(m1);
memMemb.add(m2);
}
public String[][] getMembers(){
int pos = 0;
//Creating the table with the players data
String data [][] = new String[memMemb.size()][8];
for (Object obj : memMemb){
Member mem = (Member)(obj);
data[pos][0] = mem.getID();
data[pos][1] = mem.getName();
data[pos][2] = mem.getSurname();
data[pos][3] = mem.getDob();
data[pos][4] = mem.getAddress();
data[pos][5] = mem.getTel();
data[pos][6] = mem.getMob();
}
return data;
}
public void addMember(String id, String name, String surname, String dob, String address, String tel, String mob){
Member tempMem = new Member();
tempMem.setID(id);
tempMem.setName(name);
tempMem.setSurname(surname);
tempMem.setDob(dob);
tempMem.setAddress(address);
tempMem.setTel(tel);
tempMem.setMob(mob);
memMemb.add(tempMem);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("Member Data");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(memMemb);
oos.flush();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
try {
// read back
fis = new FileInputStream("Member Data");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
memMemb = (ArrayList<Member>) obj;
ArrayList<Member> listFromFile = (ArrayList) obj;
for (Member member: listFromFile) {
System.out.println(member.toString());
}
}catch (IOException io){
System.err.println("Cannot access or write to file");
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
JTable im using是另一个包中的默认表模型。我在按钮上使用mouseListener来添加数据......这是代码的一部分......: -
memAddOk.addMouseListener(this);
memAddExit.addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
if (e.getSource().equals(memAddOk)){
id = textBox[0].getText();
name = textBox[1].getText();
surname = textBox[2].getText();
dob = textBox[3].getText();
address = textBox[4].getText();
tel = textBox[5].getText();
mob = textBox[6].getText();
MemberManager tempData = new MemberManager();
tempData.addMember(id, name, surname, dob, address, tel, mob);
} else
if (e.getSource().equals(memAddExit)) {
this.dispose();
}
}
如何将此代码存储在JTable中?
答案 0 :(得分:5)
问题是,当我点击GUI中的添加按钮,而不是在JTable中显示它时,它会打开另一个窗口并在那里显示它。
这只是做你告诉它要做的事情:
public void mouseClicked(MouseEvent e){
if (e.getSource().equals(memAddOk)){
id = textBox[0].getText();
// .... etc..
// !!!! *** Here you create a new MemberManager object *** !!!!
MemberManager tempData = new MemberManager();
tempData.addMember(id, name, surname, dob, address, tel, mob);
} else
//... etc..
解决方案:
答案 1 :(得分:4)
1)使用SwingWorker或Runnable#Thread
在File IO
或Database
2)一旦定义JTable和DefaultTableModel,启动后台任务以加载来自File
或Database
,simple example