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:
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:
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:
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.
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?– Bruno César
Yeah, that’s the idea!
– Bruno leonel nunes
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.
– Bruno César
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.
– Bruno leonel nunes