Add/Edit Staff

this.loginFrame = jframe;
loginFrame.setVisible(false);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
windowWidth = (int) (screenSize.getWidth() *90 /100);
windowHeight = (int) (windowWidth * 56 / 100);
frameControllerSize = (int) (windowHeight * 5.7 / 100);
addStaffFrame = new JFrame();
Utils.centeredFrame(addStaffFrame, windowWidth, windowHeight, "Add/Edit Staff");
addStaffFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//loginFrame.setResizable(false);
Container frameContainer = addStaffFrame.getContentPane();
frameContainer.setBackground(Color.gray);
// Changeable According to What size of screen You Want?
int containerWidth = windowWidth*94/100;
int containerHeight = windowHeight*90/100;
int panelWidth = containerWidth*98/100;
int panelHeight = containerHeight*96/100;
int posX = windowWidth / 2 - panelWidth / 2 ;
int posY = windowHeight / 2 - panelHeight / 2 - frameControllerSize / 2;
// frame without control options
panel = new JPanel();
panel.setLayout(null);//new BorderLayout());
//panel.setOpaque(false);
panel.setBackground(Color.decode("#FFFFFF"));
panel.setSize(new Dimension(panelWidth,panelHeight));
panel.setLocation(posX, posY);
frameContainer.add(panel);
// for Rounded Background
posX = windowWidth / 2 - containerWidth / 2 ;
posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
jcomponent.setLayout(null);
frameContainer.add(jcomponent, BorderLayout.CENTER);
Here is our class RoundedBackground:
public class RoundedBackground extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
int width, height,setX,setY;
public RoundedBackground (int setX,int setY,int width, int height) {
this.width=width;
this.height=height;
this.setX=setX;
this.setY=setY;
setSize(width, height);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.white);
//g2.setStroke(new BasicStroke(2.0f));
double x = setX;
double y = setY;
double w = width;
double h = height;
RoundRectangle2D round=new RoundRectangle2D.Double(x, y, w, h, 80, 80);
g2.fill(round);
}
}
After creation of window, Here we create Labels and Text Fields.
// for heading Layout
posY = panelHeight * 2/100;
Font headingFont = new Font("Serif", Font.PLAIN, 22);
// header
Border borderline = BorderFactory.createLineBorder(Color.black);
JLabel heading_text = new JLabel("Add/Edit Staff");
heading_text.setHorizontalAlignment(JLabel.CENTER);
heading_text.setVerticalAlignment(JLabel.CENTER);
heading_text.setBackground(Color.decode("#80c2b2"));
heading_text.setForeground(Color.black);
heading_text.setFont(headingFont);
heading_text.setBounds(0,0, panelWidth, panelHeight / 15);
panel.add(heading_text);
panel.add(Utils.getSeparator(0, panelHeight / 13, panelWidth, 5,"000000"));
// Staff table Base panel
// Action table Base panel
actionPanelWidth = panelWidth*35/100;
actionPanelHeight = panelHeight*86/100;
actionPanel = new JPanel();
actionPanel.setBorder(Utils.getTitledBorder(10, "Action"));
//controlPanel.add(new MyCanvas());
actionPanel.setBounds(0,panelHeight*10/100, actionPanelWidth, actionPanelHeight);
actionPanel.setLayout(null);
actionPanel.setName("Action");
panel.add(actionPanel);
Font textFont = new Font("Serif",0,12);
JLabel staffNameL = new JLabel("Staff Name");
staffNameL.setAlignmentX(staffNameL.LEFT_ALIGNMENT);
staffNameL.setAlignmentY(staffNameL.LEFT_ALIGNMENT);
staffNameL.setForeground(Color.black);
staffNameL.setFont(textFont);
staffNameL.setBounds(actionPanelWidth*10/100,actionPanelHeight*10/100, 300, 20);
staffNameL.setBackground(Color.black);
actionPanel.add(staffNameL);
staffNameField = new JTextField();
staffNameField.setForeground(Color.black);
staffNameField.setBounds(actionPanelWidth*10/100,actionPanelHeight*15/100, 300, 30);
staffNameField.setOpaque(false);
staffNameField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
staffNameField.setBackground(null);
actionPanel.add(staffNameField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*10/100,actionPanelHeight*20/100,actionPanelWidth*80/100,5,"000000"));
JLabel contactL = new JLabel("Contact No");
contactL.setAlignmentX(contactL.LEFT_ALIGNMENT);
contactL.setAlignmentY(contactL.LEFT_ALIGNMENT);
contactL.setForeground(Color.black);
contactL.setFont(textFont);
contactL.setBounds(actionPanelWidth*10/100,actionPanelHeight*25/100, 300, 20);
contactL.setBackground(Color.black);
actionPanel.add(contactL);
contactField = new JTextField();
contactField.setForeground(Color.black);
contactField.setBounds(actionPanelWidth*10/100,actionPanelHeight*30/100, 300, 30);
contactField.setOpaque(false);
contactField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
contactField.setBackground(null);
actionPanel.add(contactField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*10/100,actionPanelHeight*35/100,actionPanelWidth*80/100,5,"000000"));
JLabel emailL = new JLabel("Email Id");
emailL.setAlignmentX(emailL.LEFT_ALIGNMENT);
emailL.setAlignmentY(emailL.LEFT_ALIGNMENT);
emailL.setForeground(Color.black);
emailL.setFont(textFont);
emailL.setBounds(actionPanelWidth*10/100,actionPanelHeight*40/100, 300, 20);
emailL.setBackground(Color.black);
actionPanel.add(emailL);
emailField = new JTextField();
emailField.setForeground(Color.black);
emailField.setBounds(actionPanelWidth*10/100,actionPanelHeight*45/100, 300, 30);
emailField.setOpaque(false);
emailField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
emailField.setBackground(null);
actionPanel.add(emailField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*10/100,actionPanelHeight*50/100,actionPanelWidth*80/100,5,"000000"));
JLabel passwordL = new JLabel("Password");
passwordL.setAlignmentX(passwordL.LEFT_ALIGNMENT);
passwordL.setAlignmentY(passwordL.LEFT_ALIGNMENT);
passwordL.setForeground(Color.black);
passwordL.setFont(textFont);
passwordL.setBounds(actionPanelWidth*10/100,actionPanelHeight*55/100, 300, 20);
passwordL.setBackground(Color.black);
actionPanel.add(passwordL);
passwordField = new JTextField();
passwordField.setForeground(Color.black);
passwordField.setBounds(actionPanelWidth*10/100,actionPanelHeight*60/100, 300, 30);
passwordField.setOpaque(false);
passwordField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
passwordField.setBackground(null);
actionPanel.add(passwordField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*10/100,actionPanelHeight*65/100,actionPanelWidth*80/100,5,"000000"));
JLabel addsL = new JLabel("Address");
addsL.setAlignmentX(addsL.LEFT_ALIGNMENT);
addsL.setAlignmentY(addsL.LEFT_ALIGNMENT);
addsL.setForeground(Color.black);
addsL.setFont(textFont);
addsL.setBounds(actionPanelWidth*10/100,actionPanelHeight*70/100, 300, 20);
addsL.setBackground(Color.black);
actionPanel.add(addsL);
addsField = new JTextField();
addsField.setForeground(Color.black);
addsField.setBounds(actionPanelWidth*10/100,actionPanelHeight*75/100, 300, 30);
addsField.setOpaque(false);
addsField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
addsField.setBackground(null);
actionPanel.add(addsField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*10/100,actionPanelHeight*80/100,actionPanelWidth*80/100,5,"000000"));
Here we Define Utils class separator method.
public static JSeparator getSeparator(int x, int y, int width, int height,String color) {
JSeparator jSeparator = new JSeparator();
jSeparator.setBackground(Color.decode("#"+color));
jSeparator.setBorder(null);
jSeparator.setOpaque(false);
jSeparator.setAlignmentX(JSeparator.CENTER_ALIGNMENT);
jSeparator.setBounds(x, y, width, height);
return jSeparator;
}
Here we Define "ADD STAFF" Button and its click functionality.
addStaffBtn = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(250, 40, java.awt.Image.SCALE_SMOOTH)));
addStaffBtn.setContentAreaFilled(false);
addStaffBtn.setBorder(null);
addStaffBtn.setText("ADD STAFF");
addStaffBtn.setHorizontalTextPosition(JButton.CENTER);
addStaffBtn.setVerticalTextPosition(JButton.CENTER);
addStaffBtn.setBounds(actionPanelWidth/2-125,actionPanelHeight*85/100, 250, 40);
addStaffBtn.setOpaque(false);
addStaffBtn.setContentAreaFilled(false);
addStaffBtn.setBorderPainted(false);
addStaffBtn.setFont(new java.awt.Font("Serif",1, 15));
addStaffBtn.setForeground(Color.white);
actionPanel.add(addStaffBtn);
addStaffBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String var = "";
boolean is_name_entered = false;
boolean is_contact_entered = false;
boolean is_email_entered = false;
boolean is_password_entered = false;
boolean is_adds_entered = false;
String stfId = "STF";
String name = staffNameField.getText();
String contact = contactField.getText();
String email = emailField.getText();
String pwd = passwordField.getText();
String adds = addsField.getText();
int id_count = 0;
String key = "STAFF_LAST_COUNT";
if(name.isEmpty()) {
var += "Name Should not be empty, ";
}else {
is_name_entered = true;
}
if(contact.isEmpty()) {
var += "Contact Should not be empty, ";
}else {
is_contact_entered = true;
}
if(email.isEmpty()) {
var += "Email id Should not be empty, ";
}else {
is_email_entered = true;
}
if(pwd.isEmpty()) {
var += "Password Should not be empty, ";
}else {
is_password_entered = true;
}
if(adds.isEmpty()) {
var += "Address Should not be empty, ";
}else {
is_adds_entered = true;
}
if(is_name_entered && is_contact_entered && is_email_entered && is_password_entered && is_adds_entered) {
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'STAFF_LAST_COUNT'");
ResultSet rs = insert.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
id_count = rs.getInt(1)+1;
}
DecimalFormat df = new DecimalFormat("000");
stfId = stfId+df.format(id_count);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
insert = conn.prepareStatement("insert into staff(staff_id,name,contact_no,email_id,staff_pass,address)values(?,?,?,?,?,?)");
insert.setString(1, stfId);
insert.setString(2, name);
insert.setString(3, contact);
insert.setString(4, email);
insert.setString(5, pwd);
insert.setString(6, adds);
insert.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
insert = conn.prepareStatement("UPDATE system_setting "
+ "SET value = ? "
+ "WHERE key = ?");
insert.setInt(1, id_count);
insert.setString(2, key);
insert.executeUpdate();
conn.close();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
staff_table_update();
staffNameField.setText("");
contactField.setText("");
emailField.setText("");
passwordField.setText("");
addsField.setText("");
var = "Staff has been added successfully";
JOptionPane.showMessageDialog(null, var);
}else {
JOptionPane.showMessageDialog(null, var);
}
}
});
public class SQLiteJDBCDriverConnection {
/**
* Connect to a sample database
*/
public String rate=null;
public String date=null;
public static Connection connect1() {
try {
// SQLite connection string
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
}
// String url = "jdbc:sqlite:C:\\Users\\LWSSD\\eclipse-workspace\\HostelManagement\\hostelmanagement.db";
String url = "jdbc:sqlite:bankmanagement.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
// Staff table Base panel
int staffPanelWidth = panelWidth*64/100;
int staffPanelHeight = panelHeight*86/100;
staffPanel = new JPanel();
staffPanel.setLayout(new FlowLayout());
staffPanel.setLayout(null);
staffPanel.setBounds(panelWidth*36/100,panelHeight*10/100, staffPanelWidth, staffPanelHeight);
staffPanel.setBorder(Utils.getTitledBorder(10, "Search"));
panel.add(staffPanel);
headingFont = new Font("Serif",Font.BOLD,18);
JLabel staff_search_L = new JLabel("Search Staff");
staff_search_L.setAlignmentX(JLabel.LEFT);
//searchLabel.setBackground(Color.decode("#80c2b2"));
staff_search_L.setForeground(Color.black);
staff_search_L.setFont(headingFont);
staff_search_L.setBounds(20,10, 200, 30);
staffPanel.add(staff_search_L);
ImageIcon backround_img1 = new ImageIcon("images/ic_search.png");
Image img1 = backround_img1.getImage();
Image temp_img1 = img1.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
backround_img1 = new ImageIcon(temp_img1);
JLabel background1 = new JLabel("", backround_img1, JLabel.CENTER);
background1.setBounds(20,40, 20, 20);
staffPanel.add(background1);
JTextField staffSearch = new JTextField();
staffSearch.setBackground(Color.decode("#FFFFFF"));
staffSearch.setForeground(Color.black);
staffSearch.setBounds(40,40, 150, 20);
staffSearch.setOpaque(false);
staffSearch.setBorder(BorderFactory.createLineBorder(Color.white, 0));
staffSearch.setBackground(null);
staffPanel.add(staffSearch);
staffSearch.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
search(staffSearch.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
search(staffSearch.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
search(staffSearch.getText());
}
public void search(String str) {
if (str.length() == 0) {
sorter2.setRowFilter(null);
} else {
sorter2.setRowFilter(RowFilter.regexFilter(str));
}
}
});
staffPanel.add(Utils.getSeparator(20, 60, 170, 2,"000000"));
private void staff_table(int width, int height) {
// TODO Auto-generated method stub
String[] columnNames = {"Staff Id",
"Name",
"Contact No",
"Email Id",
"Address",
"Password"
};
DefaultTableModel model1 = new DefaultTableModel();
sorter2 = new TableRowSorter<>(model1);
model1.setColumnIdentifiers(columnNames);
staffTable = new JTable(){
public boolean isCellEditable(int row, int column) {
return false;
};
};
staffTable.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
staffTable.setRowSorter(sorter2);
//table.setEnabled(true);
staffPanel.add(staffTable);
staffTable.setModel(model1);
JScrollPane scrollPane1 = new JScrollPane(staffTable);
scrollPane1.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
staffTable.setFillsViewportHeight(true);
staffPanel.add(scrollPane1);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
staffTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
staffTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
staffTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
staffTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
staffTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
staffTable.getColumnModel().getColumn(5).setCellRenderer( centerRenderer );
staffTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
staffTable.getTableHeader().setBackground(Color.decode("#4287f5"));
staffTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
staffTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
staffTable.setRowHeight(25);
staffTable.getColumnModel().getColumn(0).setPreferredWidth(30);
staffTable.getColumnModel().getColumn(2).setPreferredWidth(40);
staffTable.getColumnModel().getColumn(5).setPreferredWidth(30);
staffTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
// System.out.println(staffTable.getSelectedRow());
editBtn.setVisible(true);
deleteBtn.setVisible(true);
}
});
staff_table_update();
}
Here's logic for Fetch data from database and show it in table.
private void staff_table_update() {
int CC;
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("select staff_id, name, contact_no, email_id, address, staff_pass from staff");
ResultSet rs = insert.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
CC = rsmd.getColumnCount();
DefaultTableModel dft = (DefaultTableModel)staffTable.getModel();
dft.setRowCount(0);
while(rs.next()) {
Vector v2 = new Vector();
for(int ii = 1; ii<=CC; ii++) {
v2.add(rs.getString("staff_id"));
v2.add(rs.getString("name"));
v2.add(rs.getString("contact_no"));
v2.add(rs.getString("email_id"));
v2.add(rs.getString("address"));
v2.add(rs.getString("staff_pass"));
}
dft.addRow(v2);
}
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
To edit and delete Staff on click on table row, we need to show Edit And Delete Button and also its functionality.
// Edite Btn
private JButton getEditButton() {
editBtn = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(150, 30, java.awt.Image.SCALE_SMOOTH)));
editBtn.setContentAreaFilled(false);
editBtn.setBorder(null);
editBtn.setText("EDIT");
editBtn.setHorizontalTextPosition(JButton.CENTER);
editBtn.setVerticalTextPosition(JButton.CENTER);
editBtn.setBounds(350,20, 150, 30);
editBtn.setFont(new java.awt.Font("Serif",1, 12));
editBtn.setForeground(Color.white);
editBtn.setVisible(false);
editBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
addStaffBtn.setVisible(false);
editBtn.setVisible(false);
updateBtn.setVisible(true);
cancleBtn.setVisible(true);
staffNameField.setText(staffTable.getValueAt(staffTable.getSelectedRow(),1).toString());
contactField.setText(staffTable.getValueAt(staffTable.getSelectedRow(),2).toString());
emailField.setText(staffTable.getValueAt(staffTable.getSelectedRow(),3).toString());
passwordField.setText(staffTable.getValueAt(staffTable.getSelectedRow(),5).toString());
addsField.setText(staffTable.getValueAt(staffTable.getSelectedRow(),4).toString());
}
});
return editBtn;
}
// Delete Btn
private JButton getDeleteButton() {
deleteBtn = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(150, 30, java.awt.Image.SCALE_SMOOTH)));
deleteBtn.setContentAreaFilled(false);
deleteBtn.setBorder(null);
deleteBtn.setText("DELETE");
deleteBtn.setHorizontalTextPosition(JButton.CENTER);
deleteBtn.setVerticalTextPosition(JButton.CENTER);
deleteBtn.setBounds(550,20, 150, 30);
deleteBtn.setFont(new java.awt.Font("Serif",1, 12));
deleteBtn.setForeground(Color.white);
deleteBtn.setVisible(false);
deleteBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String staff_id = staffTable.getValueAt(staffTable.getSelectedRow(),0).toString();
String staff_name = staffTable.getValueAt(staffTable.getSelectedRow(),1).toString();
// System.out.println(staff_id + "- "+ staff_name);
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("delete from staff where staff_id= ?");
insert.setString(1, staff_id);
insert.executeUpdate();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
staff_table_update();
editBtn.setVisible(false);
deleteBtn.setVisible(false);
}
});
return deleteBtn;
}
Implement Update And Cancel Button functionality.
updateBtn = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(180, 40, java.awt.Image.SCALE_SMOOTH)));
updateBtn.setContentAreaFilled(false);
updateBtn.setBorder(null);
updateBtn.setText("UPDATE");
updateBtn.setHorizontalTextPosition(JButton.CENTER);
updateBtn.setVerticalTextPosition(JButton.CENTER);
updateBtn.setBounds(actionPanelWidth/2-190,actionPanelHeight*85/100, 180, 40);
updateBtn.setFont(new java.awt.Font("Serif",1, 15));
updateBtn.setOpaque(false);
updateBtn.setContentAreaFilled(false);
updateBtn.setForeground(Color.white);
updateBtn.setVisible(false);
updateBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String stffId = staffTable.getValueAt(staffTable.getSelectedRow(),0).toString();
String name = staffNameField.getText();
String contact = contactField.getText();
String email = emailField.getText();
String pwd = passwordField.getText();
String adds = addsField.getText();
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("UPDATE staff SET name = ?, contact_no = ?, email_id = ?, staff_pass = ?, address = ? WHERE staff_id = ?");
insert.setString(1, name);
insert.setString(2, contact);
insert.setString(3, email);
insert.setString(4, pwd);
insert.setString(5, adds);
insert.setString(6, stffId);
insert.executeUpdate();
conn.close();
System.out.println(stffId );
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
staff_table_update();
addStaffBtn.setVisible(true);
editBtn.setVisible(true);
updateBtn.setVisible(false);
cancleBtn.setVisible(false);
staffNameField.setText("");
contactField.setText("");
emailField.setText("");
passwordField.setText("");
addsField.setText("");
}
});
// Cancel Btn
cancelBtn = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(180, 40, java.awt.Image.SCALE_SMOOTH)));
cancelBtn.setContentAreaFilled(false);
cancelBtn.setBorder(null);
cancelBtn.setText("CANCEL");
cancelBtn.setHorizontalTextPosition(JButton.CENTER);
cancelBtn.setVerticalTextPosition(JButton.CENTER);
cancelBtn.setBounds(actionPanelWidth/2+10,actionPanelHeight*85/100, 180, 40);
cancelBtn.setFont(new java.awt.Font("Serif",1, 15));
cancelBtn.setOpaque(false);
cancelBtn.setContentAreaFilled(false);
cancelBtn.setForeground(Color.white);
cancelBtn.setVisible(false);
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
addStaffBtn.setVisible(true);
editBtn.setVisible(true);
updateBtn.setVisible(false);
cancelBtn.setVisible(false);
staffNameField.setText("");
contactField.setText("");
emailField.setText("");
passwordField.setText("");
addsField.setText("");
}
});
Here's logout button UI and it's functionality.
JButton logoutButton = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(150, 40, java.awt.Image.SCALE_SMOOTH)));
logoutButton.setContentAreaFilled(false);
logoutButton.setBorder(null);
logoutButton.setText("LOGOUT");
logoutButton.setHorizontalTextPosition(JButton.CENTER);
logoutButton.setVerticalTextPosition(JButton.CENTER);
logoutButton.setOpaque(false);
logoutButton.setContentAreaFilled(false);
logoutButton.setBorderPainted(false);
logoutButton.setFont(new java.awt.Font("Serif",1, 15));
logoutButton.setForeground(Color.white);
logoutButton.setBounds(panelWidth-200,0, 150, 40);
panel.add(logoutButton);
logoutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
addStaffFrame.dispose();
loginFrame.setVisible(true);
updateisLoginValue("0");
}
});
At last we will manage closing of window
addStaffFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
addStaffFrame.dispose();
// loginFrame.setVisible(true);
updateisLoginValue("0");
}
});
addStaffFrame.setVisible(true);
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.