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