voila tt le code de ma page
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Vector;
//import javax.swing.BoxLayout;
//import java.util.Random;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
public class Dico extends JInternalFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static final int PER_PAGE = 50;
private String sBase;
private MTA win;
private JTable tab_List;
private JScrollPane js_Center;
private JPanel jp;
private JComboBox jcb_tri, jcb_tables;
private JLabel pages_Status;
private JButton jb_previous, jb_next;
private int current_page;
private JTextField jtf;
private String[] fieldName;
//Constructeur
Dico(MTA m,String base){
win=m;
sBase=base;
//construction de l'interface
jp=new JPanel(new BorderLayout());
jp.setPreferredSize(new Dimension(900,600));
JPanel jp1=new JPanel(new BorderLayout());
JButton jb=new JButton("Close");
jb.setName("Close");
jb.addActionListener(this);
jp1.add(jb,BorderLayout.EAST);
JPanel jp2=new JPanel();
BoxLayout bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jb_previous=new JButton("Previous");
jb_previous.setName("Previous");
jb_previous.addActionListener(this);
jp2.add(jb_previous);
jp2.add(Box.createHorizontalStrut(15));
jtf = new JTextField(2);
jp2.add(jtf);
jp2.add(Box.createHorizontalStrut(5));
JButton jb_go = new JButton("Go");
jb_go.setName("Go");
jb_go.addActionListener(this);
jp2.add(jb_go);
jp2.add(Box.createHorizontalStrut(15));
jb_next=new JButton("Next");
jb_next.setName("Next");
jb_next.addActionListener(this);
jp2.add(jb_next);
jp1.add(jp2, BorderLayout.WEST);
pages_Status = new JLabel("", JLabel.CENTER);
jp1.add(pages_Status, BorderLayout.CENTER);
jp.add(jp1,BorderLayout.SOUTH);
jp1 = new JPanel(new BorderLayout());
jp2 = new JPanel();
bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jcb_tables = new JComboBox();
jcb_tables.setName("tables");
jcb_tri = new JComboBox();
jcb_tri.setName("tri");
//jcb_tri.addActionListener(this);
//jcb_tri.addItemListener(this);
jp2.add(new JLabel("Table:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tables);
jp2.add(Box.createHorizontalStrut(15));
jp2.add(new JLabel("Order by:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tri);
jp1.add(jp2,BorderLayout.WEST);
jp.add(jp1, BorderLayout.NORTH);
//initialisation de la table, tri par item_code
initTable(sBase,"detail",0,PER_PAGE,"item_code");
//jcb_tri.addItemListener(this);
//ajout de la liste des tables
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst = my.executeQuery("SELECT DISTINCT table_name from headers;");
while(my.goNextRst(rst)){
jcb_tables.addItem(my.getValueRst(rst,"table_name"));
}
rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
my.closeConnection();
//ajout du listener apres la création de la liste
jcb_tri.addActionListener(this);
jcb_tables.addActionListener(this);
current_page=1;
this.getContentPane().add(jp);
//parametre de la fenetre
//this.closable=false;
this.iconable=true;
this.maximizable=true;
this.resizable=true;
this.setTitle("List of Items in the dictionnary");
this.pack();
this.setVisible(true);
Tools.centerInterFrame(this,win);
}
//Génération de la table
private void initTable(String base, String table, int lim_basse, int nb_records, String order){
String s;
int nb_lines, nb_pages;
//connection à la base de donnée
JDBCAdapter data=new JDBCAdapter(
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/"+base,
"root",
"");
tab_List=new JTable();
//on recupere le nombre de lignes et le nombre de pages
nb_lines = getNbLines(base,"ta_"+table);
nb_pages = nb_lines/PER_PAGE+1;
pages_Status.setText("Page "+(lim_basse/PER_PAGE+1)+" of "+String.valueOf(nb_pages));
jtf.setText(String.valueOf(lim_basse/PER_PAGE+1));
s="SELECT * FROM ta_" + table +" ORDER BY " + order + " LIMIT " + lim_basse + "," +nb_records;
data.executeQuery(s);
//on recupere le nom des champs de la table selectionner
MysqlAccess my = new MysqlAccess(base);
my.startConnection();
s = "SELECT name FROM headers WHERE table_name='" + table + "' ORDER BY position";
int rst = my.executeQuery(s);
String[] chmp = new String[my.getResultCount(rst)];
int i=0;
while(my.goNextRst(rst))
{
chmp[i] = my.getValueRst(rst,"name");
i++;
}
my.closeConnection();
//on défini le noms des colonnes
data.setColumnNames(chmp);
data.fireTableStructureChanged();
//on insere les données dans notre tableau
tab_List.setModel(data);
//empeche de pouvoir réorganiser les colonnes
tab_List.getTableHeader().setReorderingAllowed(false);
js_Center=new JScrollPane(tab_List);
jp.add(js_Center,BorderLayout.CENTER);
js_Center.setPreferredSize(new Dimension(300,200));
jb_previous.setEnabled(true);
jb_next.setEnabled(true);
if(lim_basse==0)
jb_previous.setEnabled(false);
if((lim_basse+PER_PAGE)>nb_lines)
jb_next.setEnabled(false);
}
//fonction permettant de retourner le nombre de ligne d'une table d'une base
private int getNbLines(String base, String table)
{
String s;
int rst,nblines;
MysqlAccess my=new MysqlAccess(base);
my.startConnection();
s="SELECT COUNT(*) AS nb_lines FROM "+table;
rst = my.executeQuery(s);
my.goNextRst(rst);
nblines = Integer.valueOf(my.getValueRst(rst,"nb_lines"));
my.closeConnection();
return(nblines);
}
public void actionPerformed (ActionEvent evt){
String s;
s=evt.getSource().getClass().toString();
//détection de la selection d'un critere de tri ou d'une table à afficher
if(s.equals("class javax.swing.JComboBox"))
{
JComboBox cb = (JComboBox)evt.getSource();
String x = cb.getName();
if(x.equals("tri"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
if(x.equals("tables"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
jcb_tri.removeActionListener(this);
jcb_tri.removeAllItems();
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
jcb_tri.addActionListener(this);
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
}
//détection des actions sur les boutons
if (s.equals("class javax.swing.JButton")){
JButton c = (JButton)evt.getSource();
String x = c.getName();
if(x.equals("Close")){
// fermetutre
//this.closable=true;
//win.setStatus("List of Items in the dictionnary").close();
//this.dispose();
win.setStatus("List of Items in the dictionnary");
this.setVisible(false);
// win.closeMTA();
}
if(x.equals("Next"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*current_page,PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page++;
this.validate();
}
if(x.equals("Previous"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(current_page-2),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page--;
this.validate();
}
if(x.equals("Go"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(Integer.valueOf(jtf.getText())-1),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page = Integer.valueOf(jtf.getText());
this.validate();
}
}
}
}
voila tt le code de ma page
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Vector;
//import javax.swing.BoxLayout;
//import java.util.Random;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
public class Dico extends JInternalFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static final int PER_PAGE = 50;
private String sBase;
private MTA win;
private JTable tab_List;
private JScrollPane js_Center;
private JPanel jp;
private JComboBox jcb_tri, jcb_tables;
private JLabel pages_Status;
private JButton jb_previous, jb_next;
private int current_page;
private JTextField jtf;
private String[] fieldName;
//Constructeur
Dico(MTA m,String base){
win=m;
sBase=base;
//construction de l'interface
jp=new JPanel(new BorderLayout());
jp.setPreferredSize(new Dimension(900,600));
JPanel jp1=new JPanel(new BorderLayout());
JButton jb=new JButton("Close");
jb.setName("Close");
jb.addActionListener(this);
jp1.add(jb,BorderLayout.EAST);
JPanel jp2=new JPanel();
BoxLayout bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jb_previous=new JButton("Previous");
jb_previous.setName("Previous");
jb_previous.addActionListener(this);
jp2.add(jb_previous);
jp2.add(Box.createHorizontalStrut(15));
jtf = new JTextField(2);
jp2.add(jtf);
jp2.add(Box.createHorizontalStrut(5));
JButton jb_go = new JButton("Go");
jb_go.setName("Go");
jb_go.addActionListener(this);
jp2.add(jb_go);
jp2.add(Box.createHorizontalStrut(15));
jb_next=new JButton("Next");
jb_next.setName("Next");
jb_next.addActionListener(this);
jp2.add(jb_next);
jp1.add(jp2, BorderLayout.WEST);
pages_Status = new JLabel("", JLabel.CENTER);
jp1.add(pages_Status, BorderLayout.CENTER);
jp.add(jp1,BorderLayout.SOUTH);
jp1 = new JPanel(new BorderLayout());
jp2 = new JPanel();
bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jcb_tables = new JComboBox();
jcb_tables.setName("tables");
jcb_tri = new JComboBox();
jcb_tri.setName("tri");
//jcb_tri.addActionListener(this);
//jcb_tri.addItemListener(this);
jp2.add(new JLabel("Table:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tables);
jp2.add(Box.createHorizontalStrut(15));
jp2.add(new JLabel("Order by:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tri);
jp1.add(jp2,BorderLayout.WEST);
jp.add(jp1, BorderLayout.NORTH);
//initialisation de la table, tri par item_code
initTable(sBase,"detail",0,PER_PAGE,"item_code");
//jcb_tri.addItemListener(this);
//ajout de la liste des tables
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst = my.executeQuery("SELECT DISTINCT table_name from headers;");
while(my.goNextRst(rst)){
jcb_tables.addItem(my.getValueRst(rst,"table_name"));
}
rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
my.closeConnection();
//ajout du listener apres la création de la liste
jcb_tri.addActionListener(this);
jcb_tables.addActionListener(this);
current_page=1;
this.getContentPane().add(jp);
//parametre de la fenetre
//this.closable=false;
this.iconable=true;
this.maximizable=true;
this.resizable=true;
this.setTitle("List of Items in the dictionnary");
this.pack();
this.setVisible(true);
Tools.centerInterFrame(this,win);
}
//Génération de la table
private void initTable(String base, String table, int lim_basse, int nb_records, String order){
String s;
int nb_lines, nb_pages;
//connection à la base de donnée
JDBCAdapter data=new JDBCAdapter(
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/"+base,
"root",
"");
tab_List=new JTable();
//on recupere le nombre de lignes et le nombre de pages
nb_lines = getNbLines(base,"ta_"+table);
nb_pages = nb_lines/PER_PAGE+1;
pages_Status.setText("Page "+(lim_basse/PER_PAGE+1)+" of "+String.valueOf(nb_pages));
jtf.setText(String.valueOf(lim_basse/PER_PAGE+1));
s="SELECT * FROM ta_" + table +" ORDER BY " + order + " LIMIT " + lim_basse + "," +nb_records;
data.executeQuery(s);
//on recupere le nom des champs de la table selectionner
MysqlAccess my = new MysqlAccess(base);
my.startConnection();
s = "SELECT name FROM headers WHERE table_name='" + table + "' ORDER BY position";
int rst = my.executeQuery(s);
String[] chmp = new String[my.getResultCount(rst)];
int i=0;
while(my.goNextRst(rst))
{
chmp[i] = my.getValueRst(rst,"name");
i++;
}
my.closeConnection();
//on défini le noms des colonnes
data.setColumnNames(chmp);
data.fireTableStructureChanged();
//on insere les données dans notre tableau
tab_List.setModel(data);
//empeche de pouvoir réorganiser les colonnes
tab_List.getTableHeader().setReorderingAllowed(false);
js_Center=new JScrollPane(tab_List);
jp.add(js_Center,BorderLayout.CENTER);
js_Center.setPreferredSize(new Dimension(300,200));
jb_previous.setEnabled(true);
jb_next.setEnabled(true);
if(lim_basse==0)
jb_previous.setEnabled(false);
if((lim_basse+PER_PAGE)>nb_lines)
jb_next.setEnabled(false);
}
//fonction permettant de retourner le nombre de ligne d'une table d'une base
private int getNbLines(String base, String table)
{
String s;
int rst,nblines;
MysqlAccess my=new MysqlAccess(base);
my.startConnection();
s="SELECT COUNT(*) AS nb_lines FROM "+table;
rst = my.executeQuery(s);
my.goNextRst(rst);
nblines = Integer.valueOf(my.getValueRst(rst,"nb_lines"));
my.closeConnection();
return(nblines);
}
public void actionPerformed (ActionEvent evt){
String s;
s=evt.getSource().getClass().toString();
//détection de la selection d'un critere de tri ou d'une table à afficher
if(s.equals("class javax.swing.JComboBox"))
{
JComboBox cb = (JComboBox)evt.getSource();
String x = cb.getName();
if(x.equals("tri"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
if(x.equals("tables"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
jcb_tri.removeActionListener(this);
jcb_tri.removeAllItems();
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
jcb_tri.addActionListener(this);
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
}
//détection des actions sur les boutons
if (s.equals("class javax.swing.JButton")){
JButton c = (JButton)evt.getSource();
String x = c.getName();
if(x.equals("Close")){
// fermetutre
//this.closable=true;
//win.setStatus("List of Items in the dictionnary").close();
//this.dispose();
win.setStatus("List of Items in the dictionnary");
this.setVisible(false);
// win.closeMTA();
}
if(x.equals("Next"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*current_page,PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page++;
this.validate();
}
if(x.equals("Previous"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(current_page-2),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page--;
this.validate();
}
if(x.equals("Go"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(Integer.valueOf(jtf.getText())-1),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page = Integer.valueOf(jtf.getText());
this.validate();
}
}
}
}