Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

How to refresh (reload) a JTable ?

Thread view: 
LC's NoSpam Newsreading account - 07 Jun 2007 18:03 GMT
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 ?

Signature

----------------------------------------------------------------------
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.

Stefan Ram - 07 Jun 2007 20:25 GMT
>How can one replace the "bottom" Jtable with a new instance and
>refresh the display ?

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

http://download.java.net/jdk7/docs/api/javax/swing/table/AbstractTableModel.html
#fireTableDataChanged
()

 (See also the other fire... operations of this class.)
LC's NoSpam Newsreading account - 08 Jun 2007 09:03 GMT
>   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 ?

Signature

----------------------------------------------------------------------
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.

LC's NoSpam Newsreading account - 08 Jun 2007 11:14 GMT
> 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 ...

Signature

----------------------------------------------------------------------
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.