URGENT !!! Mettre un JCheckBox dans un JTable

cs_nosil Messages postés 37 Date d'inscription dimanche 20 juin 2004 Statut Membre Dernière intervention 20 octobre 2005 - 22 juin 2004 à 15:33
bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005 - 25 août 2005 à 15:33
Bonjour a tous.....J'ai urgemment besoin de votre savoir !!!!

Je cree une application lier a une base de donnee PostgreSQL, (Eh oui, je suis sous linux, mais change rien au probleme) et j'aimerais inserer un JCheckBox dans une JTable....

Je vous donne mon code :

public class filsTableModel extends AbstractTableModel
{
private ResultSet rs = null;
private int rowcount = 0;
private Object data[][];

public filsTableModel(ResultSet r)
{
this.rs = r;
try
{
this.rs.last();
this.rowcount = this.rs.getRow();
this.rs.first();
} catch (Exception e)
{
System.out.println("Erreur filsTableModel " + e.getMessage());
this.rs = null;
}
fillDatas();
}

private void fillDatas()
{
try
{
rs.last();
System.out.println(rs.getRow());
System.out.println(getColumnCount());
data = new Object[getColumnCount()][rs.getRow()];
rs.first();
}catch(SQLException e)
{
System.out.println("Erreur fillDatas " + e.getMessage());
}
for (int row = 0; row <= getRowCount(); row++)
for (int col = 1; col <= getColumnCount(); col++)
{
try
{
this.rs.relative(row);
data[row][col] = (Object)this.rs.getString(col);
}catch(SQLException e)
{
System.out.println("Erreur Creation tableau " + e.getMessage());
}
}
for ( int line = 0; line <= getRowCount(); line++ )
data[line][0] = new Boolean(false);
}

public int getRowCount()
{
try
{
return rs.getRow();
}catch(SQLException e)
{
System.out.println("Erreur" + e.getMessage());
return (0);
}
}

public int getColumnCount()
{
try
{
return (this.rs.getMetaData().getColumnCount() + 1);
} catch (Exception e)
{
System.out.println("Erreur" + e.getMessage());
return (0);
}
}

public Object getValueAt(int row, int col)
{
return data[row][col];
}

public boolean IsCellEditable(int row, int col)
{
if (col > 1 )
return false;
else
return true;
}

public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}

}

Et pour la fenetre dont la JTable :

public class FenetreGenPresence extends JFrame
{
GridBagLayout gridBagLayout1 = new GridBagLayout();
JPanel jPanel1 = new JPanel();
JComboBox jComboBox1 = new JComboBox();
JCheckBox myCheckBox = new JCheckBox();
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
String data = null;
Statement s = null;
JComboBox jComboBox2 = new JComboBox();

public FenetreGenPresence()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void jbInit() throws Exception
{
this.setSize(new Dimension(800, 600));
jPanel1.setLayout(xYLayout1);
jComboBox1.addActionListener(new FenetreGenPresence_jComboBox1_actionAdapter(this));
jComboBox2.addActionListener(new FenetreGenPresence_jComboBox2_actionAdapter(this));
jComboBox2.addActionListener(new FenetreGenPresence_jComboBox2_actionAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jComboBox1, new XYConstraints(97, 5, 171, -1));
jPanel1.add(jScrollPane1, new XYConstraints(50, 53, 600, 400));
jPanel1.add(jComboBox2, new XYConstraints(297, 5, 171, -1));
jScrollPane1.getViewport().add(jTable1, null);
JCheckBox myCheckBox = new JCheckBox();
createComboBox();
}

public void createComboBox()
{
ResultSet r = null;

try
{
s = AccessPostgres.getConnection().createStatement(); r s.executeQuery( "SELECT libelle FROM enumere WHERE ref_enumere 1;" );
while(r.next())
{
data = r.getString(1);
jComboBox1.addItem(data);
}
s.close();
}catch(SQLException ex)
{
System.out.println("Erreur" + ex);
}
}

void jComboBox1_actionPerformed(ActionEvent e)
{
ResultSet r = null;
TableModele tm = null;

jTable1.removeAll();
try
{
s = AccessPostgres.getConnection().createStatement(); String requete "SELECT nom, prenom FROM personne, eleve, enumere WHERE id_personne eleve.ref_personne AND eleve.ref_enumere = id_enumere AND enumere.libelle = " + "'" +
((String) jComboBox1.getSelectedItem()) + "';";
r = s.executeQuery(requete);
tm = new TableModele ( r );
this.jTable1.setModel ( tm );

}
catch(SQLException ex)
{
System.out.println("Erreur de jComboBox1_actionPerformed" + ex.getMessage());
}
}

public void addColumnCheckBox()
{

// jTable1.getModel().setValueAt();
// jTable1.setValueAt(new Boolean(false), 1, 1);
}

void jComboBox2_actionPerformed(ActionEvent e)
{
ResultSet r = null;
filsTableModel tm = null;

jTable1.removeAll();
try
{
s = AccessPostgres.getConnection().createStatement(); String requete "SELECT libelle FROM enumere WHERE ref_enumere id_enumere AND libelle = '" +
((String) jComboBox1.getSelectedItem()) + "';";
r = s.executeQuery(requete);
tm = new filsTableModel ( r );
this.jTable1.setModel ( tm );
}
catch(SQLException ex)
{
System.out.println("Erreur de jComboBox1_actionPerformed" + ex.getMessage());
}

}

}

class FenetreGenPresence_jComboBox1_actionAdapter implements java.awt.event.ActionListener
{
FenetreGenPresence adaptee;

FenetreGenPresence_jComboBox1_actionAdapter(FenetreGenPresence adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jComboBox1_actionPerformed(e);
}
}

class FenetreGenPresence_jComboBox2_actionAdapter implements java.awt.event.ActionListener
{
FenetreGenPresence adaptee;

FenetreGenPresence_jComboBox2_actionAdapter(FenetreGenPresence adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jComboBox2_actionPerformed(e);
}
}

Ou et comment je peux inserer un JCheckBox dans la premiere colonne de mon tableau....

PLEASE HELP ME.....

Merci beaucoup d'avance

NoSiL

3 réponses

wargre Messages postés 649 Date d'inscription mardi 8 juin 2004 Statut Membre Dernière intervention 9 septembre 2004 7
22 juin 2004 à 16:27
class filsTableModel

getValueAt(int row, int col) et (getColumnClass) doivent renvoyer ta JCheckBox (ou la class) Sinon oublie pas de faire
0
cs_nosil Messages postés 37 Date d'inscription dimanche 20 juin 2004 Statut Membre Dernière intervention 20 octobre 2005
22 juin 2004 à 16:52
Argh, desole......je me suis trompe de class....

Je voulais pas mettre la class filsTablemodel mais TableModele...

Bon, je reessaye....

public class FenetreGenPresence extends JFrame
{
GridBagLayout gridBagLayout1 = new GridBagLayout();
JPanel jPanel1 = new JPanel();
JComboBox jComboBox1 = new JComboBox();
JCheckBox myCheckBox = new JCheckBox();
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
String data = null;
Statement s = null;
JComboBox jComboBox2 = new JComboBox();

public FenetreGenPresence()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void jbInit() throws Exception
{
this.setSize(new Dimension(800, 600));
jPanel1.setLayout(xYLayout1);
jComboBox1.addActionListener(new FenetreGenPresence_jComboBox1_actionAdapter(this));
jComboBox2.addActionListener(new FenetreGenPresence_jComboBox2_actionAdapter(this));
jComboBox2.addActionListener(new FenetreGenPresence_jComboBox2_actionAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jComboBox1, new XYConstraints(97, 5, 171, -1));
jPanel1.add(jScrollPane1, new XYConstraints(50, 53, 600, 400));
jPanel1.add(jComboBox2, new XYConstraints(297, 5, 171, -1));
jScrollPane1.getViewport().add(jTable1, null);
createComboBox();
}

public void createComboBox()
{
ResultSet r = null;

try
{
s = AccessPostgres.getConnection().createStatement(); r s.executeQuery( "SELECT libelle FROM enumere WHERE ref_enumere 1;" );
while(r.next())
{
data = r.getString(1);
jComboBox1.addItem(data);
}
s.close();
}catch(SQLException ex)
{
System.out.println("Erreur" + ex);
}
}

void jComboBox1_actionPerformed(ActionEvent e)
{
ResultSet r = null;
TableModele tm = null;

jTable1.removeAll();
try
{
s = AccessPostgres.getConnection().createStatement(); String requete "SELECT nom, prenom FROM personne, eleve, enumere WHERE id_personne eleve.ref_personne AND eleve.ref_enumere = id_enumere AND enumere.libelle = " + "'" +
((String) jComboBox1.getSelectedItem()) + "';";
r = s.executeQuery(requete);
tm = new TableModele ( r );
this.jTable1.setModel ( tm );

}
catch(SQLException ex)
{
System.out.println("Erreur de jComboBox1_actionPerformed" + ex.getMessage());
}
}

public void addColumnCheckBox()
{

// jTable1.getModel().setValueAt();
// jTable1.setValueAt(new Boolean(false), 1, 1);
}

void jComboBox2_actionPerformed(ActionEvent e)
{
ResultSet r = null;
filsTableModel tm = null;

jTable1.removeAll();
try
{
s = AccessPostgres.getConnection().createStatement(); String requete "SELECT libelle FROM enumere WHERE ref_enumere id_enumere AND libelle = '" +
((String) jComboBox1.getSelectedItem()) + "';";
r = s.executeQuery(requete);
tm = new filsTableModel ( r );
this.jTable1.setModel ( tm );
}
catch(SQLException ex)
{
System.out.println("Erreur de jComboBox1_actionPerformed" + ex.getMessage());
}

}

}

class FenetreGenPresence_jComboBox1_actionAdapter implements java.awt.event.ActionListener
{
FenetreGenPresence adaptee;

FenetreGenPresence_jComboBox1_actionAdapter(FenetreGenPresence adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jComboBox1_actionPerformed(e);
}
}

class FenetreGenPresence_jComboBox2_actionAdapter implements java.awt.event.ActionListener
{
FenetreGenPresence adaptee;

FenetreGenPresence_jComboBox2_actionAdapter(FenetreGenPresence adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jComboBox2_actionPerformed(e);
}
}

ensuite la class TableModele

public class TableModele extends AbstractTableModel
{
private ResultSet rs = null;
private int rowcount = 0;
public TableModele(ResultSet r)
{
this.rs = r;
try
{
this.rs.last();
this.rowcount = this.rs.getRow();
this.rs.first();
} catch (Exception e)
{
this.rs = null;
}

}
public int getRowCount()
{
return (this.rowcount );
}
public int getColumnCount()
{
try
{
return (this.rs.getMetaData().getColumnCount() + 1);
} catch (Exception e)
{
return (0);
}
}
public Object getValueAt(int row, int col)
{
try
{
this.rs.first();
this.rs.relative(row);
return ((Object) this.rs.getString(col));
} catch (Exception e)
{
return (null);
}
}

public boolean IsCellEditable(int row, int col)
{
if (col > 1 )
return false;
else
return true;
}

public ResultSet getResultSet()
{
return rs;
}
}
voila, c deja mieux.....

Dans cet etat la, il m'affiche une colonne avec des champs vide, la ou je veux mettre mes JCheckBox, et les colonnes avec les champs recuperer de la base de donnee...

Voila, help.....

Merci
0
bastet1978 Messages postés 54 Date d'inscription lundi 29 septembre 2003 Statut Membre Dernière intervention 4 décembre 2005
25 août 2005 à 15:33
Salut, bon c un peux tard je crois mais bon ca peux servir aux autres alors:

sun met a disposition des doc tres completes(pour une utilisation simple; apres faut chercher un peux plus).

pour les JTable : http://java.sun.com/docs/books/tutorial/uiswing/components/table.html



Et pour les JCheckBox dns ls JTable, c fait directement par le JTable si vous enregistrez la donnee de cette facon: "new Boolean(true)" dans l'Object[][] que vous utilisez pour votre JTable. Maintenant le problème qui se pause est que souvent on réécrit un AbstractTableModel et que l'on oublie une partie importante qui permet a la JTable de savoir quelles données elle doit traiter dans cette cellule:

public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}

nb:
Voici les formats que le JTable peut afficher de base.
Apres il faut rajouter quelques lignes :) pour un renderer plus
complexe.



<li>
Boolean
— rendered with a check box.
</li><li>
Number
— rendered by a right-aligned label.
</li><li>
Double
,
Float
— same as
Number
,
but the object-to-text translation is performed by a
NumberFormat
http://java.sun.com/j2se/5.0/docs/api/java/text/NumberFormat.html instance
(using the default number format for the current locale).
</li><li>
Date
— rendered by a label,
with the object-to-text translation performed by a
DateFormat
http://java.sun.com/j2se/5.0/docs/api/java/text/DateFormat.html instance
(using a short style for the date and time).
</li><li>
ImageIcon
,
Icon

rendered by a centered label.
</li><li>
Object
— rendered by a label that displays the object's
string value.</li>
0
Rejoignez-nous