[Swing] Comment regénérer un JPanel

Résolu
coinki Messages postés 57 Date d'inscription mercredi 26 février 2003 Statut Membre Dernière intervention 6 juillet 2009 - 7 déc. 2006 à 15:11
coinki Messages postés 57 Date d'inscription mercredi 26 février 2003 Statut Membre Dernière intervention 6 juillet 2009 - 7 déc. 2006 à 16:58
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

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 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
7 déc. 2006 à 16:17
Salut,

Utiliser un cardLayout tu te prendras moins la tête ....

sinon essai plutot :

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;

public class TestFrame extends JFrame {
  
    private JLabel label1 = null;
    private JLabel label2 = null;
    private JLabel label3 = null;
    private JButton button1 = null;
    private JButton button2 = null;
  
    private JPanel mainPanel = null;
   private boolean switcher = true;
  
    public TestFrame(){
        super();
      
        switchMainPanel();
        setContentPane(mainPanel);
    }
    
    private void switchMainPanel() {
        System.out.println("### initMainPanel()");
      
        if (mainPanel == null)//pas besoin de recreer l'instance
            mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
        if (switcher){
            if (label1 == null){//pas besoin de recreer l'instance
                label1 = new JLabel("LABEL 1");
                //idem pas besoin de refaire
                label1.setForeground(Color.ORANGE);
            }
            if (label2 == null)//pas besoin de recreer l'instance
                label2 = new JLabel("LABEL 2");
            if (button1 == null){//pas besoin de recreer l'instance
                 button1 = new JButton("BUTTON 1");
                 //idem pas besoin de refaire
                 button1.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        refreshPanel();
                    }
                });
            }
            mainPanel.add(label1);
            mainPanel.add(label2);
            mainPanel.add(button1);
          
           
           
        }else{
            if (label3 == null){//pas besoin de recreer l'instance
                label3 = new JLabel("LABEL 3");
                //idem pas besoin de refaire
                label3.setForeground(Color.BLUE);
            }
            if (button2 == null){//pas besoin de recreer l'instance
                button2 = new JButton("BUTTON 2");
                //idem pas besoin de refaire
                 button2.addActionListener(new ActionListener(){
                     public void actionPerformed(ActionEvent e) {
                         refreshPanel();
                     }
                 });
            }
           
            mainPanel.add(label3);
            mainPanel.add(button2);
          
          
        }
        switcher = !switcher;//tu switch une fois
    }

    private void refreshPanel(){
        mainPanel.removeAll();//on clear le panel
        switchMainPanel();
        mainPanel.revalidate();
        mainPanel.repaint();
    }

    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//si tu n'as pas d'autres fenetre fais plutot EXIT_ON_CLOSE
        //frame.setBounds(200,200,250,150);//pourquoi nepas centrer ta fenetre à l'ecran
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);//ici tu centre à l'ecran en fonction de la taille
        frame.setVisible(true);
    }
}

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
3
coinki Messages postés 57 Date d'inscription mercredi 26 février 2003 Statut Membre Dernière intervention 6 juillet 2009
7 déc. 2006 à 16:58
Merci beaucoup pour ta réponse.

A force de jouer avec les méthodes du panel j'étais arrivé à la même solution que toi
0
Rejoignez-nous