I'm trying to build a simple Java Swing application using the MVC architecture pattern. What I've done is create the user interface components (as private) in my views, and have public methods that return the components. These methods are then called by the controllers, through which I can write methods for event/action listeners. Below is a sample example:
View:
private JButton btnAdd;
public JButton getBtnAdd(){
return btnAdd;
}
Control:
myGuiFrame gui = new myGuiFrame();
//on add button clicked
gui.getBtnAdd().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//calls to model
}
});
Is this implementation correct?
If so, then I'm having a problem with FocusListeners. When I create a FocusListener in my view, the focusLost and focusGained methods are created within the view.
private FocusListener l;
someComponent.addFocusListener(l);
l = new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
#Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
};
I want all the event handlers to be in my controllers. My question is ... is there a way I can call/declare the focusLost and focusGained methods from my controller? I tried to define the FocusListener as public so that I can define it in my controller:
view:
public FocusListener l;
public someComponentType someComponent;
controller:
gui.l = new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
#Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
gui.someComponent.addFocusListener(gui.l);
}
};
This however does not work.
Is it possible to handle FocusEvents from the controller?
EDIT:
Gosh, my bad. Didn't quite understand what Robin was all about. I was too fixated on having the FocusListener explicitly defined somewhere. A simple:
gui.getTextFieldEmployeeCode().addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
#Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println("YES!!!");
}
});
in the controller would work just fine in the manner I planned to do it, though I quite like how nIcE cOw's gone about it. Just out of curiosity, is there a standard or widely accepted manner of implementing MVC on Swing Apps?
As far as I understood, the way you doing is this. Better still, I prefer the Anonymous Classes, they respect the concept of Encapsulation. Here try your hands on this code, see what you can grasp with this :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class View
{
private JButton focusButton;
private JButton spareButton;
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("VIEW");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
focusButton = new JButton("GAINED/LOST");
focusButton.addFocusListener(new ButtonController(this));
spareButton = new JButton("SPARE");
spareButton.setOpaque(true);
spareButton.addActionListener(new ButtonController(this));
spareButton.addFocusListener(new ButtonController(this));
contentPane.add(focusButton);
contentPane.add(spareButton);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JButton getFocusButton()
{
return focusButton;
}
public JButton getSpareButton()
{
return spareButton;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new View().createAndDisplayGUI();
}
});
}
}
class ButtonController implements FocusListener, ActionListener
{
private View view;
private JButton focusButton;
private JButton spareButton;
public ButtonController(View v)
{
view = v;
focusButton = view.getFocusButton();
spareButton = view.getSpareButton();
}
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (button == spareButton)
{
spareButton.setBackground(Color.BLUE);
focusButton.setEnabled(true);
}
}
public void focusGained(FocusEvent fe)
{
JButton button = (JButton) fe.getSource();
if (button == focusButton)
{
focusButton.setEnabled(true);
}
else if (button == spareButton)
{
spareButton.setBackground(Color.WHITE);
}
}
public void focusLost(FocusEvent fe)
{
JButton button = (JButton) fe.getSource();
if (button == focusButton)
{
focusButton.setEnabled(false);
}
else if (button == spareButton)
{
spareButton.setBackground(Color.DARK_GRAY.darker());
}
}
}
Another approach, if you feel irritated by getters/setters and sending references around, is to define one inner class as follows (A Simple workaround for the previous example) :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class View
{
private JButton focusButton;
private JButton spareButton;
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("VIEW");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
focusButton = new JButton("GAINED/LOST");
focusButton.addFocusListener(new ButtonController());
spareButton = new JButton("SPARE");
spareButton.setOpaque(true);
spareButton.addActionListener(new ButtonController());
spareButton.addFocusListener(new ButtonController());
contentPane.add(focusButton);
contentPane.add(spareButton);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class ButtonController implements FocusListener, ActionListener
{
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (button == spareButton)
{
spareButton.setBackground(Color.BLUE);
focusButton.setEnabled(true);
}
}
public void focusGained(FocusEvent fe)
{
JButton button = (JButton) fe.getSource();
if (button == focusButton)
focusButton.setEnabled(true);
else if (button == spareButton)
spareButton.setBackground(Color.WHITE);
}
public void focusLost(FocusEvent fe)
{
JButton button = (JButton) fe.getSource();
if (button == focusButton)
focusButton.setEnabled(false);
else if (button == spareButton)
spareButton.setBackground(Color.DARK_GRAY.darker());
}
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new View().createAndDisplayGUI();
}
});
}
}
Related
I have some problems with using dispose() method in my GUI project.
I' am making a GUI swing application for some kind of Elections in IntelliJ.
My problem is, by clicking a button(Confirm1, or 2 or 3) I want to open new JFrame which is checking the age of voter and closes the current JFrame where this button is located by calling dispose().
But frame.dispose(); doesn't work.
I have my JFrame declared in public static main().
Should I make reference for it in my ActionListener? I have been looking for solution, but I couldn't find any.
Here is a code:
import javax.swing.*; //another libraries
public class ElectionGUI {
private JPanel labelElection; // another texfields or etc.
private JButton Confirm1;
private JButton Confirm3;
private JButton Confirm2;
private JPanel Elections;
public VotesGUI(){
Votes votes = new Votes("...","...",0);
listX.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
NrX.setText(listX.getSelectedValue().toString());
}
}
});
listY.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
NrY.setText(listY.getSelectedValue().toString());
}
}
});
listZ.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
NrZ.setText(listZ.getSelectedValue().toString());
}
}
});
Confirm1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
votes.VotesX();
votes.countVotes();
CheckAge age = new CheckAge();
age.Check(); /// referention, to my next //Jframe called psvm Check();
}
});
Confirm2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
votes.VotesY();
votes.countVotes();
CheckAge age = new CheckAge();
age.Check();
}
});
Confirm3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
votes.VotesZ();
votes.countVotes();
CheckAge age = new CheckAge();
age.Check();
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Elentions");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setContentPane(new ElectionGUI().labelElection);
frame.pack();
}
}
as I said in other posts I'm new in Java and I'm having some dumb problems, here's the deal:
I have a radioButton (radioStock) and a textField (stockField). I want stockField to be setEnabled(false) by default, no problem with that, and whenever the radioStock is checked, set the stockField enabled on true. I wrote this code, but it doesn't work.
if (radioStock.isSelected()) {
stockField.setEnabled(true);
}else{
stockField.setEnabled(false);
}
That code needs to be in a listener that is attached to the JRadioButton such as an ActionListener or ItemListener. And you don't even need the if blocks since all you'd need is one line of code inside of the listener:
radioStock.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent itemEvent) {
stockField.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
}
});
For more on use of JRadioButtons, please check out the tutorial: button tutorial.
Edit my SSCCE
import java.awt.event.*;
import javax.swing.*;
public class ItemListenereg {
private static void createAndShowGui() {
final JRadioButton radioStock = new JRadioButton("Stock", true);
final JTextField stockField = new JTextField(10);
JPanel panel = new JPanel();
panel.add(radioStock);
panel.add(stockField);
radioStock.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent itemEvent) {
stockField.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
}
});
JOptionPane.showMessageDialog(null, panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
This should work
radioStock.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if(radioStock == e.getSource())
{
stockField.setEnabled(radioStock.isSelected());
}
}
});
I'm blocked with a probelm about Java and the use of JTree:
I want to create a JTree with, node by node, some JButtons components (or Images, I don't mind), like in the following picture. It will be 3 or 4 buttons in the same row. I succeed to do that.
But where I'm blocked is when I want to add a mouselistener on each of this button to manage their tooltip or an action on them.
In fact the JTree component is most of the time used to manage the action on the full node, but not on its inside components.
I did a short code, in comparaison at the real big code I have to work in, to quickly test what I say:
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
public class TreeWithPopup extends JPanel {
DefaultMutableTreeNode root, node1, node2, node3;
public TreeWithPopup() {
MyJTree tree;
root = new DefaultMutableTreeNode("root", true);
node1 = new DefaultMutableTreeNode("node 1", true);
node2 = new DefaultMutableTreeNode("node 2", true);
node3 = new DefaultMutableTreeNode("node 3", true);
root.add(node1);
node1.add(node2);
root.add(node3);
setLayout(new BorderLayout());
tree = new MyJTree(root);
tree.setCellRenderer(new PCellRenderer());
add(new JScrollPane((JTree) tree), "Center");
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Tree with button");
TreeWithPopup panel = new TreeWithPopup();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel, "Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
class MyJTree extends JTree implements ActionListener {
MyJTree(DefaultMutableTreeNode dmtn) {
super(dmtn);
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.out.println("JTree.MouseListener");
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("JTree.MouseListener");
}
public void mouseExited(MouseEvent arg0) {
System.out.println("JTree.MouseListener");
}
public void mousePressed(MouseEvent arg0) {
System.out.println("JTree.MouseListener");
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("JTree.MouseListener");
}
});
}
public void actionPerformed(ActionEvent ae) {
System.out.println("MyJTree.ActionPerformed");
}
}
class PCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree ptree, Object pvalue, boolean psel, boolean pexpanded, boolean pleaf, int prow,
boolean phasFocus) {
Box myPanel = new Box(BoxLayout.X_AXIS);
JButton myButton = new JButton("test");
Image imgToUse = null;
Image imgRollOver = null;
try {
URL urlIcon = new URL("file:///C:/1.jpg"); // <===== change their the path to icons
imgToUse = ImageIO.read(urlIcon);
urlIcon = new URL("file:///C:/2.jpg"); // <===== change their the path to icons
imgRollOver = ImageIO.read(urlIcon);
} catch (IOException e) {
e.printStackTrace();
}
myButton.setRolloverIcon(new ImageIcon(imgRollOver));
myButton.setIcon(new ImageIcon(imgToUse));
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(" detected on ");
}
});
myButton.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.out.println("myButton.MouseListener");
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("myButton.MouseListener");
}
public void mouseExited(MouseEvent arg0) {
System.out.println("myButton.MouseListener");
}
public void mousePressed(MouseEvent arg0) {
System.out.println("myButton.MouseListener");
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("myButton.MouseListener");
}
});
myPanel.add(myButton);
return myPanel;
}
}
You just have to change the icon path or put the two following icons at "c:/"
I also searched to use the x/y position of the row event but I was unable to find the position of my button after rendering.
If anyone can have an idea how to do that, he could be very helpful.
Thanks, at least for read this question ;-)
Tooltip support is actually provided by the TableCellRenderer directly. Your TableCellRenderer is a little muddled. It extends from DefaultTreeCellRenderer but makes no use of any of it's features, instead creating a new Box, JButton and loading icons each time a cell is rendered...
This is going to increase your memory usage a slow you application down...
Instead, try something like...
class PCellRenderer extends Box implements TreeCellRenderer {
private Image imgToUse = null;
private Image imgRollOver = null;
public PCellRenderer() {
super(BoxLayout.X_AXIS);
JButton myButton = new JButton("test");
try {
URL urlIcon = new URL("file:///C:/1.jpg"); // <===== change their the path to icons
imgToUse = ImageIO.read(urlIcon);
urlIcon = new URL("file:///C:/2.jpg"); // <===== change their the path to icons
imgRollOver = ImageIO.read(urlIcon);
} catch (IOException e) {
e.printStackTrace();
}
myButton.setRolloverIcon(new ImageIcon(imgRollOver));
myButton.setIcon(new ImageIcon(imgToUse));
add(myButton);
}
public Component getTreeCellRendererComponent(JTree ptree, Object pvalue, boolean psel, boolean pexpanded, boolean pleaf, int prow,
boolean phasFocus) {
// set the tooltip text here...
// Maybe change the icon...
return this;
}
}
Now, actually doing something...
Renderers are rubber stamps. That are not actually life components. Think of them like photos. You can take a snap shot of your friends, but you can't interact with them...same thing here...
Your idea of a MouseListener on the JTree is a correct one, in fact the JavaDocs actually have a demonstration of this...
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
mySingleClick(selRow, selPath);
}
else if(e.getClickCount() == 2) {
myDoubleClick(selRow, selPath);
}
}
}
So I have a class that extends a JPanel and within the constructor I add my JButtons and whatever else I need to add. I also have a MainFrame class that is the container (JFrame) and this class will take an argument from a class called FrameSwitcher (Controller) which will assess what buttons were clicked, and pass the information to the MainFrame
I'm having troubles doing this, I can't find a proper way to do this. I do also wish to maintain the JButtons private and non static.
JPanel example:
public class MainMenu() {
private JButton btnSinglePlayer, btnMultiPlayer;
public MainMenu() {
setLayout(null);
btnSinglePlayer = new JButton("singlePlayer");
btnSinglePlayer.setBounds(320, 25, 275, 130);
add(btnSinglePlayer);
btnMultiPlayer = new JButton("MultiPlayer");
btnMultiPlayer.setBounds(320, 170 , 275, 130);
add(btnMultiPlayer);
}
}
FrameSwitcher:
public class FrameSwitcher implements panelListener { // panelListener is an interface defined else where.
public FrameSwitcher(MainFrame frame) {
// This is irrelevant to the question.
}
#Override
public void gamePanel() {
System.out.println("GamePanel Event: Recieved");
}
#Override
public void mainMenu() {
System.out.println("mainMenu Event: Recieved");
}
#Override
public void scoreBoardPanel() {
System.out.println("scoreBoardPanel Event: Recieved");
}
}
Then my MainFrame:
public class MainFrame extends JFrame implements ActionListener {
private PanelListener panelListener;
private JFrame mainContainer = new JFrame("Game");
private JPanel mainMenu = new MainMenu();
public void start() {
mainContainer(mainMenu);
}
public MainFrame(JPanel frame) {
mainContainer.getContentPane().add(frame);
mainContainer.pack();
// Other methods to initialize the frame
return mainContainer;
}
public void switchFrames(PanelListener panelListener) {
this.panelListener = panelListener; // PanelListener is an interface.
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getsource();
if(source == MainMenu.btnSinglePlayer) {
if(panelListener != null) {
System.out.println("Recieved the event approriately.");
}
}
}
}
In this example, it does compile, but doesn't do what it is supposed to. Another thing is I currently have the JButtons as public and static, I don't want that.
In your MainMenu class, you need to add some kind of listener that interested parties can register with, so when some event occurs, they can be notified.
The simplest solution would be to provide a addActionListener method which delegated to each of the buttons. This, however, has may expose portions of the application you don't exposed (a listener now has direct access to the JButton and can do all kinds of nasty things to it).
A better solution would be to create something like a MainMenuListener which had methods like startSinglePlayer and startMultiPlayer
You would then provide a add/removeMainMenuListener method within in your MainMenu class.
Each button would then register there own actionListener and fire the appropriate menu listener event
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainMenu extends JPanel {
private JButton btnSinglePlayer, btnMultiPlayer;
public MainMenu() {
setLayout(null);
btnSinglePlayer = new JButton("singlePlayer");
btnSinglePlayer.setBounds(320, 25, 275, 130);
add(btnSinglePlayer);
btnSinglePlayer.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
fireStartSinglePlayer();
}
});
btnMultiPlayer = new JButton("MultiPlayer");
btnMultiPlayer.setBounds(320, 170, 275, 130);
add(btnMultiPlayer);
btnMultiPlayer.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
fireStartMultiPlayer();
}
});
}
public void addMainMenuListener(MainMenuListener listener) {
listenerList.add(MainMenuListener.class, listener);
}
public void removeMainMenuListener(MainMenuListener listener) {
listenerList.remove(MainMenuListener.class, listener);
}
public void fireStartSinglePlayer() {
MainMenuListener[] listeners = listenerList.getListeners(MainMenuListener.class);
if (listeners != null && listeners.length > 0) {
for (MainMenuListener listener : listeners) {
listener.startSinglePlayer();
}
}
}
public void fireStartMultiPlayer() {
MainMenuListener[] listeners = listenerList.getListeners(MainMenuListener.class);
if (listeners != null && listeners.length > 0) {
for (MainMenuListener listener : listeners) {
listener.startMultiPlayer();
}
}
}
public interface MainMenuListener extends EventListener {
public void startSinglePlayer();
public void startMultiPlayer();
}
}
First you have to create an ActionListener like this
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
//do your stuff
}
}
Then call,
yourButton.addActionListener(new MyActionListener());
I wonder how one should implement an event which will do some Action when a button is pressed, and stop do that action when button is released
I tried to add MouseListerner for this approach. The problem is it will recognize that I have pressed the button. But not which button it is. So wonder how should it be written so it will know which button I have pressed down.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Main extends JFrame implements MouseListener, ActionListener{
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel(new GridLayout(4,4));
public Main() {
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
String[] buttonNamn = {"1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
for(int i=0;i<buttonNamn.length;i++) {
JButton button = new JButton(buttonNamn[i]);
panel2.add(button);
button.addMouseListener(this);
button.addActionListener(this);
}
}
public static void createGUI() {
new Main();
}
public static void main(String[] args) {
createGUI();
}
#Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
System.out.println("Pressed");
}
#Override
public void mouseReleased(MouseEvent arg0) {
System.out.println("Relased");
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("1")) {
System.out.println("Foo 1");
}
else if(e.getActionCommand().equals("2")){
System.out.println("Foo 2");
}
}
}
You will need invokeLater as described here: Java: mytextarea.setText("hello") + Thread.sleep(1000) = strange result
otherwise your action will block the UI-thread
For the start look at SwingUtilities.invokeLater.
For the cancel, I generally use an interface with a cancel operation on it which the Runnable provided to the invokeLater implements, the cancel button then kicks off the cancel simply by calling the cancel operation. How this cancels teh operation depends on what it does, maybe it could set a cancelled flag which the running operation can periodically check to see if it should continue.
Note that you can extend listener implementation classes like MouseAdapter to save yourself some typing (only override the methods you want to use).
Rather than use a single listener and have a bunch of case statements to try and figure out the relationship between the button and the data, add a new listener to each button and hold the data in the listener. This code does this with an anonymous class.
public static void main(String[] args) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
String[] buttonName = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F" };
for (final String name : buttonName) {
JButton button = new JButton(name);
panel.add(button);
button.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e) {
System.out.println("pressed:" + name);
}
#Override public void mouseReleased(MouseEvent e) {
System.out.println("released:" + name);
}
});
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
Note the use of the final keyword.
There are other ways to express this, if you prefer. This method adds a static inner class:
private static class MyListener extends MouseAdapter {
private final String name;
public MyListener(String name) {
this.name = name;
}
#Override public void mousePressed(MouseEvent e) {
System.out.println("pressed:" + name);
}
#Override public void mouseReleased(MouseEvent e) {
System.out.println("released:" + name);
}
}
public void addStaticInnerClassListener(JButton button, String name) {
button.addMouseListener(new MyListener(name));
}
This one uses a class scoped to the method:
public void addNameListener(JButton button, final String name) {
class MyListener extends MouseAdapter {
#Override public void mousePressed(MouseEvent e) {
System.out.println("pressed:" + name);
}
#Override public void mouseReleased(MouseEvent e) {
System.out.println("released:" + name);
}
}
button.addMouseListener(new MyListener());
}
The code/action you want performed will have to be performed in another thread. Swing provides some utility classes for this, such as SwingWorker.
The getSource method of MouseEvent inherited from EventObject should give you a reference to the clicked button
java api link
Use MouseEvent.getButton() to get the button that triggered the event.