Java Action Listener not working

I have two questions:

1 . why when I execute the code, when I click on the button P it doesn't show the println. 2. I have tried to make a background with JLabel it works fine but it doesn't cover all of the JFrame. I tried JFrame with try and catch and it doesn't display it.

JPanel L1 = new JPanel();
JButton P = new JButton("Open"); JButton P1 = new JButton("Cancel"); Dimension D = new Dimension(80 , 30); Container C = getContentPane(); JLabel Label2 = new JLabel(new super.setTitle("Ismail Application"); //Buttons //Button 1 P.setToolTipText("Click To Open"); P.setPreferredSize(D); //Button 2 P1.addActionListener(this); P1.setToolTipText("Click to exit program"); P1.setPreferredSize(D); //Adding Components L1.add(P, BorderLayout.WEST); L1.add(P1, BorderLayout.EAST); add(L1, BorderLayout.SOUTH); P1.addActionListener(this); P.addActionListener(this); //Labels Label2.setLayout(null); Label2.setSize(400,300); Label2.setToolTipText("This is the Background"); add(Label2, BorderLayout.NORTH);
}
public void actionPerformed (ActionEvent e) { if(e.getSource() == P) { System.out.println("not working"); } if(e.getSource() == P1){ } }

I hope you guys can help

Thanks

2

3 Answers

Since the code you posted does not compile and cannot be run by us, it is hard to say what is going on. See below for the most basic example of a JButton with an ActionListener attached to it, which prints something each time the button is pressed. Compare that with your code to find the differences, or adjust your code to a sscce.

import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FrameWithButtonExample { public static void main( String[] args ) { EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame frame = new JFrame( "TestFrame" ); final JButton testButton = new JButton( "TestButton" ); testButton.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent aActionEvent ) { //if check to match the code from the question, but not really needed if ( aActionEvent.getSource() == testButton ){ System.out.println("TestButton pressed"); } } } ); frame.add( testButton ); frame.pack(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible( true ); } } ); }
}
0

You didn't show whether or not you implement ActionListener on the class. That said, you're better off doing this:

P.setActionCommand("P_BUTTON");

then

if (e.getActionCommand().equals("P_BUTTON"))

than the way you're doing it.

1

Since you are using Layout, then stop giving your personal Dimensions to the components.Add all setPreferredSize() thingies as comments. Better add your JLabel to the Center instead of North, so that it can fill the maximum area on the component. Moreover provide a complete code so that we can look into your ActionListener thing too. For the JLabel part, remove

add(Label2, BorderLayout.NORTH);

replace it with this

add(Label2, BorderLayout.CENTER);

And for your ActionListener part try this

public void actionPerformed(ActionEvent ae)
{ JButton button = (JButton)ae.getSource(); if (button.equals(P)) { System.out.println("not working."); } else if (button.equals(P1)) { System.out.println("working."); }
}

Hope that might help.

Regards

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like