Issue
I'm having an issue with adding a custom JPanel to a JFrame in the Netbeans Swing Editor.
To rule out other issues, I made a brand new JFrame, and tried to drag my JPanel in (the JPanel is even in the same package). It popped up an error message:
Warning: Cannot Load component Class GUI.(nameofpanel) from project ...(path)... The class could not be found. Note the class must be compiled and must be part of the sources or dependencies of the project where the target GUI form belongs to.
I've successfully added JPanels to frames before using this method, but now it is preventing me from doing so. I get the same error in versions 7.1 and 7.3 of Netbeans.
I thought I might need to build the JPanel class file, but the option to do so is grayed out. I even tried a clean and build to no effect.
Here is my JFrame:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
/**
*
* @author chris
*/
public class MainFrame extends javax.swing.JFrame {
/**
* Creates new form MainFrame
*/
public MainFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
And my JPanel:
package GUI;
import java.awt.Image;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/*
* Represents one card graphically /**
*
* @author Student-HSLH133
*/
public class CardPanel extends javax.swing.JPanel {
private Image cardImage;
/**
* Creates new form CardPanel
*/
public CardPanel() {
initComponents();
//Set to face down card initially
//Hey look, some Lisp code, jk
try {
cardLabel.setIcon(imageToIcon(ImageIO.read(new File("images/gbCard52.gif"))));
}
catch (Exception e){
System.out.println("Failed to load the image.");
System.exit(-1);
}
}
public void setCard(Image img) {
cardLabel.setIcon(imageToIcon(img));
repaint();
}
// -------------- end of load_picture ---------------------------
private ImageIcon imageToIcon(Image img) {
return new ImageIcon(img);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
cardLabel = new javax.swing.JLabel();
setMaximumSize(new java.awt.Dimension(72, 102));
setMinimumSize(new java.awt.Dimension(72, 102));
setPreferredSize(new java.awt.Dimension(72, 102));
setLayout(new java.awt.BorderLayout());
cardLabel.setMaximumSize(new java.awt.Dimension(72, 102));
cardLabel.setMinimumSize(new java.awt.Dimension(72, 102));
cardLabel.setPreferredSize(new java.awt.Dimension(72, 102));
add(cardLabel, java.awt.BorderLayout.CENTER);
cardLabel.getAccessibleContext().setAccessibleName("card");
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel cardLabel;
// End of variables declaration
}
I am totally lost. What is going on?
Edit: I somehow got it to compile the JPanel, but I still get the same error. Thinking it might be something with the JPanel itself, I made a new custom JPanel, compiled it, and tried to add it, and it worked.
Edit 2: I've tried commenting out the custom stuff I've done in the JPanel class, to no avail.
Solution
I never did pin this down, but I think it has something to do with faulty file tracking or something that got corrupted regarding my JPanel class file.
Moving the methods and variables to a new JPanel class seems to work fine, so that is the workaround.
Answered By - Chris Chambers
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)