IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Problème de switch de JPanel


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mars 2002
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Mars 2002
    Messages : 244
    Par défaut Problème de switch de JPanel
    Bonjour,

    Je pense que mon problème est dû au fait que j'utilise l'architecture MVC.
    Je vais laisser le code entier plus bas.

    J'ai une appli(JFrame) avec 4 JPanels
    Voici comment ils se composent:
    *- PanelGauche est un panel qui est tjs visible avec un bouton
    *- PanelPrincipal est un panel qui est tjs visible qui continet 2 autres panel:
    ** - PanelSous1
    ** - PanelSous2

    J'utilise un CardLayout pour le PanelPrincipal

    Quand je clique sur le bouton "test", je voudrais pouvoir afficher soit le PanelSous1, soit le PanelSous2.
    Voici un bout du code que j'utilise (ici juste des remove pour le test):

    mise en place du PanelPrincipal:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    private JPanel getPanelPrincipal() {
    		if (PanelPrincipal == null) {
    			PanelPrincipal = new JPanel();
    			PanelPrincipal.setLayout(new CardLayout());
     
    			PanelPrincipal.setBounds(new Rectangle(138, 16, 315, 250));
    			PanelPrincipal.setBackground(new Color(238, 0, 0));
    			PanelPrincipal.add(getPanelSous1(), getPanelSous1().getName());
    			//PanelPrincipal.remove(getPanelss());
    			PanelPrincipal.add(getPanelSous2(), getPanelSous2().getName());
    		}
    		return PanelPrincipal;
    	}
    et le code du bouton
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    private JButton getBtnTest() {
    		if (btnTest == null) {
    			btnTest = new JButton();
    			btnTest.setBounds(new Rectangle(19, 38, 59, 26));
    			btnTest.setText("Test");
    			btnTest.setActionCommand("test");
    			btnTest.addActionListener(controleur);
    			btnTest.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
    					PanelPrincipal.remove(getPanelss());
    					PanelPrincipal.remove(getPanelss2());
    				}
    			});
     
    		}
    		return btnTest;
    	}
    Ceci fonctionne quand je clique sur le bouton mes sous panels disparaissent

    Maintenant que j'utilise une architecture MVC, si je remets le code du bouton dans une méthode [de ma classe JFrame ]:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public void changerPanel(){
    		//this.PanelPrincipal.remove(getPanelss());
    		//PanelPrincipal.validate();
    		//PanelPrincipal.getLayout();
    		PanelPrincipal.remove(getPanelSous1());
    		PanelPrincipal.remove(getPanelSous2());
     
    	}
    et que via le controleur j'appelle cette méthode:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("test")){
    			this.vue.changerPanel();
    		}
     
    	}
    J'obtiens une erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at dbbon.controleur.DBBonControleur.actionPerformed(DBBonControleur.java:23)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:5488)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    	at java.awt.Component.processEvent(Component.java:5253)
    	at java.awt.Container.processEvent(Container.java:1966)
    	at java.awt.Component.dispatchEventImpl(Component.java:3955)
    	at java.awt.Container.dispatchEventImpl(Container.java:2024)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    	at java.awt.Container.dispatchEventImpl(Container.java:2010)
    	at java.awt.Window.dispatchEventImpl(Window.java:1778)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Je suppose que je n'arrive pas à capter le bon container ou quelque chose comme ça dans mon controlleur.

    Merci de votre aide.

    Voici le code mon appli:

    Mon Main qui utilise le modèle
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package dbbon.tests;
     
    import dbbon.gui.frame.EcranPrincipal;
    import dbbon.produit_catalogue.PCA_SQL;
     
    public class AppliTest {
     
     
    	public static void main(String[] args) {
    		PCA_SQL pca_sql = new PCA_SQL();
    		EcranPrincipal ecranPrincipal = new EcranPrincipal(pca_sql);
    		ecranPrincipal.setVisible(true);
    	}
    }
    Ma GUI
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    package dbbon.gui.frame;
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    import java.util.EventListener;
     
    import dbbon.controleur.DBBonControleur;
    import dbbon.gui.panels.PanelTest;
    import dbbon.produit_catalogue.PCA_SQL;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import java.awt.Rectangle;
    import javax.swing.JMenuItem;
     
     
     
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.BorderFactory;
    import javax.swing.border.TitledBorder;
    import java.awt.Font;
    import java.awt.Color;
    import java.util.ArrayList;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Point;
    import javax.swing.SwingConstants;
    import javax.swing.JButton;
    import java.awt.SystemColor;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.CardLayout;
     
    public class EcranPrincipal extends JFrame {
    	private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();  //  @jve:decl-index=0:
    	private static final long serialVersionUID = 1L;
    	private DBBonControleur controleur;
     
    	private JPanel jContentPane = null;
    	private JPanel PanelGauche = null;
    	private JButton btnTest = null;
    	private JPanel PanelPrincipal = null;
    	private JPanel PanelSous1 = null;
    	private JPanel PanelSous2 = null;
    	/**
             * This is the default constructor
             */
    	public EcranPrincipal(PCA_SQL modele) {
    		super();
    		controleur= new DBBonControleur(this,modele);
    		initialize();
    		this.setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2);
    	}
     
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		this.setSize(493, 320);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
     
    	/**
             * This method initializes jContentPane
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.setBackground(new Color(238, 255, 238));
    			jContentPane.add(getPanelGauche(), null);
    			jContentPane.add(getPanelPrincipal(), null);
    		}
    		return jContentPane;
    	}
     
     
     
    	/**
             * This method initializes PanelGauche
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelGauche() {
    		if (PanelGauche == null) {
    			PanelGauche = new JPanel();
    			PanelGauche.setLayout(null);
    			PanelGauche.setBounds(new Rectangle(11, 15, 82, 257));
    			PanelGauche.setBackground(new Color(0, 238, 238));
    			PanelGauche.add(getBtnTest(), null);
    		}
    		return PanelGauche;
    	}
     
    	/**
             * This method initializes btnTest
             *
             * @return javax.swing.JButton
             */
    	private JButton getBtnTest() {
    		if (btnTest == null) {
    			btnTest = new JButton();
    			btnTest.setBounds(new Rectangle(19, 38, 59, 26));
    			btnTest.setText("Test");
    			btnTest.setActionCommand("test");
    			btnTest.addActionListener(controleur);
    		/*	btnTest.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
    					//PanelPrincipal.remove(getPanelss());
    					//PanelPrincipal.remove(getPanelss2());
    				}
    			});
    		*/
    		}
    		return btnTest;
    	}
     
    	/**
             * This method initializes PanelPrincipal
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelPrincipal() {
    		if (PanelPrincipal == null) {
    			PanelPrincipal = new JPanel();
    			PanelPrincipal.setLayout(new CardLayout());
     
    			PanelPrincipal.setBounds(new Rectangle(138, 16, 315, 250));
    			PanelPrincipal.setBackground(new Color(238, 0, 0));
    			PanelPrincipal.add(getPanelSous1(), getPanelSous1().getName());
    			//PanelPrincipal.remove(getPanelss());
    			PanelPrincipal.add(getPanelSous2(), getPanelSous2().getName());
    		}
    		return PanelPrincipal;
    	}
     
    	public void changerPanel(){
    		//this.PanelPrincipal.remove(getPanelss());
    		//PanelPrincipal.validate();
    		//PanelPrincipal.getLayout();
    		PanelPrincipal.remove(getPanelSous1());
    		PanelPrincipal.remove(getPanelSous2());
     
    	}
     
    	/**
             * This method initializes PanelSous1
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelSous1() {
    		if (PanelSous1 == null) {
    			PanelSous1 = new JPanel();
    			PanelSous1.setLayout(null);
    			PanelSous1.setName("jPanel");
    		}
    		return PanelSous1;
    	}
     
    	/**
             * This method initializes PanelSous2
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelSous2() {
    		if (PanelSous2 == null) {
    			PanelSous2 = new JPanel();
    			PanelSous2.setLayout(null);
    			PanelSous2.setName("Panelss2");
    			PanelSous2.setBackground(new Color(149, 153, 153));
    		}
    		return PanelSous2;
    	}
    }  //  @jve:decl-index=0:visual-constraint="10,10"
    et mon controleur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package dbbon.controleur;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import dbbon.gui.frame.EcranPrincipal;
    import dbbon.gui.panels.PanelTest;
    import dbbon.produit_catalogue.PCA_SQL;
     
    public class DBBonControleur implements ActionListener{
    	private EcranPrincipal vue;
    	private PCA_SQL modele;
    	/**
             * Crée un controleur pour la fenêtre principale
             * @param vue   La fenêtre principale.
             * @param modele        Le modèle du MVC.
             */
    	public DBBonControleur(EcranPrincipal vue, PCA_SQL modele) {
    		// TODO Auto-generated constructor stub
    	}
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("test")){
    			this.vue.changerPanel();
    		}
     
    	}
     
    }
    Merci

    PS: Est-ce mieux de faire un remove ou une sorte de setVisible?

  2. #2
    Membre expérimenté
    Profil pro
    Dev NodeJS
    Inscrit en
    Août 2006
    Messages
    177
    Détails du profil
    Informations personnelles :
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev NodeJS

    Informations forums :
    Inscription : Août 2006
    Messages : 177
    Par défaut
    Tu n'a pas initialisé vue... Du coup il est nul! L'exception était pourtant claire à ce sujet

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    package dbbon.controleur;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import dbbon.gui.frame.EcranPrincipal;
    import dbbon.gui.panels.PanelTest;
    import dbbon.produit_catalogue.PCA_SQL;
     
    public class DBBonControleur implements ActionListener{
    	private EcranPrincipal vue;
    	private PCA_SQL modele;
    	/**
             * Crée un controleur pour la fenêtre principale
             * @param vue   La fenêtre principale.
             * @param modele        Le modèle du MVC.
             */
    	public DBBonControleur(EcranPrincipal vue, PCA_SQL modele) {
    		this.vue = vue;
    	}
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("test")){
    			this.vue.changerPanel();
    		}
     
    	}
     
    }

  3. #3
    Membre éclairé
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mars 2002
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Mars 2002
    Messages : 244
    Par défaut
    Merci, ça fonctionne.

    MAIS ...
    ceci était pour un test.
    J'ai essayé de le porter réllement à mon appli et ça foire encore.


    Mon appli est composée de 2 panneau principaux
    * Panel Gauche
    ** qui contient PanelMenubtn (un panel avec des boutons)
    *PanelPrincipal
    ** qui contient un PanelRed (un panel à fond rouge)

    Voyons pour le Panelgauche d'un peu plus près:
    J'ai crée son contenu (PanelMenuBtn - panel avec bouton) dans un autre package.
    C'est de ce bouton que part l'évènement.
    Je vois donc mon PanelMenuBtn qui est dans Panelgauche qui est dans ma Frame principale
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package dbbon.gui.panels.menugauche;
     
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import javax.swing.BorderFactory;
    import javax.swing.border.EtchedBorder;
     
    import dbbon.controleur.DBBonControleur;
    import dbbon.produit_catalogue.PCA_SQL;
     
    import java.awt.Color;
     
    public class PanelMenuBtn extends JPanel {
     
    	private DBBonControleur controleur;
     
    	private static final long serialVersionUID = 1L;
    	private JButton btnConfigGauche = null;
     
    	/**
             * This is the default constructor
             */
    	public PanelMenuBtn(PCA_SQL modele) {
    		super();
    		controleur= new DBBonControleur(this,modele);
    		initialize();
    	}
     
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		this.setSize(61, 60);
    		this.setLayout(null);
    		this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    		this.setBackground(new Color(7, 238, 238));
    		this.add(getBtnConfigGauche(), null);
    	}
     
    	/**
             * This method initializes btnConfigGauche
             *
             * @return javax.swing.JButton
             */
    	private JButton getBtnConfigGauche() {
    		if (btnConfigGauche == null) {
    			btnConfigGauche = new JButton();
    			btnConfigGauche.setBounds(new Rectangle(2, 2, 57, 54));
    			btnConfigGauche.setIcon(new ImageIcon(getClass().getResource("/pict/icon-config.gif")));
    			btnConfigGauche.setActionCommand("btnConfigGauche");
    			btnConfigGauche.addActionListener(controleur);
    		}
    		return btnConfigGauche;
    	}
     
    }  //  @jve:decl-index=0:visual-constraint="10,10"

    J'ai d'abord eu le même message et j'ai donc vérifier si j'ai bien instancié mes vues dans mon controleur.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public DBBonControleur(EcranPrincipal vue, PCA_SQL modele) {
    		this.vue=vue;
    		this.modele = modele;
    	}
     
    	public DBBonControleur(PanelMenuBtn vuePanelGauche, PCA_SQL modele) {
    		this.vuePanelGauche=vuePanelGauche;
    		this.modele = modele;
    	}
    ce qui est fait.

    Pour ne pas avoir le message d'erreur je réinitialise ma vue dans l'événement du controleur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("btnConfigGauche")){
    			//System.out.println(e);
    			this.vue=new EcranPrincipal(modele);
    			//this.vuePanelGauche=new PanelMenuBtn(modele);
    			this.vue.changerPanel();
    		}
     
    	}
    si je n'ajoute pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.vue=new EcranPrincipal(modele);
    alors j'ai le message d'erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at dbbon.controleur.DBBonControleur.actionPerformed(DBBonControleur.java:36)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:5488)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    	at java.awt.Component.processEvent(Component.java:5253)
    	at java.awt.Container.processEvent(Container.java:1966)
    	at java.awt.Component.dispatchEventImpl(Component.java:3955)
    	at java.awt.Container.dispatchEventImpl(Container.java:2024)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    	at java.awt.Container.dispatchEventImpl(Container.java:2010)
    	at java.awt.Window.dispatchEventImpl(Window.java:1778)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Ce que je ne comprend pas très bien acr si je vois ma vue (Frame Principale) elle à déjà été lancée non? Soit...

    Comme j'ai rajouté la ligne plus rien n'apparaît comme message mais surtout, l'action demandée n'est pas effectuée.

    action dans ma GUI principale
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    	public void changerPanel(){
    		this.PanelPrincipal.remove(getPanelRed());
    	}


    voici mon code

    mon main:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package dbbon.tests;
     
    import dbbon.gui.frame.EcranPrincipal;
    import dbbon.gui.panels.menugauche.PanelMenuBtn;
    import dbbon.produit_catalogue.PCA_SQL;
     
    public class AppliTest {
     
     
    	public static void main(String[] args) {
    		PCA_SQL pca_sql = new PCA_SQL();
    		EcranPrincipal ecranPrincipal = new EcranPrincipal(pca_sql);
    		//PanelMenuBtn panelMenuBtn=new PanelMenuBtn(pca_sql);
    		ecranPrincipal.setVisible(true);
    	}
    }
    mon PanelMenuBtn:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package dbbon.gui.panels.menugauche;
     
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import javax.swing.BorderFactory;
    import javax.swing.border.EtchedBorder;
     
    import dbbon.controleur.DBBonControleur;
    import dbbon.produit_catalogue.PCA_SQL;
     
    import java.awt.Color;
     
    public class PanelMenuBtn extends JPanel {
     
    	private DBBonControleur controleur;
     
    	private static final long serialVersionUID = 1L;
    	private JButton btnConfigGauche = null;
     
    	/**
             * This is the default constructor
             */
    	public PanelMenuBtn(PCA_SQL modele) {
    		super();
    		controleur= new DBBonControleur(this,modele);
    		initialize();
    	}
     
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		this.setSize(61, 60);
    		this.setLayout(null);
    		this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    		this.setBackground(new Color(7, 238, 238));
    		this.add(getBtnConfigGauche(), null);
    	}
     
    	/**
             * This method initializes btnConfigGauche
             *
             * @return javax.swing.JButton
             */
    	private JButton getBtnConfigGauche() {
    		if (btnConfigGauche == null) {
    			btnConfigGauche = new JButton();
    			btnConfigGauche.setBounds(new Rectangle(2, 2, 57, 54));
    			btnConfigGauche.setIcon(new ImageIcon(getClass().getResource("/pict/icon-config.gif")));
    			btnConfigGauche.setActionCommand("btnConfigGauche");
    			btnConfigGauche.addActionListener(controleur);
    		}
    		return btnConfigGauche;
    	}
     
    }  //  @jve:decl-index=0:visual-constraint="10,10"
    ma GUI:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    package dbbon.gui.frame;
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    import java.util.EventListener;
     
    import dbbon.controleur.DBBonControleur;
    import dbbon.gui.panels.PanelTest;
    import dbbon.gui.panels.menugauche.PanelMenuBtn;
    import dbbon.produit_catalogue.PCA_SQL;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import java.awt.Rectangle;
    import javax.swing.JMenuItem;
     
     
     
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.BorderFactory;
    import javax.swing.border.TitledBorder;
    import java.awt.Font;
    import java.awt.Color;
    import java.util.ArrayList;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Point;
    import javax.swing.SwingConstants;
    import javax.swing.JButton;
    import java.awt.SystemColor;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.CardLayout;
     
    public class EcranPrincipal extends JFrame {
    	private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();  //  @jve:decl-index=0:
    	private static final long serialVersionUID = 1L;
    	private DBBonControleur controleur;
    	private PCA_SQL modele;
     
    	private JPanel jContentPane = null;
    	private JPanel PanelGauche = null;
    	private PanelMenuBtn panelMenuBtn= null;
    	private JPanel PanelPrincipal = null;
    	private JPanel PanelRed = null;
    	/**
             * This is the default constructor
             */
    	public EcranPrincipal(PCA_SQL modele) {
    		super();
     
    		this.modele=modele;
    		controleur= new DBBonControleur(this,modele);
    		initialize();
    		this.setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2);
    	}
     
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		/////////////////////////////////////////
    		panelMenuBtn =new PanelMenuBtn(this.modele);
    		panelMenuBtn.setName("panelMenuBtn");
    		this.setSize(665, 508);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
     
    	/**
             * This method initializes jContentPane
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.setBackground(new Color(238, 255, 238));
    			jContentPane.add(getPanelGauche(), null);
    			jContentPane.add(getPanelPrincipal(), null);
    		}
    		return jContentPane;
    	}
     
     
     
    	public void changerPanel(){
    		//this.PanelPrincipal.remove(getPanelss());
    		//PanelPrincipal.validate();
    		//PanelPrincipal.getLayout();
    		//PanelPrincipal.remove(getPanelSous1());
    		//PanelPrincipal.remove(getPanelSous2());
    		this.PanelPrincipal.remove(getPanelRed());
    	}
     
    	/**
             * This method initializes PanelGauche
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelGauche() {
    		if (PanelGauche == null) {
    			PanelGauche = new JPanel();
    			PanelGauche.setLayout(new CardLayout());
    			PanelGauche.setBounds(new Rectangle(15, 18, 62, 427));
    			PanelGauche.add(panelMenuBtn, panelMenuBtn.getName());
    		}
    		return PanelGauche;
    	}
     
    	/**
             * This method initializes PanelPrincipal
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelPrincipal() {
    		if (PanelPrincipal == null) {
    			PanelPrincipal = new JPanel();
    			PanelPrincipal.setLayout(new CardLayout());
    			PanelPrincipal.setBounds(new Rectangle(141, 58, 112, 84));
    			PanelPrincipal.setBackground(new Color(238, 238, 4));
    			PanelPrincipal.add(getPanelRed(), getPanelRed().getName());
    		}
    		return PanelPrincipal;
    	}
     
    	/**
             * This method initializes PanelRed
             *
             * @return javax.swing.JPanel
             */
    	private JPanel getPanelRed() {
    		if (PanelRed == null) {
    			PanelRed = new JPanel();
    			PanelRed.setLayout(null);
    			PanelRed.setName("PanelRed");
    			PanelRed.setBackground(new Color(238, 0, 0));
    		}
    		return PanelRed;
    	}
    }  //  @jve:decl-index=0:visual-constraint="10,10"
    et mon controleur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package dbbon.controleur;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import dbbon.gui.frame.EcranPrincipal;
    import dbbon.gui.panels.PanelTest;
    import dbbon.gui.panels.menugauche.PanelMenuBtn;
    import dbbon.produit_catalogue.PCA_SQL;
     
    public class DBBonControleur implements ActionListener{
    	private EcranPrincipal vue;
    	private PanelMenuBtn vuePanelGauche;
    	private PCA_SQL modele;
     
    	/**
             * Crée un controleur pour la fenêtre principale
             * @param vue   La fenêtre principale.
             * @param modele        Le modèle du MVC.
             */
    	public DBBonControleur(EcranPrincipal vue, PCA_SQL modele) {
    		this.vue=vue;
    		this.modele = modele;
    	}
     
    	public DBBonControleur(PanelMenuBtn vuePanelGauche, PCA_SQL modele) {
    		this.vuePanelGauche=vuePanelGauche;
    		this.modele = modele;
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("btnConfigGauche")){
    			//System.out.println(e);
    			this.vue=new EcranPrincipal(modele);
    			//this.vuePanelGauche=new PanelMenuBtn(modele);
    			this.vue.changerPanel();
    		}
     
    	}
     
    }
    Merci et encore désolé pour le problème.
    Y sûrement un peti qqchose que je ne capte pas

Discussions similaires

  1. Problème avec GridBagLayout dans JPanel
    Par caneman dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 19/12/2006, 22h32
  2. Problème de switch-case ou de buffer ?
    Par Gilouou dans le forum C
    Réponses: 8
    Dernier message: 11/11/2006, 21h08
  3. Problème avec image dans JPanel
    Par littleshrimp dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 14/05/2006, 14h05
  4. [Cookies] problème de switch
    Par artotal dans le forum Langage
    Réponses: 1
    Dernier message: 28/12/2005, 02h35
  5. [Tableaux] Problème avec Switch case
    Par philippef dans le forum Langage
    Réponses: 4
    Dernier message: 07/09/2005, 16h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo