tl;dr I just started learning java and swing and I have this problem where inside the main class if I try to call any custom method defined in my custom class which extends JPanel, i get an error which says "cannot find symbol", it's as if that method isn't even declared. I tripled checked the custom class object looks properly initiated and the method is set as public. Any idea why this happens?
At first I was using Intellij Idea but it's gui designer was so frustrating to use that i thought it was the IDE's fault but even after switching to NetBeans and rewriting code from scratch I get the same error.
I wanted to write a class called Drawing which extends JPanel that could draw my custom Circuit class objects. Basically what i wanted to do for the app is that selecting New from the File menu would create and draw that Circuit object. For that I wanted that pressing the New button would call setCircuit() method defined in the Drawing class which initializes the circuit and then the paintComponent() would draw the circuit.
Code below:
import circuit.*;
import java.awt.Graphics;
public class Drawing extends javax.swing.JPanel {
private Circuit circuit = null;
public Drawing(){
}
public void setCircuit(Circuit circuit) {
this.circuit = circuit;
}
@Override
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
if(circuit == null){
graphics.drawString("Create a new circuit.", 10, 20);
} else{
// draws the circuit
}
}
}
import circuit.*;
public class GUI extends javax.swing.JFrame {
private Circuit circuit = null;
public GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
drawing = new Drawing();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
newMenuItem = new javax.swing.JMenuItem();
exitMentuItem = new javax.swing.JMenuItem();
exitMenu = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("My app");
setName("mainFrame"); // NOI18N
javax.swing.GroupLayout drawingLayout = new javax.swing.GroupLayout(drawing);
drawing.setLayout(drawingLayout);
drawingLayout.setHorizontalGroup(
drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
drawingLayout.setVerticalGroup(
drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
fileMenu.setText("File");
newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
newMenuItem.setText("New");
newMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newMenuItemActionPerformed(evt);
}
});
fileMenu.add(newMenuItem);
exitMentuItem.setText("Exit");
exitMentuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMentuItemActionPerformed(evt);
}
});
fileMenu.add(exitMentuItem);
menuBar.add(fileMenu);
exitMenu.setText("Edit");
menuBar.add(exitMenu);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
circuit = new Circuit();
drawing.setCircuit(circuit); // this is where I get the error:
/*
cannot find symbol
symbol: setCircuit(Circuit)
location: variable drawing of type JPanel
*/
}
private void exitMentuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel drawing;
private javax.swing.JMenuItem exitMentuItem;
private javax.swing.JMenu exitMenu;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem newMenuItem;
// End of variables declaration
}