Problème avec l'actualisation du JTextArea

Résolu
Greenlio - Modifié par Greenlio le 4/03/2015 à 09:14
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 4 mars 2015 à 14:19
Bonjour,

J'essaye de faire une petite console, mais j'ai un problème avec l'actualisation du JTextArea mais pourtant j'ai tout essayer (setText, revalidate...) mais toujours le même problème.

Vous pourez m'aidez si vous plaît

Je vous montre le code :

package modules;

import modules.Command;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Console extends JFrame{
 
 private JPanel container = new JPanel();
 private JButton entrer = new JButton("Entrer");
 private JTextField jtf = new JTextField("");
 
 protected JTextArea ecran_info = new JTextArea();
 
 public void fenetre() {
     this.setTitle("Console");
     this.setSize(400, 400);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setLocationRelativeTo(null);
     
     container.setBackground(Color.white);
     container.setLayout(new BorderLayout());
     
     ecran_info.setEditable(false);
     ecran_info.setText("Bienvenue dans la console");
     
     
     JPanel sud = new JPanel();
     sud.setLayout(new BoxLayout(sud, BoxLayout.LINE_AXIS));
     
     jtf.setForeground(Color.BLACK);
     jtf.setPreferredSize( new Dimension(300, 30 ) );
     
     sud.add(jtf);
     
     entrer.addActionListener(new Bouton());
     
     sud.add(entrer);
     
     container.add(ecran_info, BorderLayout.CENTER);
     container.add(sud, BorderLayout.SOUTH);

     this.setContentPane(container);
     this.setVisible(true);  
 }
 
 public void ajouter_texte(String message) {
  System.out.println("La variable 'message' : "+message);
  ecran_info.append(message);
 }
 
 class Bouton implements ActionListener {
 
  public void actionPerformed(ActionEvent arg0){
   new Command(jtf.getText());
  }
 }

}


Et le deuxieme :

package modules;

import modules.Console;

public class Command {
 
 Console console = new Console();
 
 private String command[] = {
   "message"
 };
 
 public Command(String text) {
  
  if(text.equals(command[0])) {
   System.out.println("Teste de la commande 'message'");
   console.ajouter_texte(text);
  }
  else
  {
   System.out.println("Cette command n'existe pas");
  }
 }
 
 
}


Merci d'avance

3 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
4 mars 2015 à 11:30
Salut,

Quand tu clics sur ton bouton "ajouter" tu refais une nouvelle instance de ta classe Command qui elle même refait une nouvelle instance de ton objet Console pour lui répondre du coups il est impossible que tu puisses afficher le message dans la zone de texte qui est à l'écran...

Voici une autre version (il en existe plein d'autres) qui utilise un listener pour notifier à l'objet Command qu'une nouvelle commande est arrivée:

Console.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Console extends JFrame implements ActionListener {
  private final JPanel container = new JPanel();
  private final JButton entrer = new JButton("Entrer");
  private final JTextField jtf = new JTextField("");
  private final JTextArea ecran_info = new JTextArea();
  private final List<CommandListener> listeners = new ArrayList<CommandListener>();

  public Console() {
    this.setTitle("Console");
    this.setSize(400, 400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    container.setBackground(Color.white);
    container.setLayout(new BorderLayout());

    ecran_info.setEditable(false);
    ecran_info.setText("Bienvenue dans la console\n");

    final JPanel sud = new JPanel();
    sud.setLayout(new BoxLayout(sud, BoxLayout.LINE_AXIS));

    jtf.setForeground(Color.BLACK);
    jtf.setPreferredSize(new Dimension(300, 30));

    sud.add(jtf);

    entrer.addActionListener(this);

    sud.add(entrer);

    container.add(ecran_info, BorderLayout.CENTER);
    container.add(sud, BorderLayout.SOUTH);

    this.setContentPane(container);
    this.setVisible(true);
  }

  public void addCommandListener(final CommandListener listener) {
    if (!listeners.contains(listener))
      listeners.add(listener);
  }

  private void fireCommand(final String cmd) {
    for (final CommandListener li : listeners)
      ecran_info.append(li.sendCommand(cmd) + "\n");
  }

  @Override
  public void actionPerformed(final ActionEvent arg0) {
    fireCommand(jtf.getText() + "\n");
  }

}


CommandListener.java
public interface CommandListener {
  
  public String sendCommand(final String cmd);
  
}


Command.java
public class Command implements CommandListener {

  private final String commands[] = { "message" };

  public Command() {
  }

  @Override
  public String sendCommand(final String cmd) {
    final String copy = cmd.trim();
    if (allowCommand(copy)) {
      System.out.println("Teste de la commande 'message'");
      return copy;
    } else {
      return copy + ": Cette command n'existe pas";
    }
  }

  private boolean allowCommand(final String cmd) {
    for(String c : commands)
      if(cmd.startsWith(c)) return true;
    return false;
  }
}


Main.java
public class Main {

  public static void main(final String... args) {
    Console c = new Console();
    c.addCommandListener(new Command());
    c.setVisible(true);
  }
}

1
Greenlio Messages postés 4 Date d'inscription samedi 6 décembre 2014 Statut Membre Dernière intervention 4 mars 2015
Modifié par Greenlio le 4/03/2015 à 12:42
Merci de m'avoir répondu,

J'ai pas trop compris pour dans le fichier Console, la fonction fireCommand et addCommantListener.
Et aussi, le fichier Command je comprend pas trop.

La variable "listener" sert a lire les commandes prédéfini ?
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
4 mars 2015 à 14:19
Re,

la fonction fireCommand permet d'envoyer les modifications aux différents listeners qui veulent interpréter les commandes tapées par l'utilisateur.

la fonction addCommandListener permet de rattacher un nouveau listener pour recevoir les nouvelles commandes.

Le fichier Command s'enregistre pour écouter/interpréter les commandes tapées par l'utilisateur.

Je te conseils grandement une petite lecture de ce sujet: http://rom.developpez.com/java-listeners/
0
Rejoignez-nous