Olá pessoal…
Meu problema é o seguinte:
tenho um JFrame que recebe os dados para cadastrar um filme, cujo código é:
package inventoryprogrampart5;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Valmir Junior
*/
public class FrameCadastrateMovie extends JFrame {
private static Movie movie;
private static JPanel lbPanel;
private static JPanel textFieldPanel;
private static JPanel btnPanel;
//create new JLabels.
private JLabel lbMovieNumber = new JLabel("Movie Number:");
private JLabel lbMovieName = new JLabel("Movie Name:");
private JLabel lbMovieTitle = new JLabel("Movie Title:");
private JLabel lbMovieQuantity = new JLabel("Movie Quantity:");
private JLabel lbMovieUnitPrice = new JLabel("Unit Price:");
private JLabel lbMovieMinimumStock = new JLabel("Minimum Stock:");
//create the text fields.
private JTextField jtfMovieNumber = new JTextField("", 30);
private JTextField jtfMovieName = new JTextField("", 30);
private JTextField jtfMovieTitle = new JTextField("", 30);
private JTextField jtfMovieQuantity = new JTextField("", 30);
private JTextField jtfMovieUnitPrice = new JTextField("", 30);
private JTextField jtfMovieMinimumStock = new JTextField("", 30);
//create the buttons.
private JButton btnOk = new JButton("Add Movie");
private JButton btnCancel = new JButton("Cancel");
public FrameCadastrateMovie() {
super();
this.toInitialState();
}
public Movie getMovie() {
return movie;
}
private void toInitialState() {
//----------------------Sets the frame attributes-----------------------
// set the size of the window - 400 is width, 300 is height
this.setSize(300, 300);
// set the title of the window
this.setTitle("Movie");
// center the form
this.setLocationRelativeTo(null);
//set the default close operation for this frame
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
//store the frame components
lbPanel = this.createLabelPanel();
textFieldPanel = this.createFieldPanel();
btnPanel = this.createButtonPanel();
//add the frame components
add(lbPanel, BorderLayout.WEST);
add(textFieldPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
private JPanel createLabelPanel() {
//create a new JPanel to add the labels.
JPanel labelPanel = new JPanel(new GridLayout(6, 1));
//add the labels.
labelPanel.add(lbMovieNumber);
labelPanel.add(lbMovieName);
labelPanel.add(lbMovieTitle);
labelPanel.add(lbMovieQuantity);
labelPanel.add(lbMovieUnitPrice);
labelPanel.add(lbMovieMinimumStock);
return labelPanel;
}
private JPanel createFieldPanel() {
//create a new JPanel to add the Text Fields.
JPanel txtFieldPanel = new JPanel(new GridLayout(6, 1));
//add the text fileds to the textFieldPanel.
txtFieldPanel.add(jtfMovieNumber);
txtFieldPanel.add(jtfMovieName);
txtFieldPanel.add(jtfMovieTitle);
txtFieldPanel.add(jtfMovieQuantity);
txtFieldPanel.add(jtfMovieUnitPrice);
txtFieldPanel.add(jtfMovieMinimumStock);
return txtFieldPanel;
}
private JPanel createButtonPanel() {
//create an action listener to the buttons.
class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add")) {
try {
movie = new Movie();
movie.setItemNumber(Integer.parseInt(jtfMovieNumber.getText()));
movie.setItemName(jtfMovieName.getText());
movie.setTitle(jtfMovieTitle.getText());
movie.setQuantity(Integer.parseInt(jtfMovieQuantity.getText()));
movie.setUnitPrice(Double.parseDouble(jtfMovieUnitPrice.getText()));
movie.setLowStock(Integer.parseInt(jtfMovieMinimumStock.getText()));
// clearFields();
JOptionPane.showMessageDialog(null, "Movie Added");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Incorrect Values");
}
} else if (e.getActionCommand().equals("Cancel")) {
dispose();
}
}
}
BtnListener btnListener = new BtnListener();
btnOk.setActionCommand("Add");
btnOk.addActionListener(btnListener);
btnCancel.setActionCommand("Cancel");
btnCancel.addActionListener(btnListener);
//create the panel to add the buttons.
JPanel buttonPanel = new JPanel();
//add the buttons to the buttonPanel.
buttonPanel.add(btnOk);
buttonPanel.add(btnCancel);
return buttonPanel;
}
public static void clearFields() {
//get the components in textFieldPanel
Component[] components = textFieldPanel.getComponents();
JTextField jtf = new JTextField();
//for each component in components
for (Component component : components) {
//if component is instance of JTextField
if (component.getClass().equals(jtf.getClass())) {
//clear text field text.
jtf = (JTextField) component;
jtf.setText("");
}
}
}
}
Este JFrame eu criei na mão mesmo…
o que eu quero saber é:
existe algum padrão de projeto para transporte de dados entre a camada de visualização?
pq eu tentei da seguinte forma:
criei um objeto estático Movie, dentro do próprio JFrame, que vai receber os dados do filme.
Mas o problema é que ao ser dado um “dispose()” no JFrame não tem como eu manter os dados do filme armazenado na memória para posterior uso em outro JFrame.
Não sei se sou burro demais ou o q… mas não consegui pensar numa saída para essa situação.
quem puder me dar alguma dica de como padronizar o transporte de dados para que todos os frames possam utilizá-los fico grato.
Obs.1: Os dados devem ficar armazenados em memória durante a execução do programa, estou descartando o uso de banco de dados pq tipo assim eu quero fazer com q o sistema gere telas com base em informações geradas dinamicamente.
Obs.2: Eu uso o padrão MVC. Então estou falando somente da camada View do sistema, nada de envolver a camada Model.