I'm trying to change button action in a subclass because the form is pretty much exactly the except one asks for an ID. What I i tried doing was making a ActionListener object and instantiating it to an object of an anonymous class like so:
class ParentClass extends JPanel{
JButton button;
ActionListener buttonAction;
ParentClass{
button = new JButton("Parent Action");
buttonAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the parent class");
}
};
button.add(buttonAction);
add(button);
}
}
class ChildClass extends ParentClass{
JButton button;
ActionListener buttonAction;
ChildClass{
super();
buttonAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the child class");
}
};
}
}
public static void main(String[] args){
JFrame frame = new JFrame;
frame.add(new ChildClass());
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
I was trying to use this method but the actionPerformed of buttonAction is never called. How can I make the button action different for the parent class and the subclass?
You can let parent class implement ActionListener, then use button.addActionListener(this) in order to add the action to button. Then in the subclass #Override actionPerformed method:
class ParentClass extends JPanel implements ActionListener
{
ParentClass()
{
JButton button = new JButton("something");
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent event)
{
System.out.println("I am the parent.");
}
}
class SubClass extends ParentClass
{
SubClass()
{
super();//initialize button
}
#Override
public void actionPerformed(ActionEvent event)
{
System.out.println("I am the child.");
}
}
Another way is to add the ActionListener and inside it, only call a method. Something like buttonPressed. Then in subclass #Override buttonPressed method.
A complete example:
public class Test extends JFrame {
public Test() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
add(new ParentPanel());
add(new ChildPanel());
pack();
setLocationByPlatform(true);
}
private class ParentPanel extends JPanel implements ActionListener {
public ParentPanel() {
super(new BorderLayout());
JButton button = new JButton("My Class:" + getClass());
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Parent");
}
}
private class ChildPanel extends ParentPanel {
public ChildPanel() {
super();
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Child");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
}
I the method I posted works. The issue is if you don't remove and add the button to the subclass it doesn't change the action that will run
class ParentClass extends JPanel{
JButton button;
ActionListener buttonAction;
ParentClass{
button = new JButton("Parent Action");
buttonAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the parent class");
}
};
button.add(buttonAction);
add(button);
}
}
So in the subclass what you would do is this:
class ChildClass extends ParentClass{
JButton button;
ActionListener buttonAction;
ChildClass{
super();
buttonAction = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the child class");
}
};
button.removeActionListener(button.getActionListeners()[0]);
button.addActionListener(buttonAction);
}
}
I, however, do not know why but would like an explanation as to why buttonAction had to be re-registered.
Related
I am making a game where the user has to press keys to move around. I am using keybindings but they are not working. The keybindings are supposed to call the Wp class and print "W pressed", but nothing happens. Here's the code:
public class SO extends JFrame {
public static void main(String[] args) {
new SO();
}
C c;
public SO(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
c=new C();
c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "wp");
c.getActionMap().put("wp", new Wp());
this.setVisible(true);
}
private class C extends JComponent {
public void paint(Graphics g){}
}
private class Wp extends AbstractAction {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("W pressed");
}
}
}
Use Action to call like component.getActionMap().put("doSomething", anAction);
Refer Key Binding for more information. Below is a sample code I have referred in another Stackoverflow Question reference link SO Ref link
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonBinding {
private JPanel contentPane;
private JTextField tField;
private JButton button;
private KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
private Action action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Action Performed");
contentPane.setBackground(Color.BLUE);
}
};
private MouseAdapter mouseActions = new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent me) {
System.out.println("Mouse Entered");
JButton button = (JButton) me.getSource();
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "enter");
button.getActionMap().put("enter", action);
}
#Override
public void mouseExited(MouseEvent me) {
System.out.println("Mouse Exited");
JButton button = (JButton) me.getSource();
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "none");
contentPane.setBackground(Color.RED);
}
};
private void displayGUI() {
JFrame frame = new JFrame("Button Binding Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
contentPane.setOpaque(true);
tField = new JTextField(10);
button = new JButton("Click Me");
button.addMouseListener(mouseActions);
contentPane.add(tField);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new ButtonBinding().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
I am really new to GUI, got a little problem when I was trying to study it.
Ok here is my code.
public class Sample implements ActionListener{
public void go() {
JButton button = new JButton("Click");
JFrame frame = new JFrame();
frame.getContentPane().add(button);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
button.setText("Hello");
}
});
}
It keeps telling me that The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new ActionListener(){}). I don't get it because I remember I did it before and it could work.
......
I don't get an error but the action listener won't work because the action performed method of the ActionListener Interface needs to overriden.
button.addActionListener(new ActionListener() {
// add the annotation below
#Override
public void actionPerformed(ActionEvent e) {
button.setText("hello");
}
});
And typically you would build the JFrame in the main method. Later they added an Invoke Later runner that when the class extends JFrame it would create the window in a more object oriented manner.
public class App extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
JFrame frame = new App();
frame.setVisible(true);
} catch (Exception e){
e.printStackTrace();
}
}
});
}
public App(){
JButton button = new JButton("Click");
getContentPane().add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100,100);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
button.setText("hello");
}
});
}
}
I have two jframes,
I want to get value from opened another jframe to other opened jframe.
when click jframe1 open button showing jframe2 and type some text in text field and click ok button, text field value want to get jframe1 jlable. how to do this i tried but i can't find a way to do this.
Is this possible ?
Use a callback,
add this code to your project:
Define an interface
public interface ICallbackListener{
void onNewEvent(String msg);
}
add to jframe 2:
private ICallbackListener myListener;
public void addCallback(ICallbackListener myListener){
this.myListener = myListener;
}
...
if(myListener!=null){
myListener.onNewEvent("myMessage");
}
...
add to jframe 1:
private ICallbackListener myListener;
ICallbackListener i = new ICallbackListener() {
#Override
public void onNewEvent(String msg) {
// TODO Auto-generated method stub
}
};
public void setCallback( ){
jframe2.addCallback(myListener);
}
now, every thime the jframe2 call the interface method you will get asynchronous a call to the TODO label in the jframe1
Try This
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.*;
class TestFrameExample extends JFrame implements ActionListener{
static JLabel label ;
public static TestFrameExample test;
TestFrameExample()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
label = new JLabel("This is a label!");
JButton button = new JButton("Open");
button.setText("Press me");
button.addActionListener(this);
panel.add(label);
panel.add(button);
add(panel);
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
new TestFrameExample1();
}
public static void main(String s[]) {
test=new TestFrameExample();
}
}
class TestFrameExample1 extends JFrame implements ActionListener {
JTextField t;
TestFrameExample test;
public TestFrameExample1()
{
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
t=new JTextField();
t.setBounds(100,20,150,20);
JButton button=new JButton("oK");
button.setBounds(100,50,100,30);
button.addActionListener(this);
add(t);
add(button);
}
public void actionPerformed(ActionEvent a)
{
test.label.setText(t.getText());
}
}
create a method that takes jframe1 in the jframe2
in the open button action event create a object from jframe2 and call that method that take jframe1.
so when u click Ok button in the jframe2 pass that text field value to the jframe1 object (that u passed to the jframe2) via a methdo
public class jframe1 {
public void actionPerformed(ActionEvent a){
jfame2 jf2 = new jframe2();
jf2.setJframe1(this);
}
public void updateLable(String value){
lblIdk.setText(value);
}
}
public class jframe2 {
private jframe1 jf1;
public void setJframe1(jframe1 jf1){
this.jf1 = jf1;
}
public void actionPerformed(ActionEvent a){
this.jf1.updateLable(txtidk.getText());
}
}
I've got class 'Frame' which extends JFrame and separetad JPanels: MainMenu and SinglePanel
I am using CardLayout, but I've got problem when switching back to panels using buttonSingle and powrot buttons. So my question is how can I change/swap between cards using these buttons?
My Frame class:
public class Frame extends JFrame{
CardLayout cl = new CardLayout();
final MainMenu menuPanel = new MainMenu();
final SinglePanel singlePanel = new SinglePanel();
public Frame(){
setLayout(cl);
add(menuPanel,"menu");
add(singlePanel,"single");
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
setEnabled(true);
swapView("menu");
}
public void swapView(String view){
cl.show(getContentPane(),view);
}
}
my MainMenu class:
public class MainMenu extends JPanel{
public MainMenu(){
setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
add(Box.createVerticalGlue());
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
}
});
add(buttonSingle);
add(Box.createVerticalGlue());
JButton buttonMulti = new JButton("Multiplayer");
buttonMulti.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonMulti.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
add(buttonMulti);
add(Box.createVerticalGlue());
JButton buttonExit = new JButton("Wyjście");
buttonExit.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonExit.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
add(buttonExit);
add(Box.createVerticalGlue());
}
}
my SinglePanel class
public class SinglePanel extends JPanel{
SinglePanel(){
setLayout(new FlowLayout());
JButton powrot = new JButton("Wróć do menu");
powrot.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
}
});
add(powrot);
}
}
Main class:
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*MainMenu mM = new MainMenu();*/
Frame f = new Frame();
}
}
You need a reference to the CardLayout of the JFrame inside your panel classes. What you can do is pass the Frame to the JPanel classes as reference, then you can use the CardLayout of the Frame in those classes to show or next etc. Something like
public class MainMenu {
private CardLayout layout;
private Frame frame;
public MainMenu(final Frame frame) {
this.frame = frame;
this.layout = (CardLayout)frame.getLayout();
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
layout.show(frame, "single");
}
});
}
}
When you create the MainPanel, you need to pass Frame.this to it, referencing the current Frame
MainMenu menuPanel = new MainMenu(Frame.this);
EDIT
I just noticed your swapView method. So instead of use the CardLayout directly in the panel class, you can actually just call swapView in the actionPerformed
frame.swapView("single");
Or better yet, as to not expose the Frame class, you can have the Frame class implement an interface say SwapInterface that has a method swapView you need to override. And pass the SwapInterface to the panel classes. Something like
public interface SwapInterface {
public void swapView(String view);
}
public Frame extends JFrame implements SwapInterface {
MainMenu mainPanel = new MainMenu(Frame.this);
....
#Override
public void swapView(String view) {
cl.show(getContentPane(), view);
}
}
public class MainMenu extends JPanel {
private SwapInterface swap;
public MainMenu(SwapInterface swap) {
this.swap = swap;
...
public void actionPerfomed(ActionEvent e) {
swap.swapView("single");
}
}
}
Side Note
As HovercraftFullOfEels pointed out in his comment, you should make use of String contants for the String card values so there's no mistakes. Something like
private static final String SINGLE_CARD = "single";
Then in places where you use "single", use SINGLE_CARD instead
import java.awt.*;
public class TestButton {
private Frame f;
protected Button b;
public TestButton() {
f = new Frame("Test");
b = new Button("Press Me!");
b.setActionCommand("ButtonPressed");
}
public void launchFrame() {
b.addActionListener(new ButtonHandler());
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public static void main(String args[]) {
TestButton guiApp = new TestButton();
guiApp.launchFrame();
}
}
import java.awt.*;
import java.awt.event.*;
public class ButtonHandler extends TestButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==b)
{
System.out.println("Action occurred");
System.out.println("Button's command is: "
+ e.getActionCommand());
}
}
}
I'm trying to invoke a ActionEvent when the button b is pressed but not working with getSource.
You're misusing inheritance. The ButtonHandler class should not extend the TestButton class, since the b variable in the handler class refers to a completely different Button object from the one displayed. I suggest:
Use the Swing library, not the AWT library
You can get the JButton pressed from the ActionEvent's getSource() method and use it directly.
If you need a reference to the GUI in the handler, pass in a reference in the handler's constructor.
Don't misuse inheritance to solve problems that don't involve inheritance issues.
For example:
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class TestButton extends JPanel {
private JButton btn = new JButton(new ButtonAction("Press Me!", "ButtonPressed"));
public TestButton() {
add(btn);
}
private static void createAndShowGUI() {
TestButton testButton = new TestButton();
JFrame frame = new JFrame("TestButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testButton );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
#SuppressWarnings("serial")
class ButtonAction extends AbstractAction {
public ButtonAction(String name, String actionCommand) {
super(name);
putValue(ACTION_COMMAND_KEY, actionCommand);
}
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Button's actionCommand is: " + evt.getActionCommand());
}
}
That happens because you extends TestButton in your ButtonHandler, because of you have 2 different instances of your Button and thea are not equals.
To fix that you can remove extends TestButton and make ButtonHandler as inner class of TestButton
or you can compare action commands instead of Button's like next:
if(((Button)source).getActionCommand().equals("ButtonPressed"))
I think you need to remove extends TestButton as the 2 different instances of the buttons are not equal. You should go for a ButtonHandler as inner class or anonymous class to implement this.
Check this question:
Java Button Handler