Remplir et afficher Jtable

Résolu
ezzeddinehannachi Messages postés 22 Date d'inscription vendredi 25 mai 2007 Statut Membre Dernière intervention 19 juin 2012 - 30 juil. 2008 à 13:35
zinebmeriem Messages postés 4 Date d'inscription lundi 2 mai 2011 Statut Membre Dernière intervention 11 mai 2011 - 11 mai 2011 à 13:03
bonjour, j'ai un probleme avec l'affichage de Jtable lors de l'exection
de l'application normalement elle doit m'afficher le resultat d'une
requete sql mais la table est vide et j'ai essayer le meme code de
connexion de ma Jtable dans un nouveau projet é ca marche mais lorsque
je l'integre dans l'application rien n'est afficher.

voici le code si quelqu'un peut le tester et me donner une idée
vraiment je suis coincé depuis 4 jours et j'ai pas pu résoudre le
problème.merci d'avance .

<!-- BEGIN TEMPLATE: bbcode_code -->

Code :
class CachingResultSetTableModel extends ResultSetTableModel
{public CachingResultSetTableModel(ResultSet aResultSet)
{super(aResultSet);
try
{ cache = new ArrayList();
int cols = getColumnCount();
ResultSet rs = getResultSet();
 
/* place all data in an array list of Object[] arrays
We don't use an Object[][] because we don't know
how many rows are in the result set
*/
 
while(rs.next())
{ Object[] row = new Object[cols];
for(int j = 0; j < row.length; j++)
row[j] = rs.getObject(j + 1);
cache.add(row);
}
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
 
public Object getValueAt(int r, int c)
{if(r < cache.size())
return((Object[])cache.get(r))[c];
else
returnnull;
}
 
publicint getRowCount()
{return cache.size();
}
 
private ArrayList cache;
}
 

<!-- END TEMPLATE: bbcode_code --><!-- BEGIN TEMPLATE: bbcode_code -->

Code :
import java.sql.ResultSet;
 
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
 
import javax.swing.table.AbstractTableModel;
 
abstractclass ResultSetTableModel extends AbstractTableModel
{public ResultSetTableModel(ResultSet aResultSet)
{ rs = aResultSet;
try
{ rsmd = rs.getMetaData();
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
 
public String getColumnName(int c)
{try
{return rsmd.getColumnName(c+1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
return"";
}
}
 
publicint getColumnCount()
{try
{return rsmd.getColumnCount();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return0;
}
}
 
protected ResultSet getResultSet()
{return rs;
}
 
private ResultSet rs;
private ResultSetMetaData rsmd;
}

<!-- END TEMPLATE: bbcode_code --><!-- BEGIN TEMPLATE: bbcode_code -->

Code :
import java.sql.ResultSet;
import java.sql.SQLException;
 
class ScrollingResultSetTableModel extends ResultSetTableModel
{public ScrollingResultSetTableModel(ResultSet aResultSet)
{super(aResultSet);
}
 
public Object getValueAt(int r, int c)
{try
{ ResultSet rs = getResultSet();
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
returnnull;
}
}
 
publicint getRowCount()
{try
{ ResultSet rs = getResultSet();
rs.last();
return rs.getRow();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return0;
}
}
}

<!-- END TEMPLATE: bbcode_code --><!-- BEGIN TEMPLATE: bbcode_code -->

Code :
 
 
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import java.sql.Statement;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
 
publicclass Ratiotest extends JFrame {
private BorderLayout layoutMain = new BorderLayout();

private JScrollPane scrollPane;

private ResultSetTableModel model;
private JPanel panelCenter = new JPanel();
private JMenuBar menuBar = new JMenuBar();
private JMenu menuFile = new JMenu();
private JMenuItem menuFileExit = new JMenuItem();
private JToolBar toolBar = new JToolBar();
private JButton buttonOpen = new JButton();
private JButton buttonClose = new JButton();
private JButton buttonHelp = new JButton();
private ImageIcon imageOpen = new ImageIcon(ratio.class.getResource("openfile.gif"));
private ImageIcon imageClose = new ImageIcon(ratio.class.getResource("closefile.gif"));
private ImageIcon imageHelp = new ImageIcon(ratio.class.getResource("help.gif"));
private JLabel jLabel1 = new JLabel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JLabel jLabel2 = new JLabel();
private JComboBox jComboBox1 = new JComboBox();
private ResultSet rs;
private Connection con;
private Statement stmt;
privatestaticboolean SCROLLABLE = false;
private JTextField jTextField3 = new JTextField();
private JTable jTable1 = new JTable();
 
 
public Ratiotest(){
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}

try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// force loading of driver
String url = "jdbc:odbc:dbFinJan08";
String user = "";
String password = "";
con = DriverManager.getConnection(url, user,
password);

stmt = con.createStatement();

DatabaseMetaData md = con.getMetaData();
String sql = "SELECT Name\n" +
"FROM Geo INNER JOIN GeoNames ON Geo.ID = GeoNames.ID where Geo.GeoLevel=3 ;\n";
ResultSet mrs = stmt.executeQuery( sql );
while(mrs.next()){
// System.out.println(mrs.getString(1));
jComboBox1.addItem(mrs.getString(1));
}
//mrs.close();

}
catch(ClassNotFoundException e)
{
System.out.println("Error " + e);
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
 
privatevoid jbInit()throws Exception {
this.setJMenuBar( menuBar );
this.getContentPane().setLayout( layoutMain );
panelCenter.setLayout(null);
this.setSize(new Dimension(661, 509));
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener(){publicvoid actionPerformed( ActionEvent ae ){ fileExit_ActionPerformed( ae ); }});
buttonOpen.setToolTipText("Open File");
buttonOpen.setIcon( imageOpen );
buttonClose.setToolTipText("Close File");
buttonClose.setIcon( imageClose );
buttonHelp.setToolTipText("About");
buttonHelp.setIcon( imageHelp );
jLabel1.setText("Date Début");
jLabel1.setBounds(new Rectangle(5, 20, 115, 30));
jTextField1.setBounds(new Rectangle(130, 20, 120, 30));
jTextField2.setBounds(new Rectangle(130, 65, 120, 30));
jLabel2.setText("Date Fin");
jLabel2.setBounds(new Rectangle(5, 65, 115, 30));
jComboBox1.setBounds(new Rectangle(270, 20, 120, 35));
jComboBox1.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
jComboBox1_actionPerformed(e);
}
});
jTextField3.setBounds(new Rectangle(5, 120, 125, 30));
jTextField3.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
// jTextField3_actionPerformed(e);
//jTextField3.setText((String)jComboBox1.getSelectedItem());
}
});
jTable1.setBounds(new Rectangle(115, 200, 320, 115));
menuFile.add( menuFileExit );
menuBar.add( menuFile );
toolBar.add( buttonOpen );
toolBar.add( buttonClose );
toolBar.add( buttonHelp );
this.getContentPane().add( toolBar, BorderLayout.NORTH);

// scrollPane.getViewport().add(jTable1, null);
panelCenter.add(jTable1, null);
panelCenter.add(jTextField3, null);
panelCenter.add(jComboBox1, null);
panelCenter.add(jLabel2, null);
panelCenter.add(jTextField2, null);
panelCenter.add(jTextField1, null);
panelCenter.add(jLabel1, null);
this.getContentPane().add(panelCenter, BorderLayout.CENTER);
}
 
void fileExit_ActionPerformed(ActionEvent e){
System.exit(0);
}
publicvoid actionPerformed(ActionEvent evt)
{if(evt.getSource() == jComboBox1)
{// show the selected table from the combo box
 
if(scrollPane != null)
getContentPane().remove(scrollPane);

try
{ String tableName
= (String)jComboBox1.getSelectedItem();
// if (rs != null) rs.close(); String query "SELECT Sum(ClientTrack.Amount) AS SumOfAmount,Count(ClientTrack.Client) AS CountOfClients FROM ((ClientTrack INNER JOIN Loan ON ClientTrack.Loan Loan.ID) INNER JOIN Table_essai ON Loan.Location = Table_essai.Souszone) INNER JOIN GeoNames ON Table_essai.Zone = GeoNames.ID where Name='"+tableName+"'" ;
rs = stmt.executeQuery(query);
if(SCROLLABLE)
model = new ScrollingResultSetTableModel(rs);
else
model = new CachingResultSetTableModel(rs);
 
JTable jTable1 = new JTable(model);

}
catch(SQLException e)
{ System.out.println("Error " + e);
}
catch(Exception e){
System.out.print("erreur1 "+e.getMessage());//récuperer le message d'erreur en cas d'erreur
}
}
}
publicstaticvoid main(String args[]){
Ratiotest rati=new Ratiotest();
rati.show();

}
 
privatevoid jComboBox1_actionPerformed(ActionEvent e){
jTextField3.setText((String)jComboBox1.getSelectedItem());
}
 

}
 
 
 

7 réponses

cs_Chatbour Messages postés 764 Date d'inscription jeudi 27 juillet 2006 Statut Membre Dernière intervention 6 septembre 2010 19
31 juil. 2008 à 15:41
Re,

essayes d'afficher un JScrollPane au lieu d'afficher la JTable..

_____________________
Vos avis et critiques sur le livre "Objets réactifs en java" de Frédéric Boussinot : contactez moi par MP..
http://books.go%3C/body
3
Rejoignez-nous