How to refresh (reload) a JTable ?

G

Guest

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 ?
 
G

Guest

When I was programming a JTable, I used to use the following
operation to refresh the JTable.

fireTableDataChanged()

I will experiment with it.
I'm afraid it will require some change to my philosophy ...

Currently the bottom (last) component of my panel is a Jtable (tied to a
mysql tablename passed as args[0] via a MyTableModel) but is a
JSeparator if args[0] is null.

(I'd have to provide a table model which deals with a null tablename)

Can I somehow disconnect/destroy the last component of the panel and
add a new one ? And then somehow repaint ?
 
G

Guest

Can I somehow disconnect/destroy the last component of the panel and
add a new one ? And then somehow repaint ?

I found that this piece of code in my button listener does ALMOST all
what I need (tabname is the new database table name retrieved from a
form, dbprova is the JTable surrounded by scroll pane attached to such
mysql table)

tabname=tabl.getText() ;
panel.remove(bottom) ;
bottom = new dbprova(tabname);
panel.add(bottom,c2) ;
revalidate() ;
repaint();

where bottom is a JComponent which is initialized to
bottom = new dbprova(tabname);
when tabname is supplied as args[0] and
bottom = new JSeparator(SwingConstants.HORIZONTAL) ;
when args[0] is null

The only problems are that my application frame is laid out as such (in
a GridBag), with a row of a button, some Jlabels'='JTextField, on top of
the JTable

CONNECT param=text param=text param=text param=text tableparam=text
Jtable

if I call the application with a tablename the Jtable is initialized
with the content of the first table. The width is determined by the top
row of buttons&labels, the height is arbitrary (for small tables there
is "gray" space. Loading a new table will keep dimensions unchanged.

If however I call the application with no arguments, the Jtable is
replaced by an (invisible) separator. Loading a new table works, but I
cannot see the new table unless I explicitly resize the window.

What is worse, all the JTextField's in the top row become extremely
thin, and there is no way to resize them ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top