balvinder dhillon balvinder dhillon - 1 year ago 47
Java Question

Hi i have created a private class "PathakP" but i am getting PathakP cannot be resolved to a type

Ok i tried to create a dialog box with Jbutton but when i am adding actionListener to it and passing the class to button which i have created to implements ActionListener i am getting "PathakP(Class Name) cannot be resolved to a type"

the code i have used is

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class GUI1 extends JFrame {

private JTextField J;
private Font pf,bf,itf,bif;
private JRadioButton pb,bb,ib,bib;
private ButtonGroup B;
private JButton ab;

public GUI1(){
super("To check the Font styles" );
setLayout(new FlowLayout());

J=new JTextField("This is the Text who's Font will be Changed pahtak is with me ",40);
pb=new JRadioButton("Plain Button",true);
bb=new JRadioButton("Bold Button",false);
bib=new JRadioButton("Bold & Italic Button",false);
ib=new JRadioButton("Italic Button",false);
ab=new JButton("PathakButton");


B=new ButtonGroup();

pf=new Font("Serif",Font.PLAIN,15);
bf=new Font("Serif",Font.BOLD,15);
itf=new Font("Serif",Font.ITALIC,15);
bif=new Font("Serif",Font.BOLD+Font.ITALIC,16);

pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
bib.addItemListener(new HandlerClass(bif));
ib.addItemListener(new HandlerClass(itf));

ab.addActionListener(new PathakP());
private class HandlerClass implements ItemListener{
private Font font;
public HandlerClass(Font f){

public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub

private class PathakP implements ActionListener{

public void actionPerformed(ActionEvent ae) {

JOptionPane.showMessageDialog(null, "This is just JOptionPane example");




Main Class

import javax.swing.*;
public class Apples {

public static void main(String[] args) {
GUI1 G=new GUI1();



i don't believe there is any error in the main class
i can troubleshoot this error by just creating another class outside but i want to know why it is not taking the class i have created and show unused in it

Answer Source

Your PathakP is written inside of HandlerClass. You have two solutions from there (after having corrected your bracket problem)

  • Either write it inside of GUI instead and since you're calling it from the constructor, it will be binded to this instance of GUI

  • Or, if you want to keep it within HandlerClass, you need to bind it to an instance of HandlerClass : ab.addActionListener(new HandlerClass().new PathakP())