How to display received variables in a JFrame?

2

I am working with socket server, it receives information from an Android application, there are three information: table, request and quantity. However, I can not display this information in JFrame , I have no idea how to do this, I tried some forms, but all gave a thread error. If anyone can help me I thank you right away!

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.

    
asked by anonymous 30.04.2015 / 17:05

2 answers

1

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

Basically, you need the data of mesa , prato and qtd to be available in the screen object to be updated, so you can organize your code in infinite ways to fit that.

I'll show you a basic example of this working, but you should organize your application in the way that's convenient for you.

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

Generatedbyacodesimilartothis:

publicclassJFrameSimplesPedidoextendsJFrame{privatestaticfinallongserialVersionUID=-2339970233456764439L;privatefinalStringlblMesaInicial="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've encapsulated the attributes in a SimplesPedido object:

public class SimplesPedido {

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

    // getters e setter

}

Let's now see a way to update the labels. In the JFrameSimplesPedido class I will make available a method to update them, like this:

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 , which will call the updateLabels method of the JFrameSimplesPedido instance it knows, to update the labels, something like this: / p>

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;
    }

}

Notice that I simply update the labels by calling the updateLabels method every time a new request "arrives."

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

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);
    }

}

When running SimplesApplication , this is what we will observe:

AsIsaidatthebeginning,thereareseveralwaystodothis,oneofwhichyoucanuseis Observer . , a pattern of projects that can help you, find a way to use it. With it you can notify observers, in case your screen.

This image below is another way of doing and I have used Observer quoted and a slightly more elaborate screen:

It is to serve as inspiration =)

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

    
02.05.2015 / 00:59
0

Could you create static methods in the class that extends JFrame? You would update the information by these methods and could access them outside the class.

    
30.04.2015 / 17:16