Skip to content
Snippets Groups Projects
Commit 8ca3ad5b authored by NeggutsMc's avatar NeggutsMc
Browse files

panel java

parent 8fb02142
No related branches found
No related tags found
No related merge requests found
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/panel_admin_sae.iml" filepath="$PROJECT_DIR$/panel_admin_sae.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
File added
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/mysql-connector-java-8.0.30.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
package src;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
public class Panel implements ActionListener {
private Connection dbConnection;
private JFrame frame;
private JTabbedPane tabbedPane;
private JPanel competitionsPanel;
private JTextField lieuField, horaireField, placesField;
private JButton addButton, deleteButton, updateButton, loginButton;
private JTable competitionsTable;
private DefaultTableModel competitionsTableModel;
private JTextField loginField;
private JPasswordField passwordField;
private boolean isLoggedIn = false;
public Panel(Connection connection) {
this.dbConnection = connection;
frame = new JFrame("Panel");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
tabbedPane = new JTabbedPane();
// Add the login panel
JPanel loginPanel = createLoginPanel();
tabbedPane.addTab("Login", loginPanel);
// Add the competitions panel but set it as disabled initially
competitionsPanel = createCompetitionsPanel();
tabbedPane.addTab("Gérer Compétitions", competitionsPanel);
tabbedPane.setEnabledAt(1, false);
frame.add(tabbedPane);
frame.setVisible(true);
}
private JPanel createLoginPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel titleLabel = new JLabel("Connexion");
titleLabel.setFont(new Font("Arial", Font.BOLD, 36));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(titleLabel, gbc);
JLabel usernameLabel = new JLabel("Nom d'utilisateur:");
usernameLabel.setFont(new Font("Arial", Font.PLAIN, 20));
gbc.gridy = 1;
gbc.gridwidth = 1;
panel.add(usernameLabel, gbc);
loginField = new JTextField(25);
loginField.setFont(new Font("Arial", Font.PLAIN, 20));
gbc.gridx = 1;
panel.add(loginField, gbc);
JLabel passwordLabel = new JLabel("Mot de passe:");
passwordLabel.setFont(new Font("Arial", Font.PLAIN, 20));
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(passwordLabel, gbc);
passwordField = new JPasswordField(25);
passwordField.setFont(new Font("Arial", Font.PLAIN, 20));
gbc.gridx = 1;
panel.add(passwordField, gbc);
loginButton = new JButton("Connexion");
loginButton.setFont(new Font("Arial", Font.BOLD, 20));
loginButton.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
panel.add(loginButton, gbc);
passwordField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginButton.doClick();
}
});
return panel;
}
private JPanel createCompetitionsPanel() {
JPanel panel = new JPanel(new BorderLayout());
competitionsTableModel = new DefaultTableModel();
competitionsTableModel.addColumn("ID");
competitionsTableModel.addColumn("Lieu");
competitionsTableModel.addColumn("Horaire");
competitionsTableModel.addColumn("Places Disponibles");
competitionsTable = new JTable(competitionsTableModel);
TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(competitionsTableModel);
competitionsTable.setRowSorter(sorter);
sorter.setComparator(0, Comparator.<Object, Integer>comparing(o -> (Integer) o));
sorter.setComparator(3, Comparator.<Object, Integer>comparing(o -> (Integer) o));
JScrollPane scrollPane = new JScrollPane(competitionsTable);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new GridLayout(4, 2, 10, 10)); // Ajout de l'espacement entre les composants
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Ajout de marges
inputPanel.add(new JLabel("Lieu:"));
lieuField = new JTextField();
inputPanel.add(lieuField);
inputPanel.add(new JLabel("Horaire (yyyy-MM-dd HH:mm:ss):"));
horaireField = new JTextField();
inputPanel.add(horaireField);
inputPanel.add(new JLabel("Places Disponibles:"));
placesField = new JTextField();
inputPanel.add(placesField);
addButton = new JButton("Ajouter");
deleteButton = new JButton("Supprimer");
updateButton = new JButton("Modifier");
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 10, 0)); // Ajout de l'espacement entre les boutons
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
inputPanel.add(buttonPanel);
panel.add(inputPanel, BorderLayout.SOUTH);
if (isLoggedIn) {
loadCompetitionsData();
}
return panel;
}
private void loadCompetitionsData() {
competitionsTableModel.setRowCount(0);
try {
Statement stmt = dbConnection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM competitions");
while (rs.next()) {
int competitionID = rs.getInt("CompetitionID");
String lieu = rs.getString("Lieu");
Timestamp horaire = rs.getTimestamp("Horaire");
int placesDisponibles = rs.getInt("PlacesDisponibles");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(horaire);
competitionsTableModel.addRow(new Object[]{competitionID, lieu, formattedDate, placesDisponibles});
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
authenticateUser();
} else if (e.getSource() == addButton && isLoggedIn) {
addCompetition();
} else if (e.getSource() == deleteButton && isLoggedIn) {
deleteCompetition();
} else if (e.getSource() == updateButton && isLoggedIn) {
updateCompetition();
}
}
private void authenticateUser() {
String enteredLogin = loginField.getText();
char[] enteredPasswordChars = passwordField.getPassword();
String enteredPassword = new String(enteredPasswordChars);
try {
PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM admin WHERE login = ? AND password = ?");
stmt.setString(1, enteredLogin);
stmt.setString(2, enteredPassword);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
isLoggedIn = true;
tabbedPane.setEnabledAt(1, true);
tabbedPane.setSelectedComponent(competitionsPanel);
loadCompetitionsData();
tabbedPane.remove(0);
} else {
JOptionPane.showMessageDialog(frame, "Invalid login credentials.", "Authentication Failed", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void addCompetition() {
try {
String lieu = lieuField.getText();
String horaireStr = horaireField.getText();
String placesStr = placesField.getText();
if (!placesStr.isEmpty()) {
try {
int placesDisponibles = Integer.parseInt(placesStr);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = sdf.parse(horaireStr);
Timestamp horaire = new Timestamp(parsedDate.getTime());
PreparedStatement stmt = dbConnection.prepareStatement("INSERT INTO competitions (Lieu, Horaire, PlacesDisponibles) VALUES (?, ?, ?)");
stmt.setString(1, lieu);
stmt.setTimestamp(2, horaire);
stmt.setInt(3, placesDisponibles);
int rowsAffected = stmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(frame, "Compétition ajoutée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE);
loadCompetitionsData();
} else {
JOptionPane.showMessageDialog(frame, "Erreur lors de l'ajout de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Veuillez entrer un nombre valide pour les places disponibles.", "Erreur", JOptionPane.ERROR_MESSAGE);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(frame, "Format de date invalide.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(frame, "Veuillez entrer un nombre pour les places disponibles.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void deleteCompetition() {
int selectedRow = competitionsTable.getSelectedRow();
if (selectedRow != -1) {
int competitionID = (int) competitionsTableModel.getValueAt(selectedRow, 0);
try {
PreparedStatement stmt = dbConnection.prepareStatement("DELETE FROM competitions WHERE CompetitionID = ?");
stmt.setInt(1, competitionID);
int rowsAffected = stmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(frame, "Compétition supprimée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE);
loadCompetitionsData();
} else {
JOptionPane.showMessageDialog(frame, "Erreur lors de la suppression de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(frame, "Veuillez sélectionner une compétition à supprimer.", "Sélection requise", JOptionPane.WARNING_MESSAGE);
}
}
private void updateCompetition() {
int selectedRow = competitionsTable.getSelectedRow();
if (selectedRow != -1) {
try {
int competitionID = (int) competitionsTableModel.getValueAt(selectedRow, 0);
String lieu = lieuField.getText();
String horaireStr = horaireField.getText();
String placesStr = placesField.getText();
if (!horaireStr.isEmpty()) { // Ajout de la vérification de non-nullité et non-vacuité
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = sdf.parse(horaireStr);
Timestamp horaire = new Timestamp(parsedDate.getTime());
int placesDisponibles = Integer.parseInt(placesStr);
PreparedStatement stmt = dbConnection.prepareStatement("UPDATE competitions SET Lieu = ?, Horaire = ?, PlacesDisponibles = ? WHERE CompetitionID = ?");
stmt.setString(1, lieu);
stmt.setTimestamp(2, horaire);
stmt.setInt(3, placesDisponibles);
stmt.setInt(4, competitionID);
int rowsAffected = stmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(frame, "Compétition mise à jour avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE);
loadCompetitionsData();
} else {
JOptionPane.showMessageDialog(frame, "Erreur lors de la mise à jour de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(frame, "Veuillez entrer une date valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException | ParseException | NumberFormatException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "Erreur lors de la mise à jour de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(frame, "Veuillez sélectionner une compétition à mettre à jour.", "Sélection requise", JOptionPane.WARNING_MESSAGE);
}
}
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
new Panel(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment