How to display incoming variables in a Jframe?

Asked

Viewed 3,201 times

2

I’m working with server socket, it receives information from an Android app, are three information: table, order and quantity. However, I can not show this information in a JFrame, I have no idea how to do this, I tried some forms, but all gave thread error. If anyone can help me I thank you already!

public class Calculadora {

    private static int mesa;
    private static String prato;
    private static String qtd;          

    public Calculadora(DataInputStream in) throws IOException {
        //Faz a leitura dos números
        mesa = in.readInt();
        prato = in.readUTF();
        qtd = in.readUTF();
    }

    public int somar() {
        return 0;
    }

    public void enviar(DataOutputStream out) throws IOException {
        System.out.println("Mesa: " + mesa + "\nPedido: " + prato + "\nQuantidade: " + qtd);

        out.writeInt(mesa);
        out.writeChars(prato);
        out.writeChars(qtd);
    }

}

This is the class responsible for reading and sending the result, but my goal is not to send a result but to show what was received.

  • Do you want to automatically display? For example, if your server socket receives an order, it is already displayed on the screen (the JFrame) that you quoted?

  • Yeah, that’s the idea!

  • You can inform in the question also how is your screen so far, what have you tried to do? Why on the question there is nothing about your attempts at building the screen.

  • My screen this as a test, it simply this with 3 label to receive these values, is a very simple screen even, because I’m trying to make it work, so I can add in a CRUD that I have.

2 answers

1

There are many ways you can do this, apparently your problem is OO, organization of your application.

Basically you need the data from mesa, prato and qtd are available on the screen object to be updated, so you can organize your code in infinite ways to meet this.

I’ll show you a basic example of this working, but you should organize your application in the way that is best for you.

For your simple screen I will consider that you have something like this, only with Abels, as you said in one of the comments on your question:

JFrame com Labels

Generated by a code similar to this:

public class JFrameSimplesPedido extends JFrame {

    private static final long serialVersionUID = -2339970233456764439L;

    private final String lblMesaInicial = "Mesa: ";
    private final JLabel lblMesa;

    private final String lblPratoInicial = "Prato: ";
    private final JLabel lblPrato;

    private final String lblQuantidadeInicial = "Quantidade: ";
    private final JLabel lblQuantidade;

    public JFrameSimplesPedido(final String title) {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        final JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(1, 0, 0, 0));

        lblMesa = new JLabel(lblMesaInicial);
        contentPane.add(lblMesa);

        lblPrato = new JLabel(lblPratoInicial);
        contentPane.add(lblPrato);

        lblQuantidade = new JLabel(lblQuantidadeInicial);
        contentPane.add(lblQuantidade);
    }

}

To organize as little as possible, I encapsulated the attributes in an object SimplesPedido:

public class SimplesPedido {

    private int mesa;
    private String prato;
    private String quantidade;

    // getters e setter

}

Let’s now see a way to update the Abels. In class JFrameSimplesPedido I will provide a method to update them, so:

public void updateLabels(final SimplesPedido pedido) {
    lblMesa.setText(lblMesaInicial + pedido.getMesa());
    lblPrato.setText(lblPratoInicial + pedido.getPrato());
    lblQuantidade.setText(lblQuantidadeInicial + pedido.getQuantidade());
}

To generate new requests I will use a Thread, let’s call it ThreadSimplesPedido, that will call the method updateLabels of the instance of JFrameSimplesPedido that he knows, to update the Abels, something like this:

public class ThreadSimplesPedido implements Runnable {

    private final JFrameSimplesPedido frame;

    public ThreadSimplesPedido(final JFrameSimplesPedido frame) {
        this.frame = frame;
    }

    @Override
    public void run() {
        while (true) {
            // 'dorme' por 5 segundos e depois gera outro pedido
            Thread.sleep(5000);

            frame.updateLabels(gerarPedido());
        }
    }

    private final Random random = new Random();

    private SimplesPedido gerarPedido() {
        final SimplesPedido pedido = new SimplesPedido();
        pedido.setMesa(random.nextInt(100));
        pedido.setPrato("" + random.nextInt(100));
        pedido.setQuantidade("" + random.nextInt(100));
        return pedido;
    }

}

Note that I simply update the Abels by calling the method updateLabels each time a new request "arrives".

In class JFrameSimplesPedido I will remove the main and create a new class to start our application, something like that:

public class SimplesApplication {

    public static void main(final String[] args) {
        final Runnable runner = () -> {
            final JFrameSimplesPedido frame = new JFrameSimplesPedido("Update Simples de Pedidos");
            frame.setVisible(true);

            final ThreadSimplesPedido tPedido = new ThreadSimplesPedido(frame);
            final Thread t = new Thread(tPedido);
            t.setDaemon(true);
            t.start();
        };
        EventQueue.invokeLater(runner);
    }

}

As we rotate SimplesApplication, this is what we will observe:

Atualiza Pedidos - JLabels

As I said at the beginning, there are several ways to do this, one of which you can use is with Observer, a pattern of projects that can help you, search how to use it. With it you can notify observers, in case your screen.

This image below is another way of doing and I used the Observer and a screen a little more elaborate:

Atualiza Pedidos - JTable e Observer

Stay and be an inspiration =)

By finishing and reinforcing, you can design your application in several ways, this answer is a north functional of what you can do.

  • Ola Bruno, sorry it takes to give the answer, I did the project that this in your answer, I put the class that receives the values inside the Threadsimplespedido and inside the generating methodPedido I put the variables , however it does not show on screen, shows null, as if there was nothing inside the variables, I made a printnl to see if there really was nothing, but printed the values received.

  • @Brunoleoneles dude, if you did exactly like the example, there’s no way to printar null. The images were made with this example and in them are the values, no null. Now, if you’re null, is because it is another instance and not the one created in gerarPedido. I suggest reviewing your project, unfortunately there is more to be done than what is in the answer, since it is a functional example.

0

Could create methods Static in the class that extends Jframe? You would update the information by these methods and could access it from outside the class.

  • I tried this way, but the values did not pass, always received null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.