Mcolo Mcolo - 2 months ago 11
Java Question

How to add funtionallity to a Jbutton,located in superclass from a subclass?

I am trying to add functionality to a Jbutton using subclass butt Jbutton is actually declared and defined in super class.
Here is my code:

JButton zz=new JButton(ss);
zz.setBounds(470,70,35,35);
zz.setBorder(oo);
zz.setBackground(new Color(0,170,120));
l.add(zz);


This is my Jbutton located in super class named as realestate in my project.I have created subclass in which i am trying to add action listener for this button.
Here is my subclass:

public class assan extends RealEstate{
zz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent u)
{
System.out.println("kk");
}
});}


butt i run this program i got different types of errors such as:
Package 'zz' does not exist
etc.
This is my full code:

package realestate;

import java.awt.Color;
import javax.swing.*;

public class Realestate extends JFrame {
Realestate()
{
JLabel l=new JLabel(new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\file (2).jpg"));
l.setBounds(100,50,300,250);
add(l);
ImageIcon ss=new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\1470672145_Help_mark_query_question_support_talk.png");
public JButton zz=new JButton(ss);
zz.setBounds(470,70,35,35);
zz.setBackground(new Color(0,170,120));
l.add(zz);
}
public class assan extends Realestate{
zz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o)
{
System.out.println("Cena");
}
});
}
public static void main(String[] args) {
RealEstate v=new RealEstate();
v.setUndecorated(true);
v.setVisible(true);
v.setBounds(350,200,600,350);
v.setForeground(Color.WHITE);
assan n=new assan();
}

}


what am i doing wrong?

Answer
  • Put the following class in the file RealState.java:

    public class RealEstate extends JFrame {
    
        protected JButton zz = null;
    
        RealEstate() {
            JLabel l = new JLabel("Label");
            l.setBounds(100, 50, 300, 250);
            add(l);
    
            // ImageIcon ss = new ImageIcon("path_to_image_file");
            zz = new JButton("MyButton");
            zz.setBounds(470, 70, 35, 35);
            zz.setBackground(new Color(0, 170, 120));
            add(zz); 
        }
    }
    
  • And put the subclass in its own file too: Assan.java

    public class Assan extends RealEstate {
    
        public Assan() {
            zz.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent o)   {
                   System.out.println("Cena");
               }
            });
        }
    
    
        public static void main(String[] args) {
            Assan v = new Assan();
            v.setUndecorated(true);
            v.setVisible(true);
            v.setBounds(350, 200, 600, 350);
            v.setForeground(Color.WHITE);
        }
    }
    

Note that I have made two changes:

  • make the variable you want to modify in the subclass protected
  • move the main method to the subclass if you want the event handler to be executed.
  • And lastly, you have to follow some tutorials, for example
    1. Here you will learn about inheritance
    2. Java Swing tutorials.