简体   繁体   English

如何在 Java 中的 TextArea 中刷新文本

[英]How to refresh text in a TextArea in Java

I have a really simple chat in Java and I have a doubt.我在 Java 中有一个非常简单的聊天,我有一个疑问。 If you check the GUI 1 , you will see that I have a button to refresh messages.如果您检查 GUI 1 ,您会看到我有一个按钮来刷新消息。 However, I would like that everytime a message is send from any user, every of them refresh their messages to check for new ones.但是,我希望每次从任何用户发送消息时,他们每个人都会刷新他们的消息以检查新消息。 How can I do it without the button?没有按钮怎么办?

Here is the code of the GUI:这是图形用户界面的代码:

    private JPanel Panel;
    private JTextField Mensaje;
    private JButton Enviar_Grupo;
    private JButton Enviar_Usuario;
    private JTextPane Mensajes_Directos;
    private JTextPane Mensajes_Grupo;
    private JLabel usuarios;
    private JLabel grupo;
    private JButton Recargar;
    private JLabel Usuario;
    private JTextField Usuario_Enviar;

    Pantalla(String titulo, Colega colega, ArrayList<Colega> colegitas){
        super(titulo);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane((Panel));
        this.pack();

        Usuario.setText("Chat de " + colega.getID());

        Enviar_Grupo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String mensaje = Mensaje.getText();
                colega.comunicar_grupal(mensaje);
            }
        });

        Enviar_Usuario.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String mensaje = Mensaje.getText();
                String usuario_envio = Usuario_Enviar.getText();
                usuario_envio = Usuario_Enviar.getText();
                Boolean no_enviado=true;

                for(int i=0;i<colegitas.size();i++){
                    if(colega.getID() != colegitas.get(i).getID()) {
                        if (usuario_envio.equals(colegitas.get(i).getID())) {
                            if (((colegitas.get(i).getRol() == 1) && (colega.rol == 1)) || (colegitas.get(i).getRol() == 0)){
                                colega.comunicar_individual(mensaje, colegitas.get(i));
                                Usuario_Enviar.setText("Mensaje Enviado...");
                                no_enviado = false;
                            }
                        }
                    }
                }
                if(no_enviado==true){
                    Usuario_Enviar.setText("Mensaje no enviado (Admin o no encontrado).");
                }
            }
        });
        Recargar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String mensajes_indi=new String();
                String mensajes_grupales=new String();

                for(int j = 0; j<colega.get_mensajes_individuales().size(); j++) {
                    mensajes_indi = mensajes_indi + colega.get_mensajes_individuales().get(j) + "\n";
                }
                for(int j = 0; j<colega.get_mensajes_grupales().size(); j++) {
                    mensajes_grupales=mensajes_grupales+colega.get_mensajes_grupales().get(j)+"\n";
                }
                Mensajes_Directos.setText(mensajes_indi);
                Mensajes_Grupo.setText(mensajes_grupales);
            }
        });
    }
}

1 1 在此处输入图像描述

I guess that you have somewhere in the server side a list of message?我猜你在服务器端的某个地方有一个消息列表? If yes then you could use a listener connect to that list in client side and then refresh the tchat on it.如果是,那么您可以使用侦听器连接到客户端的该列表,然后刷新其上的 tchat。

To add a listener to your list you can do it as there要将侦听器添加到您的列表中,您可以在那里进行

https://stackoverflow.com/a/9685844/14631359 https://stackoverflow.com/a/9685844/14631359

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM