Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Swing - Changement de contenu d'un Layout

La gestion des évènements en Java Mise en oeuvre d'une barre de menu



Cet exemple de code vous montre comment changer le contenu d'un layout dans une application Swing. La gestion des événements est faites via une référence sur méthode (syntaxe ::) : un Java SE 8.0 minimum est donc requis pour faire tourner cet exemple.

Le point le plus important de cet exemple, consiste à demande de réactualiser le contenu de la fenêtre après le changement : les appels à revalidate (ligne 58) et à repaint (ligne 59) sont donc nécessaires. Ne les obliez pas.

 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 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class DemoSwing extends JFrame {

    private JComboBox<String> comboBox = new JComboBox<>( new String[] { "First", "Second", "Third" } );
    private JScrollPane tree = new JScrollPane( new JTree() );
    private JScrollPane textArea = new JScrollPane( new JTextArea("Content to edit") );
    private JButton button = new JButton( "Click Me" );
    private JPanel contentPane;
    
    private Component currentInjectedComponent = null;
    
    public DemoSwing() {
        super( "Demo changement contenu" );
        this.setSize( 400, 300 );
        this.setLocationRelativeTo( null );
        this.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        
        contentPane = (JPanel) getContentPane();
        contentPane.add( comboBox, BorderLayout.NORTH );
        
        // Utilisation de référence sur méthode pour la gestion des événements (requière un Java SE 8)
        comboBox.addItemListener( this::comboBoxItemStateChange );
        
        this.setVisible( true );
    }
    
    public void comboBoxItemStateChange( ItemEvent event ) {
        if ( event.getStateChange() == ItemEvent.SELECTED ) {
            if ( currentInjectedComponent != null ) {
                contentPane.remove( currentInjectedComponent );
            }
            switch( (String) event.getItem() ) {
                case "First":
                    currentInjectedComponent = tree;
                    contentPane.add( tree, BorderLayout.CENTER );
                    break;
                case "Second":
                    currentInjectedComponent = textArea;
                    contentPane.add( textArea, BorderLayout.CENTER );
                    break;
                default:
                    currentInjectedComponent = button;
                    contentPane.add( button, BorderLayout.CENTER );
            }
            contentPane.revalidate();
            currentInjectedComponent.repaint();
        }
    }
    
    
    public static void main( String[] args )  throws Exception {
        UIManager.setLookAndFeel( new NimbusLookAndFeel() );
        new DemoSwing();
    }
    
}
Exemple de changement de contenu d'un Layout
en ligne 65, et pour un meilleur visuel, j'ai opté pour un « look and feel » Nimbus.


La gestion des évènements en Java Mise en oeuvre d'une barre de menu