Ouvrir une classe à partir d'un JButton
Bonjour,
Je suis débutant en Java et je suis entrain de développer une petite appli avec laquelle j'apprends en même temps le langage Java.
J'ai 2 fichiers. L'un s'appelle FirstView.java dans laquelle il y a un Jbutton qui doit appeler une autre classe (sous forme de Jframe) qui s'appelle InsertData.Java. Donc quand j'appuie sur le button, ça m'ouvre la fenêtre (InsertData.java) qui va me permettre de saisir des données.
Je cherche depuis hier sur le net, je ne vois pas.:calim2::calim2::calim2:
Merci de votre aide.
Bouton dans FirstView.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
JButton NewInsert = new JButton();
NewInsert.setText("New Admin");
NewInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
/*InsertData Win = new InsertData();
Win.pack();
Win.setVisible(true);*/
//JOptionPane.showMessageDialog(null, "TEST");
//new InsertData();
}
}); |
InsertData.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
package viewer;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.lynda.javatraining.db.ConnectionManager;
@SuppressWarnings("serial")
public class InsertData extends JFrame {
public static void main(String[] args) {
final JFrame f = new JFrame();
f.setTitle("Nouvel Admin");
JPanel p1= new JPanel(new GridLayout(5,2));
//JTabbedPane tp = new JTabbedPane();
JLabel l1 = new JLabel("New Name:");
JLabel l2 = new JLabel("New Password:");
final JTextField tf1 = new JTextField(12);
final JTextField tf2 = new JTextField(12);
JButton savebtn = new JButton("Add");
JButton resetbtn = new JButton("Reset");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(savebtn);
p1.add(resetbtn);
savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
Connection conn = ConnectionManager.getInstance().getConnection();
String s1 = tf1.getText();
String s2 = tf2.getText();
String sql = "INSERT INTO admin (userName, password) VALUES ('"+s1+"','"+s2+"')";
ResultSet insertion = null;
//System.out.println(s1 + s2); Vérification avec Eclipse
try (
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
)
{
int affected = stmt.executeUpdate();
if (affected == 1) {
insertion = stmt.getGeneratedKeys();
insertion.next();
JOptionPane.showMessageDialog(null, "Nouvelles données insérées !");
tf1.setText("");
tf2.setText("");
}
} catch (Exception e2) {
System.err.println(e2);
JOptionPane.showMessageDialog(null, e2.getMessage(), "Aucun enregistrement effectué !", JOptionPane.ERROR_MESSAGE);
}
}
});
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
}
});
//f.getContentPane().add(tp);
//tp.addTab("Add Record",p1);
f.add(p1);
f.setSize(350,180);
f.setVisible(true);
f.setResizable(false);
ConnectionManager.getInstance().close();
}
} |