Initializing TextFields JTextField txtName new JTextField JTextField txtAge new

Initializing textfields jtextfield txtname new

This preview shows page 2 - 4 out of 4 pages.

//Initializing TextFields JTextField txtName = new JTextField(); JTextField txtAge = new JTextField(); JTextField txtPhoneNo = new JTextField(); JTextField txtAddress = new JTextField(); JButton btnAdd = new JButton("Add"); JButton btnView = new JButton("View"); p1.add(lName); p1.add(txtName); p1.add(lAge); p1.add(txtAge); p1.add(lPhoneNo); p1.add(txtPhoneNo); p1.add(lAddress); p1.add(txtAddress); p1.add(new JLabel()); p1.add(lConsole);
Image of page 2
JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.add(btnAdd); p2.add(btnView); DefaultTableModel model = new DefaultTableModel(); model.addColumn("Name"); model.addColumn("Age"); model.addColumn("Ph. No."); model.addColumn("Address"); JTable table = new JTable(model); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(p1); add(scrollPane); add(p2); setVisible(true); DatabaseCRUD dbCrud = new DatabaseCRUD(); btnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = txtName.getText(); if (name.isEmpty()) { lConsole.setText("Please enter name"); txtName.requestFocus(); return; } String age = txtAge.getText(); if (age.isEmpty()) { lConsole.setText("Please enter age"); txtAge.requestFocus(); return; } String phoneNo = txtPhoneNo.getText(); if (phoneNo.isEmpty()) { lConsole.setText("Please enter phone no"); txtPhoneNo.requestFocus(); return; } String address = txtAddress.getText(); if (address.isEmpty()) { lConsole.setText("Please enter address"); txtAddress.requestFocus(); return; } lConsole.setText("");
Image of page 3
User user = new User(); user.setName(name); user.setAge(Integer.parseInt(age)); user.setPhNo(phoneNo); user.setAddress(address); dbCrud.insertIntoDatabase(user); txtName.setText(""); txtAge.setText(""); txtPhoneNo.setText(""); txtAddress.setText(""); } }); btnView.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { List<User> users = dbCrud.getFromDatabase(); if (users != null && !users.isEmpty()) { for (User user : users) { model.insertRow(0, new Object[]{user.getName(),user.getAge(), user.getPhNo(), user.getAddress()}); } } } }); } }
Image of page 4

You've reached the end of your free preview.

Want to read all 4 pages?

  • Winter '17
  • Ujjwol Shakya

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors