-1

Iwould like to create a form using java eclipse. The problem is i cannot obtain the total added JLabel and JTextField.this is my code:

 class gestiontache extends JFrame{


JFrame f;
JPanel p1, p2, p3;
JTabbedPane tp;

JLabel l1, l2, l3,l4,l5;
JComboBox  tf3categor;
JComboBox  tf4Affiliation;
JComboBox  tf5montant;

JTextField tf1, tf2;
JScrollPane sp1;
JButton savebtn, resetbtn, editbtn;
private static String FILE = "c:/temp/DocumentPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
        Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
        Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
        Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
        Font.BOLD);
gestiontache() {
    f = new JFrame("Form");
    GridLayout lay1= new GridLayout(12, 2);
    GridLayout lay2= new GridLayout(5, 2);
    p1 = new JPanel(lay1);
    p2 = new JPanel(lay2);
    lay1.setHgap(5); //Cinq pixels d'espace entre les colonnes (H comme Horizontal)
    lay1.setVgap(5); //Cinq pixels d'espace entre les lignes (V comme Vertical) 
    lay2.setHgap(5);
    lay2.setVgap(5);
    tp = new JTabbedPane();
    l1 = new JLabel("Nom");
    l2 = new JLabel("Prénom");
    l3 = new JLabel("Catégorie");
    l4 = new JLabel("Affiliation");
    l5 = new JLabel("Montant à payer");
    tf1 = new JTextField(12);
    tf2 = new JTextField(12);
    tf3categor = new JComboBox( new String[] { "Medecin", "Technicien", "Pharmacien","Autre" });
    tf4Affiliation =new JComboBox( new String[] { "K", "T", "Sf","Gab","Toze","Med","Tat","Na","B","G","Si","Ga","Ke","Kr" });
    tf5montant = new JComboBox( new String[] { "15 Dinars", "30 Dinars"});
    savebtn = new JButton(" Ajouter ");
    resetbtn = new JButton(" Annuler");
    editbtn = new JButton(" Imprimer");

    p1.add(l1);
    p1.add(tf1);
    p1.add(l2);
    p1.add(tf2);
    p1.add(l3);
    p1.add(tf3categor);
    p1.add(l4);
    p1.add(tf4Affiliation);
    p1.add(l5);
    p1.add(tf5montant);
    p1.add(savebtn);
    p1.add(resetbtn);
    p2.add(l1);
    p2.add(tf1);
    p2.add(l2);
    p2.add(tf2);
    p2.add(editbtn);
    resetbtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            clear();
        }
    });
    savebtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String nom, prenom,categorie, affiliation, montant;
            nom = tf1.getText();
            prenom = tf2.getText();
            categorie=(String) tf3categor.getSelectedItem();
            affiliation=(String) tf4Affiliation.getSelectedItem();
            montant=(String) tf5montant.getSelectedItem();
            String url = "jdbc:mysql://localhost:3306/seminaire";
            String userid = "root";
            String password = "";
            try {
            Connection connection = DriverManager.getConnection(url,
                        userid, password);
            Statement st = connection.createStatement();

                if (nom != "" && prenom != ""&& categorie!= ""&& affiliation!= ""&& montant!= "") {
  st.executeUpdate("insert into participant values('" + nom
                            + "','" + prenom + "','" + categorie + "','"+affiliation+"','"+montant+"')");
 JOptionPane.showMessageDialog(null,"Données insérées avec succès");
                    clear();
                } else {
  JOptionPane.showMessageDialog(null, "merci de saisir vos données");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
            editbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
            String nom, prenom,categorie, affiliation, montant;
            nom = tf1.getText();
            prenom = tf2.getText();
            String url = "jdbc:mysql://localhost:3306/seminaire";
            String userid = "root";
            String password = "";
            try {
                Connection connection = DriverManager.getConnection(url,
                        userid, password);
                Statement st = connection.createStatement();

                if (nom != "" && prenom != "") {
                 ResultSet rs= st.executeQuery("SELECT * FROM participant 
                 WHERE nom=nom && prenom=prenom");
                    while (rs.next())
  {
    String nm = rs.getString("nom");
    String prnm = rs.getString("prenom");
    String cat = rs.getString("categorie");
    String afl=rs.getString("affiliation");
    String mnt=rs.getString("montant");  
    // print the results
    Document document = new Document();
    try {
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        //open
        document.open();
        Paragraph p = new Paragraph();
        p.add("Reçu");
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);
        Paragraph p2 = new Paragraph();
        p2.add(nm); //no alignment
        document.add(p2);
        Font f = new Font();
        f.setStyle(Font.BOLD);
        f.setSize(8);
        document.add(new Paragraph("This is my paragraph 3", f));
        //close
        document.close();
        System.out.println("Done");
        } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
         }
       }
       }    
       else {
      JOptionPane.showMessageDialog(null, "merci de saisir vos données");

                }

        }
            catch (Exception e)
            {
              System.err.println(e.getMessage());
            } 
          }});
        }
          void dis() {
       f.getContentPane().add(tp);
       tp.addTab("Ajouter participant", p1);
       tp.addTab("Imprimer attestation", p2);

        f.setSize(500, 400);
    f.setVisible(true);
    f.setResizable(true);
      }

     void clear()
    {
      tf1.setText("");
      tf2.setText("");
      tf3categor.setSelectedItem("");
      tf4Affiliation.setSelectedItem("");
      tf5montant.setSelectedItem("");
    } 

    public static void main(String z[]) {
    gestiontache data = new gestiontache();
    data.dis();
     }
        }        

` The problem here is the JLabel and JTextField(nom, prenom)don't appear in the form in order to insert or select from database. Have any idea how can i correct it please. Thank you

  • `if (nom != "" && prenom != "")` will not work. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. Also, you will get more help if you format your code properly. As it currently is, the code is quite difficult to understand. – VGR Oct 26 '17 at 12:54

1 Answers1

0
p2.add(l1);

p2.add(tf1);

p2.add(l2);

p2.add(tf2);

These above fields are added in both p1(Tab1) and p2(Tab2) Panels.

Thats why its not showing.

You must create seperate controls for both p1 and p2 panels. Don't reuse same controls in two panels.

For example:

l7 = new JLabel("Normal");

p2.add(l7);
Ansharja
  • 1,237
  • 1
  • 14
  • 37
jai
  • 98
  • 8