Reputation: 29
My test project is just like following:
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(()->{
JFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class MyFrame extends JFrame {
private JButton imgBtn = new JButton("Open"); // Create a button named “Open”.
public MyFrame() {
setPreferredSize(new Dimension(960, 540));
setSize(getPreferredSize());
initBtnListener();
// deliverParamTest(s);
}
// Set listener for imgBtn to respond to a click event.
public void initBtnListener() {
imgBtn.addMouseListener(new MyMouseListener(imgBtn));
}
// A method to receive a parameter from imgBtn's adapter and print it out.
public void passParamTest(String s) {
System.out.println(s);
}
}
And a MyMouseListener class is created in another file:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
public class MyMouseListener extends MouseAdapter {
private JButton btn;
private String testString = "A String to be delivered to another class as a parameter";
public MyMouseListener(JButton btn) {
this.btn = btn;
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
switch (btn.getText()) {
// Deal with the pass logic here.
case "Open":
// Pass the String testString to Test class
break;
default:
break;
}
}
}
And now the question is how can I pass testString to the test file as a parameter of the passParamTest() method in MyFrame class?
Upvotes: 0
Views: 136
Reputation: 6448
You could pass the instance of your JFrame
as a constructor parameter and call the specific method from within your clicked
event.
public class MyMouseListener extends MouseAdapter {
private JButton btn;
private JFrame myFrame;
private String testString = "A String to be delivered to another class as a parameter";
public MyMouseListener(JButton btn, JFrame myFrame) {
this.btn = btn;
this.myFrame = myFrame;
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
switch (btn.getText()) {
// Deal with the pass logic here.
case "Open":
// Pass the String testString to Test class
myFrame.passParamTest(this.testString);
break;
default:
break;
}
}
}
Upvotes: 0
Reputation: 337
One solution is to pass your JFrame-instance to your listener:
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(()->{
JFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class MyFrame extends JFrame {
private JButton imgBtn = new JButton("Open");
public MyFrame() {
setPreferredSize(new Dimension(960, 540));
setSize(getPreferredSize());
initBtnListener();
}
// Set listener for imgBtn to respond to a click event.
public void initBtnListener() {
imgBtn.addMouseListener(new MyMouseListener(imgBtn, this));
}
// A method to receive a parameter from imgBtn's adapter and print it out.
public void passParamTest(String s) {
System.out.println(s);
}
}
MouseListener:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
public class MyMouseListener extends MouseAdapter {
private JButton btn;
private String testString = "A String to be delivered to another class as a parameter";
private MyFrame myFrame;
public MyMouseListener(JButton btn, MyFrame myFrame) {
this.btn = btn;
this.myFrame = myFrame;
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
switch (btn.getText()) {
// Deal with the pass logic here.
case "Open":
myFrame.passParamTest(testString);
// Pass the String testString to Test class
break;
default:
break;
}
}
}
Upvotes: 1