1

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?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Osiris93
  • 296
  • 2
  • 18
  • I am not sure what you mean by "hard coded". could you provide more source code? – Christian Ammann Jul 07 '16 at 17:52
  • What I mean by hard coded is that I am not using 'drag and drop'. Everything is coded from scratch. – Osiris93 Jul 07 '16 at 17:56
  • See [The use of multiple JFrames, good / bad practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) and for better help sooner, please post a [mcve] not just a code snippet, so we can copy-paste it and see the same error as you – Frakcool Jul 07 '16 at 18:10

1 Answers1

1

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);
    }   
}
Christian Ammann
  • 888
  • 8
  • 19