aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes !! Urgent !! Please hlp me ( problem jTable)!!!! thanks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "!! Urgent !! Please hlp me ( problem jTable)!!!! thanks" Watch "!! Urgent !! Please hlp me ( problem jTable)!!!! thanks" New topic
Author

!! Urgent !! Please hlp me ( problem jTable)!!!! thanks

mebarkia med
Greenhorn

Joined: Apr 04, 2004
Posts: 5
Hello!
I believe that I am null for the construction of the methods.
The following lines selects the first line of JTable.
�/.
ListSelectionModel selectionModel =
jTable1.getSelectionModel();
selectionModel.setSelectionInterval(0, 0);
selectionModel.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
�/.
I wanted to make a method to move in the lines of Jtable.
I made:
int nligne = 0, nligne1 = 0 ;
public void position(int nligne,int nligne1 ){
this.nligne =nligne;
this.nligne1=nligne1;
selectionModel = jTable1.getSelectionModel();
selectionModel.setSelectionInterval(nligne, nligne1);
selectionModel.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
}
then I call my method from a OnClick event.
the last line is:
// nbrLigne is the number of recording
position (nbrLigne, nbrLigne) ;
the first line is
position(0,0) ;
The following line is:
int lg = jTable1.getSelectedRow()+1;
if(lg== nbrLigne) suivant.setEnabled(false);
position(lg, lg) ;
The preceding line is:
int lg = jTable1.getSelectedRow()-1;
if (lg==0) precedent. setEnabled(false);
position(lg, lg) ;

I would like to know why this method does not function?
That it is part of the program that am spirit to develop�
Encourage me if it like to you has to finish it
thank you!
Tables and the source:

varcCodeContinent varcLibelContinent
----------------- -------------------------
CON0001 Afrique
CON0002 Am�rique
CON0003 Europe
CON0004 Asie
CON0005 Oc�anie
(5 ligne(s) affect�e(s))
nCodePays varcCodeContinent varcLibelPays
------------ ---------------------- ----------------
11000 CON0003 Espagne
11001 CON0005 Australie
11002 CON0001 Alg�rie
11003 CON0003 France
11004 CON0003 Italy
11005 CON0001 Inde
11006 CON0001 Mali
(7 ligne(s) affect�e(s))
/** CLasse Connexion et chargement du Driver */
import java.sql.*;
import java.util.Properties;
public class Connexion {
private static Connexion factory = new Connexion();
private String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
private String baseURL = "jdbc dbc:";
private String username = "";
private String password = "";
private boolean driverLoaded;
private Properties props;
private Connexion() {
try {
fillProperties();
Class.forName(driverName);
driverLoaded = true;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public static Connection getConnection(String dbName)
throws SQLException {
return factory.openConnection(dbName);
}
protected Connection openConnection(String dbName) throws SQLException {
checkDriverLoaded();
return DriverManager.getConnection(baseURL + dbName, props);
}
private void checkDriverLoaded() throws SQLException {
if (!driverLoaded) {
throw new SQLException("no driver: " + driverName);
}
}
private void fillProperties() {
if (props != null) {
return;
}
props = new Properties();
props.put("user", username);
props.put("password", password);
}
}

// Importer la liste des Continents dans un JComboBox
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class ListeContinents extends JFrame {
String StrContinent = "CON0001";
private Connection con;
private String dbName = "Jakarta_Base";
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
JComboBox ComboCont = new JComboBox();
private JLabel jLabel1 = new JLabel();
private JButton AjoutBtn = new JButton();
private JLabel jLabel2 = new JLabel();
public ListeContinents() {
try {
rempComboCont();
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jPanel1.setBackground(SystemColor.inactiveCaptionText);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(null);
ComboCont.setBounds(new Rectangle(94, 60, 247, 21));
ComboCont.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(ItemEvent e) {
ComboCont_itemStateChanged(e);
}
});
jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
jLabel1.setForeground(SystemColor.activeCaption);
jLabel1.setText("Continent :");
jLabel1.setBounds(new Rectangle(29, 65, 69, 17));
AjoutBtn.setFont(new java.awt.Font("Dialog", 1, 12));
AjoutBtn.setText(". . .");
AjoutBtn.setBounds(new Rectangle(350, 58, 23, 23));
AjoutBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AjoutBtn_actionPerformed(e);
}
});
jLabel2.setText("jLabel2");
jLabel2.setBounds(new Rectangle(45, 133, 320, 26));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, null);
jPanel1.add(ComboCont, null);
jPanel1.add(AjoutBtn, null);
jPanel1.add(jLabel2, null);
}
public void rempComboCont()/* M�thode pour Remplir le combo continent*/
{
DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
try {
con = Connexion.getConnection(dbName);
String sql = " select varcCodeContinent, varclibelcontinent from continent ";
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
RechStrStr Rech = new RechStrStr(rs.getString(1),
rs.getString("varclibelcontinent"));
comboModel.addElement(Rech);
}
stmt.close();
}
catch (SQLException e) { e.printStackTrace(); }
finally {
try {
con.close();
}
catch (Throwable t) { // ignore
}
// remplir le ComboBox
ComboCont = new JComboBox(comboModel);
}
}
void AjoutBtn_actionPerformed(ActionEvent e) {

AffichePays AP = new AffichePays(StrContinent);
AP.setSize(386,316);
AP.setResizable(false);
AP.setLocation(360,100);
AP.setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new ListeContinents();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(400,200);
// frame.pack();
frame.setVisible(true);
}
void ComboCont_itemStateChanged(ItemEvent e) {
StrContinent = ComboCont.getSelectedItem().toString();
System.out.println(StrContinent);
}
}
/*****************************************************************************************************************************************************/
// Affiche les pays avec un navigateur
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
public class AffichePays extends JFrame {
String ValCont;
int nbrLigne=0;
int nligne = 0,nligne1 = 0;
// Etablir la connexion � la base de donn�es
private Connection con;
private String dbName = "Jakarta_Base";
// pour le JTable
private Statement statement, stm;
private ResultSet resultSet, res;
private ResultSetMetaData rsmd;
private JTable jTable1;
ListSelectionModel selectionModel ;
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JPanel jPanel3 = new JPanel();
private JButton premier = new JButton();
private JButton precedent = new JButton();
private JButton suivant = new JButton();
private JButton dernier = new JButton();
private JButton ajouter = new JButton();
private JButton supprimer = new JButton();
private JButton valider = new JButton();
private JTextField code = new JTextField();
private JTextField pays = new JTextField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JScrollPane jScrollPane1 = new JScrollPane();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel6 = new JLabel();
// private JTable jTable1 = new JTable();
public AffichePays(String StrContinent) {
try {
jbInit();
ValCont = StrContinent;
// afficher les enregistrements dans la tableau du pays
getTable();
NbrEnreg();
System.out.println("Nombre de ligne est : .........> "+nbrLigne);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setBackground(Color.lightGray);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
this.getContentPane().setLayout(null);
jPanel1.setBackground(new Color(130, 160, 230));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 373, 43));
jPanel1.setLayout(null);
jPanel2.setBackground(new Color(27, 85, 135));
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(4, 50, 373, 185));
jPanel2.setLayout(null);
jPanel3.setBounds(new Rectangle(3, 238, 374, 49));
jPanel3.setLayout(null);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setBackground(new Color(130, 160, 230));
premier.setBounds(new Rectangle(86, 9, 24, 27));
premier.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
premier_actionPerformed(e);
}
});
premier.setBackground(new Color(130, 160, 230));
premier.setFont(new java.awt.Font("Dialog", 1, 16));
premier.setBorder(BorderFactory.createRaisedBevelBorder());
premier.setActionCommand("");
premier.setHorizontalTextPosition(SwingConstants.CENTER);
premier.setText("I<");
precedent.setBounds(new Rectangle(116, 9, 24, 27));
precedent.setBackground(new Color(130, 160, 230));
precedent.setFont(new java.awt.Font("Dialog", 1, 16));
precedent.setBorder(BorderFactory.createRaisedBevelBorder());
precedent.setActionCommand("");
precedent.setHorizontalTextPosition(SwingConstants.CENTER);
precedent.setText("<");
suivant.setActionCommand("");
suivant.setBorder(BorderFactory.createRaisedBevelBorder());
suivant.setBackground(new Color(130, 160, 230));
suivant.setFont(new java.awt.Font("Dialog", 1, 16));
suivant.setBounds(new Rectangle(146, 9, 24, 27));
suivant.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
suivant_actionPerformed(e);
}
});
suivant.setHorizontalTextPosition(SwingConstants.CENTER);
suivant.setText(">");
ajouter.setActionCommand("");
ajouter.setBorder(BorderFactory.createRaisedBevelBorder());
ajouter.setBackground(new Color(130, 160, 230));
ajouter.setFont(new java.awt.Font("Dialog", 1, 16));
ajouter.setBounds(new Rectangle(206, 9, 24, 27));
ajouter.setHorizontalTextPosition(SwingConstants.CENTER);
ajouter.setText("+");
supprimer.setActionCommand("");
supprimer.setBorder(BorderFactory.createRaisedBevelBorder());
supprimer.setBackground(new Color(130, 160, 230));
supprimer.setFont(new java.awt.Font("Dialog", 1, 16));
supprimer.setBounds(new Rectangle(236, 9, 24, 27));
supprimer.setHorizontalTextPosition(SwingConstants.CENTER);
supprimer.setText("-");
dernier.setHorizontalTextPosition(SwingConstants.CENTER);
dernier.setText(">I");
dernier.setActionCommand("");
dernier.setBorder(BorderFactory.createRaisedBevelBorder());
dernier.setBackground(new Color(130, 160, 230));
dernier.setFont(new java.awt.Font("Dialog", 1, 16));
dernier.setBounds(new Rectangle(176, 9, 24, 27));
valider.setHorizontalTextPosition(SwingConstants.CENTER);
valider.setText("ok");
valider.setBounds(new Rectangle(266, 9, 24, 27));
valider.setBackground(new Color(130, 160, 230));
valider.setFont(new java.awt.Font("Dialog", 1, 16));
valider.setBorder(BorderFactory.createRaisedBevelBorder());
valider.setActionCommand("");
code.setText("");
code.setBounds(new Rectangle(49, 154, 63, 21));
pays.setText("");
pays.setBounds(new Rectangle(168, 154, 199, 21));
jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
jLabel1.setForeground(SystemColor.inactiveCaptionBorder);
jLabel1.setText("Code :");
jLabel1.setBounds(new Rectangle(9, 159, 41, 17));
jLabel2.setFont(new java.awt.Font("Dialog", 1, 12));
jLabel2.setForeground(SystemColor.inactiveCaptionBorder);
jLabel2.setText("Pays :");
jLabel2.setBounds(new Rectangle(128, 159, 41, 17));
jScrollPane1.getViewport().setBackground(Color.white);
jScrollPane1.setBorder(BorderFactory.createLoweredBevelBorder());
jScrollPane1.setBounds(new Rectangle(6, 7, 360, 136));
jLabel3.setText("Fichier pays");
jLabel3.setBounds(new Rectangle(39, 2, 223, 39));
jLabel3.setFont(new java.awt.Font("Dialog", 1, 25));
jLabel4.setText("Fichier pays");
jLabel4.setBounds(new Rectangle(38, 1, 222, 34));
jLabel4.setForeground(Color.white);
jLabel4.setFont(new java.awt.Font("Dialog", 1, 25));
jLabel6.setForeground(Color.white);
jLabel6.setText("temoin");
jLabel6.setBounds(new Rectangle(221, 10, 143, 28));
this.getContentPane().add(jPanel1, null);
jPanel1.add(jLabel4, null);
jPanel1.add(jLabel3, null);
jPanel1.add(jLabel6, null);
this.getContentPane().add(jPanel2, null);
jPanel2.add(jLabel1, null);
jPanel2.add(jLabel2, null);
jPanel2.add(code, null);
jPanel2.add(pays, null);
jPanel2.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jPanel3, null);
jPanel3.add(suivant, null);
jPanel3.add(dernier, null);
jPanel3.add(ajouter, null);
jPanel3.add(supprimer, null);
jPanel3.add(valider, null);
jPanel3.add(premier, null);
jPanel3.add(precedent, null);
}
// Compte le nombre de lignes (Enregistrements)
private void NbrEnreg()
{
System.out.println(ValCont);
String valCt = ValCont.substring(0,7);
String requete="";
try {
requete = "select count(*) from pays "+
" where varcCodeContinent = '"+valCt+"'";
con = Connexion.getConnection(dbName);
statement = con.createStatement();
resultSet = statement.executeQuery( requete );
resultSet.next();
nbrLigne = resultSet.getInt(1);
// fermeture de la BD
resultSet.close();
statement.close();
con.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void getTable()
{
System.out.println(ValCont);
String valCt = ValCont.substring(0,7);
String requete="";
try {
requete = "select nCodePays as Code,varclibelPays as Pays from pays "+
" where varcCodeContinent = '"+valCt+"'";
con = Connexion.getConnection(dbName);
statement = con.createStatement();
resultSet = statement.executeQuery( requete );
afficheResultSet( resultSet );
// AffTextFieldEnrg(resultSet);
// fermeture de la BD
resultSet.close();
statement.close();
con.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void afficheResultSet( ResultSet rs )
throws SQLException
{
// Positionner dans le premier enregistrement
boolean moreRecords = rs.next();
if (moreRecords==false){
jTable1 = new JTable(4, 4 );
jScrollPane1.remove(jTable1);
// JTable jTable1 = new JTable( rows, columnHeads );
jScrollPane1.getViewport().add(jTable1, null);
jTable1.repaint();}
else{
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// Get des en-t�tes de colonnes.
rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
// Afficher une table avec le contenu de la s�rie de r�sultats.
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// Affiche la table avec le resultat
jTable1 = new JTable(4, 4 );
jScrollPane1.remove(jTable1);
JTable jTable1 = new JTable( rows, columnHeads );
jScrollPane1.getViewport().add(jTable1, null);
jTable1.repaint();
//Selectionner la premiere ligne du tableau
/* ListSelectionModel selectionModel =
jTable1.getSelectionModel();
selectionModel.setSelectionInterval(0, 0);
selectionModel.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
System.out.println("ligne selectionner est :"+jTable1.getSelectedRow());*/
jTable1.setRowSelectionInterval(0, 0);

setFixedRow();
// jTable1.setRowSelectionInterval(0, 0);
//position(nbrLigne,nbrLigne);
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}}
void setFixedRow() {
int index=jTable1.getSelectedRow()+2;
System.out.println("index :"+index);
jTable1.setRowSelectionInterval(index, index);
}
// methode de selection de lignes
public void position(int nligne,int nligne1 ){
this.nligne =nligne;
this.nligne1=nligne1;
selectionModel = jTable1.getSelectionModel();
selectionModel.setSelectionInterval(nligne, nligne1);
selectionModel.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);

}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i ){
currentRow.addElement( rs.getString( i ) );

}
return currentRow;
}
// Afficher les enregistrement dans JTextField
public void AffTextFieldEnrg(){
try {
System.out.println(ValCont);
String valCt = ValCont.substring(0,7);
String requete="";
requete = "select nCodePays as Code,varclibelPays as Pays from pays "+
" where varcCodeContinent = '"+valCt+"'";
con = Connexion.getConnection(dbName);
stm = con.createStatement();
res = stm.executeQuery( requete );
if(res.next()==true)
{
AfEnrg(res);
premier.setEnabled(false);
precedent.setEnabled(false);}
else{
code.setText("");
pays.setText("");
}
// si en ferme le Statement en peut pas parcourir !!!
/// stm.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
// methode de remplissage
public void AfEnrg(ResultSet res){
try
{
code.setText(res.getString(1));
pays.setText(res.getString(2));
}
catch (Exception e){}
}
void this_windowOpened(WindowEvent e) {
AffTextFieldEnrg();
jLabel6.setText(ValCont);
valider.setEnabled(false);
System.out.println("ouverture de la fenetre creation pays");
}
void premier_actionPerformed(ActionEvent e) {
try {
suivant.setEnabled(true);
precedent.setEnabled(false);
res.first();
}
catch(Exception ee) {}
AffTextFieldEnrg();
}
void suivant_actionPerformed(ActionEvent e) {
try {
jTable1.clearSelection();

jTable1.setRowSelectionInterval(1, 1);
jTable1.repaint();
premier.setEnabled(true);
precedent.setEnabled(true);
res.next();
}
catch(Exception ee) {}
AfEnrg(res);
}
}
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281

This is a simplified version of your program to show that the selection works ok. Hope it helps.


The nice thing about Standards is that there are so many to choose from!
mebarkia med
Greenhorn

Joined: Apr 04, 2004
Posts: 5
Thank you for the answer
I tested this method,
May it does not function one click on a button. (event).
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
I tried to compile your code but could not find class RechStrStr.
You really need to make a working copy to solve the problem you have and then add the other code.
One way would be to set the data up in a set of arrays, make this work properly, and only then add the database part.
Sorry I cannot help you more.
Ed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: !! Urgent !! Please hlp me ( problem jTable)!!!! thanks
 
Similar Threads
Problems with ImageIcon when focus is lost
Updating values in a jTable
How to show the data in JTable using RandomAccessFile as well add, delete, update function
problem clicking jbuttons in jtable
Split Pane not working