mardi 31 mars 2015

Proper way of receiving and updating data from server in JPanel


Vote count:

0




I have my java serverthread which retrieves data from a database and sends them to my client. The main client GUI is represented by a JTabbedPane and I want every tab (which is a JPanel) to receive it's own data and show them (for example home panel, purchasespanel, profilepanel etc). What i did is to pass the ObjectInput/Output stream to every panel and let him receive data and update them itself by calling the getAndSet method on the panel object from the main class(you can see below). Is that the correct way of doing that?


Here's an example of one of my panels



public class PurchasesPanel extends JPanel {

private JTable table;
private JScrollPane scrollPane;
private ObjectInputStream input;
private ObjectOutputStream output;
private static final String PANEL_ID = "purchases";


public PurchasesPanel(ObjectInputStream input, ObjectOutputStream output) {

this.output = output;
this.input = input;
createGUI();

}


public void createGUI() {

//Impostazione del layout e creazione della tabella
setLayout(new BorderLayout());
table = new JTable();
scrollPane = new JScrollPane(table);
add(scrollPane);

}

public void getAndSetTableModel() {

try {

output.writeUTF(PANEL_ID);
output.flush();

DefaultTableModel model = (DefaultTableModel) input.readObject();

table.setModel(model);

} catch (Exception e) {

JOptionPane.showMessageDialog(this, "Si è verificato un errore con il pannello riepilogo acquisti. ");

}


}


}



asked 1 min ago







Proper way of receiving and updating data from server in JPanel

Aucun commentaire:

Enregistrer un commentaire