public AddNewStudent() // constructor { // initializing buttons btnok = new JButton("OK"); btnok.addActionListener(this); btnexit = new JButton("Exit"); btnexit.addActionListener(this); btnaddnew = new JButton("AddNew"); btnaddnew.addActionListener(this); // initializing textfields tf1 = new JTextField(12); tf2 = new JTextField(12); // initializing labels lblname = new JLabel("Name:"); lbladd = new JLabel("Address:"); lblmsg = new JLabel("", JLabel.CENTER); // initializing panels p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); psouth = new JPanel(); // adding buttons and label to panel p1 // setting flowlayout p1.setLayout(new FlowLayout()); p1.add(btnok); p1.add(btnexit); p1.add(btnaddnew); // adding lblmsg to panel p3 p3.add(lblmsg); // adding both the panels to new panel,psouth // settin layout 2:1 psouth.setLayout(new GridLayout(2, 1)); psouth.add(p3); psouth.add(p1); // adding label and textfields to panel p2 p2.setLayout(new GridLayout(3, 1)); // setting line and titled border for panel p2 p2.setBorder(BorderFactory.createLineBorder(Color.red)); p2.setBorder(BorderFactory.createTitledBorder("Enter Your Details")); p2.add(lblname); p2.add(tf1); p2.add(lbladd); p2.add(tf2); // adding panel to container this.getContentPane().add(p2, "Center"); this.getContentPane().add(psouth, "South"); this.setSize(300, 300); this.setLocation(100, 200); this.show(); }
public void actionPerformed(ActionEvent e) { p2.str1 = fn1.getText(); p2.str2 = ln1.getText(); p2.str3 = un1.getText(); p2.str4 = ps11.getText(); p2.str5 = eid1.getText(); p2.str6 = ps21.getText(); p2.str7 = mb1.getText(); p2.str8 = cct1.getText(); p2.str9 = hquali1.getText(); p2.str10 = dober1.getText(); p2.str11 = dober2.getText(); p2.str12 = dober3.getText(); p2.str13 = p2.str10 + "/" + p2.str11 + "/" + p2.str12; boolean bl1, bl2, bl3, bl4, bl5, bl6, bl7, bl8, bl9; bl1 = false; bl2 = false; bl3 = false; bl4 = false; bl5 = false; bl6 = false; bl7 = false; bl8 = false; if (str1.length() > 0) { // er1.setForeground(null); er1.setText(""); fn1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl1 = true; repaint(); } else { er1.setForeground(Color.red); er1.setText("(First name can't be null)"); fn1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str3.length() > 0) { er3.setText(""); un1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl2 = true; repaint(); } else { er3.setForeground(Color.red); er3.setText("(Username can't be null)"); un1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str5.length() > 0) { er6.setText(""); eid1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl3 = true; repaint(); } else { er6.setForeground(Color.red); er6.setText("(email id can't be null)"); eid1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str4.length() > 0) { er4.setText(""); ps11.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl4 = true; repaint(); } else { er4.setForeground(Color.red); er4.setText("(Password can't be null)"); ps11.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if ((str4.equals(str6))) { str6 = str4; er5.setForeground(null); er5.setText(""); ps21.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl5 = true; repaint(); } else { er5.setForeground(Color.red); er5.setText("(both pwd should be same)"); ps21.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str7.length() == 10) { er7.setText(""); mb1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl6 = true; repaint(); } else { er7.setForeground(Color.red); er7.setText("(number should be of 10 digit)"); mb1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str8.length() > 0) { er8.setText(""); cct1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl7 = true; repaint(); } else { er8.setForeground(Color.red); er8.setText("(Current City can't be null)"); cct1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } if (str9.length() > 0) { er9.setText(""); hquali1.setBorder(BorderFactory.createLineBorder(Color.GRAY)); bl8 = true; repaint(); } else { er9.setForeground(Color.red); er9.setText("(Highest Qualif. can't be null)"); hquali1.setBorder(BorderFactory.createLineBorder(Color.red)); repaint(); } try { if ((bl1 && bl2 && bl3 && bl4 && bl5 && bl6 && bl7 && bl8) == true) { JOptionPane.showMessageDialog(null, "Data submitting "); p2.goer(); } } catch (Exception e3) { } }