Reputation: 31
What I'm trying to do is to get some data from an ArrayList, which contains data like GroupName, Name, Time, URL, and Memo, sort it using the insert() class, which basically sorts up the data by group, and then display them on my jTable using addRow.
This works just fine, but now I need to add a new row of data, using a button. jb6 is the button that get's it data that I input in another frame, and puts it into Arraylist. I am hoping that by changing the ArrayList this way, the Jtable will refresh itself to show the new data, by putting the new ArrayList through insert() again, and displaying it. I;ve checked that the data goes correctly into Arraylist, so can anyone tell me how to refresh the JTable accordingly? Thanks.
public class BookmarkListGUI{
String[] header= {"","Group","Name","URL","Time","Memo"};
BookmarkList bmlist=new BookmarkList("C:\\bookmark.txt");
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(header,0);
JScrollPane scroll;
public BookmarkListGUI() {
JPanel panel=new JPanel();
JFrame Frame1=new JFrame("Bookmark Manager");
table.setModel(model);
scroll = new JScrollPane(table);
insert();
scroll.setBounds(0,0,1000,500);
scrolladd.setBounds(0,0,1000,100);
panel.setBounds(0,0,1000,500);
panel.setLayout(null);
panel.add(scroll);
Frame1.add(panel);
Frame1.setVisible(true);
Button jb6 = new Button("INPUT");
jb6.setLocation(1000,0);
jb6.setSize(200,50);
jb6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String o1=(String)modeladd.getValueAt(0, 0);
String o2=(String) modeladd.getValueAt(0, 1);
String o3=(String)modeladd.getValueAt(0, 2);
String o5=(String)modeladd.getValueAt(0, 4);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm");
String formatedNow = now.format(formatter);
Bookmark add = new Bookmark(o2,formatedNow,o3,o1,o5);
bmlist.bookmarks.add(add);
Frame1.setVisible(true);
Frame1.revalidate();
Frame1.repaint();
Frame2.setVisible(false);
}
});
Frame2.add(jb6);
}
public void insert() {
int bmnumber=bmlist.numBookmarks();
int k=bmnumber;
int p=0;
for(int i=1;i<=k;i++) {
bmlist.mergeByGroup();
if(bmlist.getBookmark(p).getGroupName().length()!=0) {
if(i==1) {
model.addRow(new Object[] {">",bmlist.getBookmark(p).getGroupName(),
"","","",""});
p++;
}
else if(bmlist.getBookmark(p).getGroupName().equals(bmlist.getBookmark(p-1).getGroupName())==true) {
i--;
k--;
p++;
continue;
}
else if(bmlist.getBookmark(p).getGroupName().equals(bmlist.getBookmark(p-1).getGroupName())==false) {
model.addRow(new Object[] {">",bmlist.getBookmark(p).getGroupName(),
"","","",""});
p++;
}
}
else {
model.addRow(new Object[] {"","",bmlist.getBookmark(p).getName(),
bmlist.getBookmark(p).getUrl(),bmlist.getBookmark(p).getTime(),bmlist.getBookmark(p).getMemo()});
p++;
}
}
}
```
Upvotes: 0
Views: 26
Reputation: 1760
You're making this really hard on yourself. Create a custom tableModel. I'm gonna use a simpler bean, but the principles are the same. Lets say I have a list of Books and I want a tableModel to represent them:
public class BookTableModel extends AbstractTableModel {
private List<Book> list;
public BookTableModel(List<Book> books) {
list = books;
}
public void addBook(Book book) {
list.add(book);
fireTableRowsInserted(list.size()-1, list.size()-1);
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public Object getValueAt(int row, int col) {
try {
Book book = list.get(row);
switch (col) {
case 0:
return book.getIsbn();
case 1:
return book.getTitle();
case 2:
return book.getAuthor();
default: break;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Now if you want to add a new row, just call the model.addBook()
method. The rest is done for you.
Upvotes: 1