Osiris93
Osiris93

Reputation: 316

Cannot change between jFrames in hard coded java applications?

When I create a Swing GUI class, I can change from one JFrame to the other via a jButton using the following code:

private void btnChangeClassActionPerformed(java.awt.event.ActionEvent evt) {                                               

        ClassA ca = new ClassA();
        ca.setVisible(true);
        this.dispose();
    }

But when I create a blank java class (hard coding), this code does not work as the .setVisible(true); does not work. In fact when I press ctrl + space to bring up suggestions, nothing is listed after ca.. This only occurs when I try to change jFrames with hard coded applications.

The stacktrace produces this error:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException:
Uncompilable source code - Erroneous sym type: classPackage.ClassB.setVisible

How can I correct this error?

Upvotes: 1

Views: 95

Answers (1)

Christian Ammann
Christian Ammann

Reputation: 938

I am not sure what's your problem, but what you a trying to do should work. Here is my example for ClassA.java

public class ClassA extends JFrame{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("helo moto");
        ClassA a = new ClassA();
    }

    ClassA(){
        super();
        setSize(200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton("ClassB");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new ClassB();
                ClassA.this.dispose();
            }
        });
        Container container = getContentPane();
        container.add(button);
        setVisible(true);
    }
}

And here is ClassB.java:

public class ClassB extends JFrame{
    ClassB(){
        super();
        setSize(200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton("ClassA");

        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new ClassA();
                ClassB.this.dispose();
            }
        });
        Container container = getContentPane();
        container.add(button);
        setVisible(true);
    }   
}

Upvotes: 1

Related Questions