My button is not gaving out any action when clicked:
(libraryBtn):

package LibraryDatabase;

import static javax.swing.JOptionPane.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class PostLibraryDatabase extends JFrame implements ActionListener {

// Text Fields XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    JTextField firstName, surName, id, serialNumber, category, dateToday, coverTitle, isbnNumber,
		       authoursName, coAuthoursName, pubDate, loanDate, returnedDate, email, details;
// JButton Fields XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    JButton writeBtn, displayBtn, exitBtn, libraryBtn, resetBtn ;
    
// ImageIcon Fields XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    ImageIcon pic = new ImageIcon ("Postlib.png");
		JButton picBtn = new JButton ("" ,pic);

// TextArea Fields  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    TextArea information = new TextArea(18, 97);

    DBHandler db = new DBHandler();


    public static void main(String[] args) {
        new PostLibraryDatabase();
    }

    public PostLibraryDatabase() {
        setLayout(new BorderLayout());

// text Field


                firstName = new JTextField(16);
                surName = new JTextField(17);
                id = new JTextField(10);
		serialNumber = new JTextField(18);
		category = new JTextField(21);
		dateToday = new JTextField(8);
		coverTitle = new JTextField(25);
		isbnNumber = new JTextField(30);
		authoursName = new JTextField(16);
		coAuthoursName = new JTextField(17);
		pubDate = new JTextField(8);
		loanDate = new JTextField(8);
		returnedDate = new JTextField(8);
                email = new JTextField(27);
                details = new JTextField(48);


                writeBtn = new JButton("Write to database");
                displayBtn = new JButton("Display Records");
                libraryBtn = new JButton("Library Record");
                resetBtn = new JButton("Reset");
                exitBtn = new JButton("Exit");

// position top XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        JPanel top = new JPanel();
        add("North", top);
// position center XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        JPanel middle = new JPanel();
        middle.add(new JLabel("First name:"));
        middle.add(firstName);
        middle.add(new JLabel("Surname:"));
        middle.add(surName);
        middle.add(new JLabel("Library ID:"));
        middle.add(id);


        middle.add(new JLabel("Category name:"));
        middle.add(category);
        middle.add(new JLabel("Cover Title:"));
        middle.add(coverTitle);
        middle.add(new JLabel("Today's Date:"));
        middle.add(dateToday);
        middle.add(new JLabel("                            ISBN Number:"));
        middle.add(isbnNumber);
        middle.add(new JLabel("Authours Name(s):"));
        middle.add(authoursName);
        middle.add(new JLabel("                  Co - Authours Name(s):"));
        middle.add(coAuthoursName);
		middle.add(new JLabel("Serial Number:"));
        middle.add(serialNumber);
		middle.add(new JLabel("   Dated Published :"));
        middle.add(pubDate);
		middle.add(new JLabel("      Loan Date:"));
        middle.add(loanDate);
		middle.add(new JLabel("Returned Date:"));
        middle.add(returnedDate);
		middle.add(new JLabel("             Borrowers E-mail Address:"));
        middle.add(email);
		middle.add(new JLabel("Book / Magazine Details:"));
        middle.add(details);


        information.setText(LibraryData.listAll());
        middle.add(information);
        

        add("Center", middle);

// position center
        JPanel bottom = new JPanel();

        bottom.add(writeBtn);
        bottom.add(displayBtn);
	bottom.add(libraryBtn);
        bottom.add(resetBtn);
	bottom.add(exitBtn);

        add("South", bottom);
        add("West", new JPanel());
        add("East", new JPanel());

// ActionListener XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        writeBtn.addActionListener(this);
        displayBtn.addActionListener(this);
        libraryBtn.addActionListener(this);
        resetBtn.addActionListener(this);
        exitBtn.addActionListener(this);

// Windows Size  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        setSize(730, 690);
        top.add (picBtn);
        
        setTitle("Post Library Database");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
    }
//  Action Performed XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == writeBtn) {
            String f = firstName.getText();
            String s = surName.getText();
            String z = id.getText();
            String a = serialNumber.getText();
            String b = category.getText();
            String c = dateToday.getText();
            String d = coverTitle.getText();
            String g = isbnNumber.getText();
            String h = authoursName.getText();   
            String i = coAuthoursName.getText();
            String k = pubDate.getText();
            String l = loanDate.getText();
            String m = returnedDate.getText();
            String n = email.getText();
            String o = details.getText();


// XXXXXX if any field is blank, signal an error XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            if (f.equals("") || s.equals("") || i.equals("")) {
                showMessageDialog(this, "One or more fields blank");
                return;
            }
            boolean ok = db.write(z, s, f, a, b, c, d, g, h, i, k, l, m, n, o);
            id.setText("");
            if (!ok) showMessageDialog(this, "Duplicate key " + id);
            else {
                firstName.setText("");
                surName.setText("");
                serialNumber.setText("");
                category.setText("");
                dateToday.setText("");
                coverTitle.setText("");
                authoursName.setText("");
                coAuthoursName.setText("");
                pubDate.setText("");
                loanDate.setText("");
                returnedDate.setText("");
                email.setText("");
                details.setText("");
            }
        
            if (e.getSource() == libraryBtn) {
                 new CheckLin();
             

	} else if (e.getSource() == exitBtn) {
            LibraryData.close();
	    System.exit(0);
			}

		   }
		}
	}

Dani AI

Generated

The visible symptom — only the first branch behaving and the other buttons appearing inert — is the classic sign of a mis-nested actionPerformed block. correctly pointed out mismatched braces and noted the missing handlers for displayBtn and resetBtn; 's suggestion to add a trailing else for diagnostics is also useful. The immediate fix is to ensure the if (e.getSource() == writeBtn) block is closed before testing libraryBtn, displayBtn, resetBtn, and exitBtn, so each test runs at the same level instead of being nested.

Practical checklist to repair and verify:

  • Reformat the method in an IDE and use brace-matching/highlighting to find the misplaced }.
  • Add lightweight logging at the top of actionPerformed to print the event source and action command, and keep a final else that logs any unhandled events (this reveals whether events reach the method).
  • Confirm addActionListener(this) is actually called for every button. If uncertainty remains, attach separate listeners (anonymous classes or lambdas) to isolate behavior.
  • Implement the missing displayBtn and resetBtn branches; make sure the libraryBtn and exitBtn checks are not nested inside the writeBtn block.

A few robustness improvements not in the thread: create the GUI on the Event Dispatch Thread (see the Swing concurrency notes at the Oracle tutorial: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). Avoid mixing AWT TextArea with Swing components (use JTextArea in a JScrollPane) to prevent focus/z-order oddities (see mixing heavyweight/lightweight guidance: ). Using pack() with proper layout managers instead of fixed setSize makes the UI more predictable and easier to debug.

Once the braces are fixed and each button has its own top-level branch or listener, retest with simple prints. If events still do not arrive, verify components are enabled/visible and that no other code swallows events. For reference on the event listener contract, see the ActionListener docs: https://docs.oracle.com/javase/8/docs/api/java/awt/event/ActionListener.html.

It seems that you have messed up with the brackets.
You have this:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == writeBtn) {
        
            if (e.getSource() == libraryBtn) {

            } else if (e.getSource() == exitBtn) {

	    }

       }
}

Maybe you need this:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == writeBtn) {
             
	} else if (e.getSource() == libraryBtn) {

        } else if (e.getSource() == exitBtn) {

	}
}

Can you please verify it?

(1) In void actionPerformed(ActionEvent e)method body there are missing code blocks for two cases:
if (e.getSource() == displayBtn){...} and
if (e.getSource() == resetBtn){...}
(2) in void actionPerformed(ActionEvent e)method body
one curly bracket '}' is missing in line 178
That's why, I guess, the buttons do not work properly.

While debugging I'd add a trailing else:

} else if (e.getSource() == exitBtn) {
 
	}else {
            System.out.println("unhandled action " + e); // tell em about it
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.