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 :

[swing] Comment regénérer un JPanel ?


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 38
    Par défaut [swing] Comment regénérer un JPanel ?
    Bonjour,

    J'ai un JPanel qui suivant une certaine valeur contient des composants différents.
    Lorsque j'appuie sur un bouton, je voudrai que ça passe de l'un à l'autre.
    Malheureusement, je n'y arrive pas.
    Voici un petit code qui simule ce que je veux faire.
    Le but est d'arriver à remplir la méthode refreshPanel.

    Merci d'avance

    Alain


    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
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
     
    public class TestFrame extends JFrame {
     
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JButton button1;
        private JButton button2;
     
        private JPanel mainPanel;
     
        private boolean switcher = true;
     
        public TestFrame(){
            super();
     
            initGUI();
            getContentPane().add(mainPanel);
        }
     
        private void initGUI(){
            System.out.println("### initGUI()");
            mainPanel = initMainPanel();
        }
     
        private JPanel initMainPanel() {
            System.out.println("### initMainPanel()");
     
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
     
            if (switcher){
                label1 = new JLabel("LABEL 1");
                label1.setForeground(Color.ORANGE);
                panel.add(label1);
                label2 = new JLabel("LABEL 2");
                panel.add(label2);
                button1 = new JButton("BUTTON 1");
                panel.add(button1);
     
                button1.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent arg0) {
                        refreshPanel();
                    }
                });
     
                switcher=false;
            }else{
                label3 = new JLabel("LABEL 3");
                label3.setForeground(Color.BLUE);
                panel.add(label3);
                button2 = new JButton("BUTTON 2");
                panel.add(button2);
     
                button2.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent arg0) {
                        refreshPanel();
                    }
                });
     
                switcher=true;
            }
            return panel;
        }
     
        private void refreshPanel(){
            // ????
        }
     
        public static void main(String[] args) {
            TestFrame frame = new TestFrame();
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.setBounds(200,200,250,150);
            frame.setVisible(true);
        }
    }

  2. #2
    Membre chevronné Avatar de spekal
    Inscrit en
    Mai 2005
    Messages
    502
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 502
    Par défaut
    Moi quand j'ai à faire ce genre de truc j'utilise le CardLayout.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 38
    Par défaut
    C'est que j'ai simplifié le problème.
    En fait, j'ai plusieurs panels dans ma JFrame et je veux en rafraichir qu'un !

  4. #4
    Membre confirmé
    Avatar de jolatouf
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    170
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations forums :
    Inscription : Novembre 2004
    Messages : 170
    Par défaut
    bonjour,

    Pourquoi ne pas faire quelque chose du genre

    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
     
    private void refreshPanel(){
         if(switcher){
             label1.setVisibe(false);
             label2.setVisible(false)
             button1.setVisible(false);
             label3.setVisibe(true);
             label4.setVisible(true)
             button2.setVisible(true);
     
         }else{
             label3.setVisibe(false);
             label4.setVisible(false)
             button2.setVisible(false);
             label1.setVisibe(true);
             label2.setVisible(true)
             button1.setVisible(true);
    }
     
     
    }
    et en ayant créé au préalable tous tes labels et bouttons mais en visible false pour ceux qui ne sont pas utilisé

    Ps:

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 38
    Par défaut
    Merci pour vos réponses.

    Jolatouf, je pense que ta solution doit marcher, mais dans mon écran cela n'est pas trop possible.

    J'ai fait le code ci-dessous qui colle mieux à mes besoins.
    En fait, je supprime tous les composants et je les recréé.

    Qu'en pensez-vous?

    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
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
     
    public class TestFrame extends JFrame {
     
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JButton button1;
    	private JButton button2;
     
    	private JPanel mainPanel;
     
    	private boolean switcher = true; 
     
    	public TestFrame(){
    		super();
     
    		initGUI();
    		getContentPane().add(mainPanel);
    	}
     
    	private void initGUI(){
    		System.out.println("### initGUI()");
    		mainPanel = new JPanel(); 
    		initMainPanel();
    	}
     
    	private JPanel initMainPanel() {
    		System.out.println("### initMainPanel()");
     
    		mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
     
    		if (switcher){
    			label1 = new JLabel("LABEL 1");
    			label1.setForeground(Color.ORANGE);
    			mainPanel.add(label1);
    			label2 = new JLabel("LABEL 2");
    			mainPanel.add(label2);
    			button1 = new JButton("BUTTON 1");
    			mainPanel.add(button1);
     
    			button1.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent arg0) {
    					refreshPanel();
    				}
    			});
     
    			switcher=false;
    		}else{
    			label3 = new JLabel("LABEL 3");
    			label3.setForeground(Color.BLUE);
    			mainPanel.add(label3);
    			button2 = new JButton("BUTTON 2");
    			mainPanel.add(button2);
     
    			button2.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent arg0) {
    					refreshPanel();
    				}
    			});
     
    			switcher=true;
    		}
    		return mainPanel;
    	}
     
    	private void refreshPanel(){
    		mainPanel.removeAll();
    		initMainPanel();
    		mainPanel.repaint();
    		mainPanel.revalidate();
    	}
     
    	public static void main(String[] args) {
    		TestFrame frame = new TestFrame();
    		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    		frame.setBounds(200,200,250,150);
    		frame.setVisible(true);
    	}
    }

  6. #6
    Membre chevronné Avatar de bassim
    Homme Profil pro
    Ingénieur Réseaux
    Inscrit en
    Février 2005
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur Réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2005
    Messages : 666
    Par défaut
    pourquoi ne pas faire comme ça :
    tu as 2 panels : panel 1 et panel 2 .

    tu fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    panelPrincipal.add(panel1);
     
    // lors du changement
    panelPrincipal.remove(panel1);
    panelPrincipal.add(panel2);
    panelPrincipal.repaint();
    panel1 et panel 2 contiennent à l'avance tes composants

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Swing] Comment colorer un JSpinner ?
    Par calogerogigante dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 02/04/2006, 11h52
  2. PB : Comment regénérer mon journal des transactions ?
    Par SPIKE84 dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 13/02/2006, 09h38
  3. [Swing] comment savoir si une fenetre est ouverte ?
    Par uraxyd dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 31/12/2005, 11h55
  4. [SWING] Comment créer un thème ?
    Par sqwam71 dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 27/12/2005, 12h34
  5. [debutant][swing] Probleme avec un Jpanel
    Par JeanMoul dans le forum Débuter
    Réponses: 6
    Dernier message: 27/08/2005, 19h07

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