How to refresh (reload) a JTable ?
I have written an application which loads into a JTable a mysql database
table. Currently it gets the table name from the args[] array, and all
other parameters (host, database, mysql user and password) are fixed.
The main(String[] args) method is the standard Swing tutorial stuff
which run()s createAndShowGUI
createAndShowGUI is also a standard Swing tutorial wrapper which
myGui = new myGui();
myGui.setOpaque(true); //content panes must be opaque
frame.setContentPane(myGui);
//Display the window.
frame.pack();
frame.setVisible(true);
myGui is currently an inner class that extends JPanel implements
ActionListener and make a GridBagLayout panel which currently contains :
- a form of text areas, which echo the current value of host, user,
database and table (all of which are so far disabled but the table
name
- a Commit button
- the Jtable itself which is called as (c is a GridBagConstraints) :
dbprova bottom = new dbprova(tabname);
panel.add(bottom,c) ;
dbprova is a method in the top level class which
super(new GridLayout(1,0));
establishes the mysql connection
issues the mysql select statement
generates a new MyTableModel();
generates the JTable table = new JTable(mtm);
(wraps it in a scroll pane)
adds the scroll pane and adjusts the table layout
closes the mysql connection
The quoted Commit button in myGui is assciated to an action listener.
Its intended usage is that the user which might want to CHANGE the
database table (and in perspective database, user, host) enters it in
the appropriate form text area and presses Commit.
The action listener retrieves the table name and sets it into the global
variable tabname, but how can it force the Jtable to be reloaded ?
I thought perhaps one had to call revalidate, but it seems to do
nothing.
public void actionPerformed(ActionEvent e) {
if ("mysqlconnect".equals(e.getActionCommand())) {
tabname=tabl.getText() ;
System.out.println("now table is "+tabname );
revalidate();
}
How can one replace the "bottom" Jtable with a new instance and refresh
the display ?
--
----------------------------------------------------------------------
nospam@mi.iasf.cnr.it is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.