01/10/2018, 16:27
Add đối tượng vào araylist trong java
Mình đang làm một ứng dụng login đơn giản. Người dùng điền thông tin xong nhấn submit thì một đối tượng sẽ được tạo và add vào arraylist. Mình bị bí ở chỗ người dùng submit xong thì tiếp tục tạo một account khác và khi submit thì chỉ có thông tin của đối tượng sau được add vào arraylist. Mong mọi người hướng dẫn mình.
Bài liên quan
Mình đọc chưa hiểu lắm @@ Nhưng mà mình nghĩ cách làm là:
Mỗi khi nhấn nút “Submit” thì tạo ra một đối tượng mới, set các thông tin cho đối tượng đó rồi add và Array. Sau đó thì clear tất cả các text ở trên form để người dùng nhập lại.
Nếu chưa đúng ý bạn thì hãy nói rõ ra hơn nữa nhé
xóa cái cũ đi chèn cái mới vào
Thì không phải mỗi lần nhấn là tạo ra một đối tượng mới à ??
Tất nhiên, khi làm vậy chỉ có cái sau cùng, khi submit thì sẽ tạo 1 new request mới, ArrayList lúc này sẽ là 1 instant khác lần submit trước, cái trước không đươc lưu lại đâu.
Muốn lưu lại cái trước thì tìm hiểu về hhttpsession trong java (cách làm thực tế)
Còn muốn lưu giữ bình thường thì dùng biến static
đúng rồi bạn. Sau khi clear rồi nhập lại và add xong show ra thì nó chỉ hiển thị thông tin vừa nhập và thông tin trước đó bị mất
// Đây là code của mình
package Exercise;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/**
*
@author trung.nguyen
*/
public class Ex7Form extends javax.swing.JFrame {
/**
*/
public Ex7Form() {
initComponents();
}
/**
This method is called from within the constructor to initialize the form.
WARNING: Do NOT modify this code. The content of this method is always
regenerated by the Form Editor.
*/
@SuppressWarnings(“unchecked”)
//
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
nameLabel = new javax.swing.JLabel();
nameTextField = new javax.swing.JTextField();
userLabel = new javax.swing.JLabel();
userTextField = new javax.swing.JTextField();
passwordLabel = new javax.swing.JLabel();
passwordField = new javax.swing.JPasswordField();
telLabel = new javax.swing.JLabel();
telTextField = new javax.swing.JTextField();
genderLabel = new javax.swing.JLabel();
languageLabel = new javax.swing.JLabel();
maleRadioButton = new javax.swing.JRadioButton();
femaleRadioButton = new javax.swing.JRadioButton();
englishCheckBox = new javax.swing.JCheckBox();
frenchCheckBox = new javax.swing.JCheckBox();
otherCheckBox = new javax.swing.JCheckBox();
submitButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
showButton = new javax.swing.JButton();
nameWarningLabel = new javax.swing.JLabel();
userWarningLabel = new javax.swing.JLabel();
passWarningLabel = new javax.swing.JLabel();
telWarningLabel = new javax.swing.JLabel();
genderWarningLabel = new javax.swing.JLabel();
languageWarningLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
nameLabel.setText(“Name :”);
userLabel.setText(“User Name :”);
passwordLabel.setText(“Password :”);
passwordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordFieldActionPerformed(evt);
}
});
telLabel.setText(“Tel Number :”);
genderLabel.setText(“Gender :”);
languageLabel.setText(“Language :”);
buttonGroup1.add(maleRadioButton);
maleRadioButton.setText(“Male”);
maleRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
maleRadioButtonActionPerformed(evt);
}
});
buttonGroup1.add(femaleRadioButton);
femaleRadioButton.setText(“Female”);
englishCheckBox.setText(“English”);
englishCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
englishCheckBoxActionPerformed(evt);
}
});
frenchCheckBox.setText(“French”);
otherCheckBox.setText(“Other”);
otherCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
otherCheckBoxActionPerformed(evt);
}
});
submitButton.setText(“Submit”);
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
resetButton.setText(“Reset”);
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});
showButton.setText(“Show”);
showButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(userLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(passwordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(telLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
.addComponent(userTextField)
.addComponent(passwordField)
.addComponent(telTextField)))
.addGroup(layout.createSequentialGroup()
.addComponent(genderLabel)
.addGap(18, 18, 18)
.addComponent(maleRadioButton)
.addGap(18, 18, 18)
.addComponent(femaleRadioButton))
.addGroup(layout.createSequentialGroup()
.addComponent(languageLabel)
.addGap(18, 18, 18)
.addComponent(englishCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(frenchCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(otherCheckBox)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(languageWarningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(genderWarningLabel)
.addComponent(nameWarningLabel)
.addComponent(userWarningLabel)
.addComponent(passWarningLabel)
.addComponent(telWarningLabel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(submitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(resetButton, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(showButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(78, 78, 78))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nameLabel)
.addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nameWarningLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userLabel)
.addComponent(userTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(userWarningLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(passWarningLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(telLabel)
.addComponent(telTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(telWarningLabel))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(genderLabel)
.addComponent(maleRadioButton)
.addComponent(femaleRadioButton)
.addComponent(genderWarningLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(languageLabel)
.addComponent(englishCheckBox)
.addComponent(frenchCheckBox)
.addComponent(otherCheckBox)
.addComponent(languageWarningLabel))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(submitButton)
.addComponent(resetButton)
.addComponent(showButton))
.addContainerGap(68, Short.MAX_VALUE))
);
pack();
}//
private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void maleRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void otherCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void englishCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void showButtonActionPerformed(java.awt.event.ActionEvent evt) {
JFrame tableFrame = new JFrame(“User List”);
tableFrame.setSize(300,400);
tableFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
tableFrame.setVisible(true);
JTextArea listTextArea = new JTextArea(300,400);
tableFrame.add(listTextArea);
for(int i = 0 ; i < listUser.size() ; i++)
listTextArea.setText(listUser.get(i).getFullName());
}
private static ArrayList listUser = new ArrayList();
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {
nameTextField.setText("");
userTextField.setText("");
telTextField.setText("");
passwordField.setText("");
buttonGroup1.clearSelection();
englishCheckBox.setSelected(false);
frenchCheckBox.setSelected(false);
otherCheckBox.setSelected(false);
}
private boolean checkGender(){
return maleRadioButton.isSelected();
}
private int[] checkLanguage(){
int[] i = new int[3];
if(englishCheckBox.isSelected())
i[0] = 1;
if(englishCheckBox.isSelected())
i[1] = 1;
if(englishCheckBox.isSelected())
i[2] = 1;
return i;
}
private int checkTel(){
long l = 0;
try{
l = Long.parseLong(telTextField.getText());
}catch(java.lang.NumberFormatException ex){
return 1;
}
if(telTextField.getText().length()!=10)
return 2;
return 0;
}
private int check(){
if(nameTextField.getText().isEmpty())
return -1;
if(userTextField.getText().isEmpty())
return -1;
if(telTextField.getText().isEmpty())
return -1;
if(passwordField.getPassword().length == 0)
return -1;
if(!maleRadioButton.isSelected() && !femaleRadioButton.isSelected())
return -1;
if(!englishCheckBox.isSelected() && !frenchCheckBox.isSelected() && !otherCheckBox.isSelected())
return -1;
return 0;
}
/**
@param args the command line arguments
/
public static void main(String args[]) {
/ Set the Nimbus look and feel /
//
/ If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Ex7Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Ex7Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Ex7Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Ex7Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ex7Form().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JCheckBox englishCheckBox;
private javax.swing.JRadioButton femaleRadioButton;
private javax.swing.JCheckBox frenchCheckBox;
private javax.swing.JLabel genderLabel;
private javax.swing.JLabel genderWarningLabel;
private javax.swing.JLabel languageLabel;
private javax.swing.JLabel languageWarningLabel;
private javax.swing.JRadioButton maleRadioButton;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField nameTextField;
private javax.swing.JLabel nameWarningLabel;
private javax.swing.JCheckBox otherCheckBox;
private javax.swing.JLabel passWarningLabel;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
private javax.swing.JButton resetButton;
private javax.swing.JButton showButton;
private javax.swing.JButton submitButton;
private javax.swing.JLabel telLabel;
private javax.swing.JTextField telTextField;
private javax.swing.JLabel telWarningLabel;
private javax.swing.JLabel userLabel;
private javax.swing.JTextField userTextField;
private javax.swing.JLabel userWarningLabel;
// End of variables declaration
}
đây là code của mình. Mong bạn chỉ giúp với chứ còn lơ mơ quá
private static ArrayList listUser = new ArrayList();
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
Không phải là nó không thêm cái cũ vào, mà là cách Show nó ra của bạn bị sai !
Bạn chỉ show ra từng cái một thôi, nên bị hiểu lầm.
Thế làm thế nào để khắc phục đây bạn
Tạo một String để gắn các text vào, sau đó mới setText()