Reputation: 557
So I have this application with a JTable in it. The JTable is inside of a JScrollPane and JScrollPane is painted on a JFrame.
Now in my application I open a new windows to add a new row to that table and after I click a button to save the changes, the new window closes.
Now I have tried adding these lines after the new window is closed:
askTableInfo(); //a method to save the info in database to table and then save the table to variable 'table'
table.repaint();
scrollPane.repaint();
And of course repaint(); by it self. But it still does not seem to update my table in the JFrame.
What could be the issue here?
public class AppWindow extends JFrame implements ActionListener {
String user = "";
JLabel greetText = new JLabel();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(1, 3));
JScrollPane scrollPane;
JTable tabel;
JButton newBook = new JButton("Add a book");
JButton deleteBook = new JButton("Remove a book");
JButton changeBook = new JButton("Change a book");
int ID;
public AppWindow(String user, int ID) {
this.ID = ID;
this.user = user;
setSize(500, 500);
setTitle("Books");
setLayout(new BorderLayout());
greetText.setText("Hi "+user+" here are your books:");
add(greetText, BorderLayout.NORTH);
askData();
panel.add(scrollPane);
add(panel, BorderLayout.CENTER);
panel2.add(newBook);
panel2.add(deleteBook);
panel2.add(changeBook);
add(paneel2, BorderLayout.SOUTH);
newBook.addActionListener(this);
setVisible(true);
}
private void askData() {
DataAsker asker = null;
try {
asker = new AndmeKysija(ID);
} catch (SQLException e) {
e.printStackTrace();
}
table = asker.giveTable();
scrollPane = new JScrollPane(tabel);
}
public static void main(String[] args){
AppWindow window = new AppWindow("Name", 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newBook){
new BookAdded(ID);
panel.revalidate();
panel.add(scrollPane);
panel.repaint();
repaint();
}
}
}
Upvotes: 0
Views: 2425
Reputation: 347234
Not the best method, but it will get you across the line...
public AppWindow(String user, int ID) {
this.ID = ID;
this.user = user;
setSize(500, 500);
setTitle("Books");
setLayout(new BorderLayout());
greetText.setText("Hi "+user+" here are your books:");
add(greetText, BorderLayout.NORTH);
JTable table = askData();
scrollPane.setViewportView(table);
panel.add(scrollPane);
add(panel, BorderLayout.CENTER);
panel2.add(newBook);
panel2.add(deleteBook);
panel2.add(changeBook);
add(paneel2, BorderLayout.SOUTH);
newBook.addActionListener(this);
setVisible(true);
}
private JTable askData() {
DataAsker asker = null;
try {
asker = new AndmeKysija(ID);
} catch (SQLException e) {
e.printStackTrace();
}
return asker.giveTable();
}
public static void main(String[] args){
AppWindow window = new AppWindow("Name", 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newBook){
new BookAdded(ID);
JTable table = askData();
scrollPane.setViewportView(table);
}
}
What you should be doing is creating a new TableModel
from the results of AndmeKysija
, AndmeKysija
should have idea or concept of the UI. You would then simply need to use JTable#setModel
to update the view...
Swing uses a varient of the Model-View-Controller paradigm
Upvotes: 1