• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

refresh a jtable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening,
I developp a frame with a jtable to consult an oracle database using an AbstractTableModel.
The jtable must be refreshed to show the new records added to the database.
I developped a void to search the records and refresh the result every 2 seconds.
This void run correctly if it's called in the void main.
I want to integrate this void in an action performed of a jbutton to put the search under control(to specify the options of the search and change the query if needed),but doing this make the application bloked without error message.
I don't understand why the void run correctly under the void main and doesn't in an action performed.
thanks
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this might be a threading / synchronization problem: ActionEvents like mouse clicks on a button are handled in the Swing event dispatcher thread. If you call your refresh method from somewhere within the actionPerformed() method of the ActionListener of your button, it might interfer with the thread executing the main method, which also calls the refresh method every two seconds, at least if the refresh method is synchronized. What happens if you remove the polling and refresh the table only in response to a click on the button?

In addition, some code would be very helpful, otherwise we can only guess what goes wrong.
 
Amin Raykou
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply Guido.
The refresh method is called only in the action performed of the jbutton.And when I do that and launch the application, as soon as I clik on the button it stops running and block, the problem is that if I put this method in the main it run correctly.Here is a simple code I hope it make it clair

First code: the search method in the main:

public class test extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private static JTable jTable = null;
private JButton jButton = null;
private static String req="select * from emp";

/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 80, 637, 84));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}

/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setSize(new Dimension(581, 260));
}
return jTable;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(99, 20, 91, 26));
jButton.setText("Rechercher");



}
return jButton;
}

//the method to search and refresh the jtable

public static void rechercher(){
try{

Dbmanager d = new Dbmanager(); //Dbmanager is a class in witch I put the methods needed for connection and getting query...

d.connect("database", "user", "password");
MonTableModel m =new MonTableModel(d.ColNames(d.getQuery(req)),d.donnee(d.getQuery(req))); //MonTableModel is the class extending AbstractTableModel

jTable.setModel(m);
while(true){
m.majDonnees(d.donnee(d.getQuery(req)));


try {
Thread.sleep(2000);
}catch(InterruptedException ie){}
}
}catch(Exception et){et.printStackTrace();}
}

public static void main(String[] args) {
new test();
rechercher(); // here I call the method of search and refresh


}

/**
* This is the default constructor
*/
public test() {
super();
initialize();

}


private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);

}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}
This code run perfectly, but I need to call the search in an action performed to get the search options and give the user the oppertunity to run the search (so to disable the search at the run of the application), so I modifyed the code:

public class test extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private static JButton jButton = null;
private static String req="select * from emp";

* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 80, 637, 84));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}

/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setSize(new Dimension(581, 260));
}
return jTable;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(99, 20, 91, 26));
jButton.setText("Rechercher");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
req="select ename from emp" ;
rechercher();} //here I add the search method in the action performed
});



}
return jButton;
}


public void rechercher(){
try{

Dbmanager d = new Dbmanager();
d.connect("base", "user", "password");
MonTableModel m =new MonTableModel(d.ColNames(d.getQuery(req)),d.donnee(d.getQuery(req)));
jTable.setModel(m);
while(true){
m.majDonnees(d.donnee(d.getQuery(req)));


try {
Thread.sleep(2000);
}catch(InterruptedException ie){}
}
}catch(Exception et){et.printStackTrace();}
}

public static void main(String[] args) {
new test(); // I delete the call of the search method from the main
}

/**
* This is the default constructor
*/
public test() {
super();
initialize();

}


private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);


}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}



}
The problem is that with that code the application bloks as soon as I press the button, and I don't get error message, it seems like the "while(true)" bloc is running without showing nothing in the table.

please help!!!
 
Amin Raykou
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guido for your help.
In fact it was a threading/synchro problem.
I wrote a class extending thread, and in the void run() I call the refresh method.Then I start the thread in the action performed, it works !!!
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to take a look at the SwingUtilities.invokeLater(Runnable r) method, which enqueues the specified Runnable to be executed after all pending events (in particular, the mouse click on the button) have been processed. No need for using a thread.
 
Amin Raykou
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact I tryed to use the SwingUtilities.invokeLater() before posting the topic but there was the same problem.Now the new thread, is the best solution I found.
Thanks for your help
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic