Kilkenny95
Messages postés151Date d'inscriptionlundi 24 septembre 2018StatutMembreDernière intervention31 août 2020
-
14 mars 2019 à 16:29
tarek_dotzero
Messages postés817Date d'inscriptionjeudi 19 juillet 2007StatutMembreDernière intervention12 avril 2022
-
25 mars 2019 à 11:24
Bonjour à toutes et à tous,
J'ai créer un Jtable "jtable" dans lequel se trouvent des données d'une bdd MySQL.
Cependant j'ai quelques problèmes :
Lorsque je surprime un élément de ma bdd (et donc de mon tableau), l'élement de se supprime pas dynamiquement (Alors que lorsque j'ajoute une données, là, aucun soucis.
Second problème, l'affiche de ma table est vraiment degeu lorsque ma Jframe devient trop petit pour afficher toutes les données :
Je n'arrive pas à afficher ce foutu Scrollbar.
Si quelqu'un peut m'aider merci
Code de mon interface :
public class Presentation2 {
ProductList productList = new ProductList();
public Presentation2(){
JFrame presentation =new JFrame("Database MPSI FM");
JPanel main = new JPanel();
presentation.setSize(900,500);
presentation.setResizable(true);
presentation.setLocationRelativeTo(null);
presentation.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
presentation.setContentPane(main);
presentation.setVisible(true);
main.setLayout(new BorderLayout());
main.setBackground(Color.lightGray);
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
JPanel centernorth = new JPanel();
centernorth.setLayout(new FlowLayout(FlowLayout.RIGHT));
JPanel middlecenter = new JPanel();
middlecenter.setLayout(new GridBagLayout());
GridBagConstraints gb = new GridBagConstraints();
JPanel southcenter = new JPanel();
southcenter.setLayout(new FlowLayout(FlowLayout.RIGHT));
JPanel southleft = new JPanel();
southleft.setLayout(new FlowLayout(FlowLayout.LEFT));
JScrollPane js = new JScrollPane(productList.jTable);
gb.gridy = 0;
gb.gridx = 0;
middlecenter.add(productList.jTable.getTableHeader(), gb);
gb.gridy = 1;
gb.gridx = 0;
middlecenter.add(productList.jTable, gb);
centernorth.add(productList.ajouter);
centernorth.add(productList.supprimer);
centernorth.add(productList.modifier);
southleft.add(productList.addNode);
southleft.add(productList.removeNode);
southleft.add(productList.nomAddedNode);
centernorth.add(productList.recherche);
productList.arbre.setPreferredSize(new Dimension(300, 600));
center.add(centernorth,BorderLayout.NORTH);
center.add(southcenter,BorderLayout.SOUTH);
center.add(middlecenter,BorderLayout.CENTER);
main.add(productList.arbre,BorderLayout.WEST);
main.add(center,BorderLayout.CENTER);
main.add(southleft, BorderLayout.SOUTH);
presentation.setVisible(true);
}
}
et code ma JTable :
public ProductList() {
nom_texte.setPreferredSize(new Dimension(100,20));
recherche.setPreferredSize(new Dimension(100, 20));
model.addColumn("id");
model.addColumn("name");
model.addColumn("localisation");
model.addColumn("historique");
model.addColumn("etat");
model.addColumn("PDF");
recherche.setText("rechercher");
recherche.setForeground(Color.gray);
recherche.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
recherche.setText("");
recherche.setForeground(Color.black);
}
});
nomAddedNode.setText("Ajouter niveau");
nomAddedNode.setForeground(Color.gray);
nomAddedNode.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
nomAddedNode.setText("");
nomAddedNode.setForeground(Color.black);
}
});
DefaultTableModel model = (DefaultTableModel) jTable.getModel();
jTable.setPreferredSize(new Dimension(450,350));
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.setVisible(true);
Jtreebdd();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bdd_mspifm_mysql?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC", "admin", "mspifm46");
PreparedStatement ps = con.prepareStatement("SELECT * FROM product");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
model.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)});
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
supprimer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == supprimer){
int rowSelected = jTable.getSelectedRow();
int idSelected = (int) jTable.getValueAt(rowSelected, 0);
System.out.println(idSelected);
ProductDaoImpl pdi = new ProductDaoImpl();
pdi.delete(idSelected);
System.out.println("element supprimer: "+idSelected);
model.removeRow(rowSelected);
}
}
});
ajouter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==ajouter){
JFrame ajouter_data = new JFrame("Ajouter données");
JPanel main = new JPanel();
JButton valider = new JButton("OK");
valider.setPreferredSize(new Dimension(60,20));
JTextField name = new JTextField("Nom");
name.setForeground(Color.gray);
name.setPreferredSize(new Dimension(100,20));
name.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
name.setText("");
name.setForeground(Color.black);
}
});
JTextField historique = new JTextField("Historique");
historique.setForeground(Color.gray);
historique.setPreferredSize(new Dimension(100,20));
historique.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
historique.setText("");
historique.setForeground(Color.black);
}
});
ajouter_data.setSize(400,300);
ajouter_data.setResizable(true);
ajouter_data.setLocationRelativeTo(null);
ajouter_data.setVisible(true);
ajouter_data.setContentPane(main);
main.add(name);
main.add(historique);
main.add(valider);
valider.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == valider){
String getName = name.getText();
String getHistorique = historique.getText();
ProductDaoImpl pdi = new ProductDaoImpl();
Product product = new Product(getName, "", getHistorique,""); //insertion donnees dans table product
pdi.insert(product); //insertion donnees dans table product
model.addRow(new Object[]{product.getId(), product.getNameProduct(), product.getLocalisation(), product.getHistorique()});
ajouter_data.setVisible(false);
}
}
});
}
}
});
modifier.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == modifier){
JFrame modifier_data = new JFrame("Ajouter données");
JPanel main = new JPanel();
JButton valider = new JButton("OK");
valider.setPreferredSize(new Dimension(60,20));
JTextField name = new JTextField();
name.setPreferredSize(new Dimension(100,20));
JTextField historique = new JTextField();
historique.setPreferredSize(new Dimension(100,20));
int rowSelected = jTable.getSelectedRow();
int idSelected = (int) jTable.getValueAt(rowSelected, 0);
String nameSelected = (String) jTable.getValueAt(rowSelected, 1);
String historiqueSelected = (String) jTable.getValueAt(rowSelected, 3);
name.setText(nameSelected);
historique.setText(historiqueSelected);
valider.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == valider){
int rowSelected = jTable.getSelectedRow();
int idSelected = (int) jTable.getValueAt(rowSelected, 0);
ProductDaoImpl pdi = new ProductDaoImpl();
pdi.delete(idSelected);
System.out.println("element supprimer: "+idSelected);
model.removeRow(rowSelected);
String getName = name.getText();
String getHistorique = historique.getText();
Product product = new Product(getName, "", getHistorique,""); //insertion donnees dans table product
pdi.insert(product); //insertion donnees dans table product
model.addRow(new Object[]{product.getId(), product.getNameProduct(), product.getLocalisation(), product.getHistorique()});
modifier_data.setVisible(false);
Presentation presentation = new Presentation();
presentation.presentation.revalidate();
}
}
});
modifier_data.setSize(400,300);
modifier_data.setResizable(true);
modifier_data.setLocationRelativeTo(null);
modifier_data.setVisible(true);
modifier_data.setContentPane(main);
main.add(name);
main.add(historique);
main.add(valider);
}
}
});
}