Custom Register Form using Java Swing


Custom Register Form using Java Swing




import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.HashMap;

import java.util.Hashtable;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JPopupMenu;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.table.DefaultTableModel;


public class UiCurd extends javax.swing.JFrame {

 public UiCurd() {

  initComponents();

  DefaultTableModel dt=(DefaultTableModel)jTable1.getModel();

  for(int i=0;i   dt.addRow(new String[]{String.valueOf(i),"A"+i,"B"+i});

  }

 }


 private void initComponents() {

  jScrollPane1 = new javax.swing.JScrollPane();

  jTable1 = new javax.swing.JTable();

  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

  jTable1.setModel(new javax.swing.table.DefaultTableModel(

    new Object[][] {

    }, new String[] { "Title 1", "Title 2", "Title 3" }));

  jTable1.addMouseListener(new java.awt.event.MouseAdapter() {

   public void mouseClicked(java.awt.event.MouseEvent evt) {

    jTable1MouseClicked(evt);

   }

  });

  jScrollPane1.setViewportView(jTable1);

  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(

    getContentPane());

  getContentPane().setLayout(layout);


  layout.setHorizontalGroup(layout.createParallelGroup(

    javax.swing.GroupLayout.Alignment.LEADING).addGroup(

    javax.swing.GroupLayout.Alignment.TRAILING,

    layout.createSequentialGroup().addContainerGap(13,

      Short.MAX_VALUE).addComponent(jScrollPane1,

      javax.swing.GroupLayout.PREFERRED_SIZE, 375,

      javax.swing.GroupLayout.PREFERRED_SIZE)

      .addContainerGap()));


  layout.setVerticalGroup(layout.createParallelGroup(

    javax.swing.GroupLayout.Alignment.LEADING).addGroup(

    javax.swing.GroupLayout.Alignment.TRAILING,

    layout.createSequentialGroup().addContainerGap(178,

      Short.MAX_VALUE).addComponent(jScrollPane1,

      javax.swing.GroupLayout.PREFERRED_SIZE, 122,

      javax.swing.GroupLayout.PREFERRED_SIZE)));

  pack();

 }//


 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {

  JPopupMenu p=new JPopupMenu();

  JMenuItem m1=new JMenuItem("Update");

  JMenuItem m2=new JMenuItem("Delete");

  p.add(m1);

  p.add(m2);

  p.show(jTable1, evt.getX(), evt.getY());

  m1.addActionListener(new ActionListener() {

   @Override

   public void actionPerformed(ActionEvent e) {

   HashMap map=login(UiCurd.this);

   System.out.println(map.get("user"));

   System.out.println(map.get("pass"));

   System.out.println(map.get("mobile"));

    }

  });

  

 m2.addActionListener(new ActionListener() {   

   @Override

   public void actionPerformed(ActionEvent e) {

   JOptionPane.showMessageDialog(UiCurd.this, "delete");

   }

  });

 }

 

 public HashMap login(JFrame frame) {

     HashMap login = new HashMap();

     JPanel panel= new JPanel(new BorderLayout(5, 5));

     JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));

     label.add(new JLabel("UserId", SwingConstants.RIGHT));

     label.add(new JLabel("Mobile", SwingConstants.RIGHT));

     label.add(new JLabel("Password", SwingConstants.RIGHT));

     panel.add(label, BorderLayout.WEST);

      JPanel fields = new JPanel(new GridLayout(0, 1, 2, 2));

     JTextField username = new JTextField();

    /* username.setText("Deep singh");

     username.setEditable(false);*/

     fields.add(username);

     JPasswordField password = new JPasswordField();

     JTextField mobile = new JTextField();

     fields.add(mobile);

     fields.add(password);

     panel.add(fields, BorderLayout.CENTER);

      JOptionPane.showMessageDialog(frame, panel, "login",-1);

      login.put("user", username.getText());

     login.put("pass", new String(password.getPassword()));

     login.put("mobile", mobile.getText());

     return login;


 }

 public static void main(String args[]) {

  java.awt.EventQueue.invokeLater(new Runnable() {

   public void run() {

    new UiCurd().setVisible(true);

   }

  });

 }


 private javax.swing.JScrollPane jScrollPane1;

 private javax.swing.JTable jTable1;

 // End of variables declaration//GEN-END:variables

}


No comments: