0

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.

Buck Wheat
  • 41
  • 5
  • Call `e.getSource()` on the MouseEvent parameter in your MouseListener, and thereby get a reference to the component that was clicked. You will need to cast it to a Panel1 object to call its `getPanelID()` method, and do so with care, since you need to be absolutely sure that it is in fact a Panel1 object before casting. – Hovercraft Full Of Eels Oct 06 '17 at 23:36
  • I did not try casting it as a Panel1 object. I'm trying to avoid that as the Handler will need to discern which object was clicked among many of the same type of component. I was hoping to do this with the ID#s. The idea is that there will be Labels in a Panel and depending on which Label is clicked, a different Panel with labels will come up. I hope that makes sense. – Buck Wheat Oct 07 '17 at 01:14
  • Then you have other options including using the [putClientProperty](http://docs.oracle.com/javase/9/docs/api/javax/swing/JComponent.html#putClientProperty-java.lang.Object-java.lang.Object-) and it's related get method from JComponent. Other options: make all components that need to hold and return an id value implement the same interface. There's no magic here, just bread and butter Java. But for my money, I would use separate listeners for separate classes of components that do separate functionality to avoid "switchboard" listeners. Good luck. – Hovercraft Full Of Eels Oct 07 '17 at 01:20
  • That'd be a lotta listeners!:) I'm still fairly new to Jave so thanks for the insight. I'll give putClientProperty a shot and work by up to multiple listeners. Thanks again. – Buck Wheat Oct 07 '17 at 01:25
  • Not a new listener for each component, a new listener for each *class* of component. Consider a calculator example. You'd have a bunch of JButtons, and one ActionListener for all the number buttons and perhaps an ActionListener for the operation buttons. – Hovercraft Full Of Eels Oct 07 '17 at 01:26

0 Answers0