I'm using NetBean's GUI builder. I've created a JPanel added it to a JFrame.
To the class and code that is generated by NetBeans for the Panel I have added an ID number to identify the panel.
I have a Handler class set up as an MouseListener for the Panel.
When I click on the panel (or any future component I may add), I would like to be able to access the ID number from that object.
I've tried using things like getSource() but I can not get to the ID.
Here is the code generated by NetBeans and the ID variable I have added to the class:
package Panels;
import swingtest2.Frame;
public class Panel1 extends javax.swing.JPanel {
int ID = 1;
public int getPanelID(){
return ID;
}
/**
* Creates new form Panel1
*/
public Panel1() {
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() {
setBackground(new java.awt.Color(51, 204, 0));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.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)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
Here is the code for the Handler:
package Handler;
import Panels.*;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import swingtest2.*;
public class Handler implements MouseListener{
int currentPageID;
public void createWindow(){
Frame frame = new Frame();
Panel1 panel1 = new Panel1();
panel1.addMouseListener(this);
frame.setContentPane(panel1);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
// get the ID from panel1 here
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
Any help is much appreciated.