Hi guys,

I am trying to add a new component to my window by clicking on a clickable jLabel. Better said, with mousevent. Unfortunately, it does not work, at least for what i tried with. Can you help me here?

My code:

package test_1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;


        public class MainWindow extends javax.swing.JFrame implements ActionListener, MouseListener{

            /**
             * Creates new form MainWindow
             */
            public MainWindow() {
                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() {

                jFrame1 = new javax.swing.JFrame();
                jFrame1.getContentPane().setBackground(Color.YELLOW);

                jLabels= new ArrayList<JLabel>();

                jPanel1 = new javax.swing.JPanel();
                jPanel1.setOpaque(true);
                jPanel1.setBackground(Color.white);
                jPanel1.setBorder(
                        BorderFactory.createEmptyBorder(5, 5, 5, 5));
                    jPanel1.setLayout(new BorderLayout(5, 5));
                    jPanel1.setBorder(
                            BorderFactory.createTitledBorder(
                            BorderFactory.createEtchedBorder(
                                    EtchedBorder.RAISED, Color.GRAY
                                    , Color.DARK_GRAY), "Hauptmenü"));







                jButton1 = new javax.swing.JButton("Hello!");


                //border = LineBorder.createGrayLineBorder();

                jLabel3 = new javax.swing.JLabel("Check-In");
                jLabel3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                jLabel3.addMouseListener((MouseListener) this);
                Image img= new ImageIcon(this.getClass().getResource("/check-In.jpg")).getImage();
                jLabel3.setIcon(new ImageIcon(img));
                //jLabel3.setBorder(border);


                jLabel4 = new javax.swing.JLabel("Check-Out");
                jLabel4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                jLabel4.addMouseListener(this);
                Image img2= new ImageIcon(this.getClass().getResource("/check-Out.jpg")).getImage();
                jLabel4.setIcon(new ImageIcon(img2));
                //jLabel4.setBorder(border);

                jLabel5 = new javax.swing.JLabel();
                jLabel5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                jLabel5.addMouseListener(this);
                Image img3= new ImageIcon(this.getClass().getResource("/Alle Gaeste.jpg")).getImage();
                jLabel5.setIcon(new ImageIcon(img3));
                jLabel5.setBorder(border);


                jLabel6 = new javax.swing.JLabel("Zahlungsstand");
                jLabel6.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                jLabel6.addMouseListener(this);
                Image img4= new ImageIcon(this.getClass().getResource("/Zahlungsstand_v1.jpg")).getImage();
                jLabel6.setIcon(new ImageIcon(img4));
                jLabel6.setBorder(border);


                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



                javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
                jFrame1.getContentPane().setLayout(jFrame1Layout);
                jFrame1Layout.setHorizontalGroup(
                    jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 400, Short.MAX_VALUE)
                );
                jFrame1Layout.setVerticalGroup(
                    jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 300, Short.MAX_VALUE)
                );

                 jLabel3.setText("");

                jLabel4.setText("");

                jLabel5.setText("");

                jLabel6.setText("");


                javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                jPanel1.setLayout(jPanel1Layout);
                jPanel1Layout.setHorizontalGroup(
                    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                            //.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap(40, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(32, 32, 32 )
                            //.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(50, Short.MAX_VALUE)))
                );
                jPanel1Layout.setVerticalGroup(
                    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(353, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        //.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()

                            //.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(640, Short.MAX_VALUE)))
                );

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 800, Short.MAX_VALUE))
                );
                layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 127, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                );

                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(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                    java.util.logging.Logger.getLogger(MainWindow.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 MainWindow().setVisible(true);
                    }
                });
            }

            // Variables declaration - do not modify  
            private javax.swing.JFrame jFrame1;
            private javax.swing.JLabel jLabel3;
            private javax.swing.JLabel jLabel4;
            private javax.swing.JLabel jLabel5;
            private javax.swing.JLabel jLabel6;
            private javax.swing.JPanel jPanel1;
            private javax.swing.JPanel jPanel2;
            private javax.swing.JButton jButton1;
            private Border border ;

            ArrayList <JLabel> jLabels;


            // End of variables declaration                   
            @Override
            public void actionPerformed(ActionEvent arg0) {


            }
            public void SetBorders (JLabel label)
            {


                border = LineBorder.createGrayLineBorder();
                label.setBorder(border);


                jLabels.add(label);

                for(JLabel l: jLabels)
                {
                    System.out.println(l.toString());
                }

            }

            @Override
            public void mouseClicked(MouseEvent arg0) {

                JLabel b2= (JLabel)arg0.getSource();

                if(jLabel3.toString().equals(b2.toString()))
                {
                    jPanel2=new javax.swing.JPanel();
                    jPanel2.setOpaque(true);
                    jPanel2.setBackground(Color.white);
                    jPanel2.setBorder(
                            BorderFactory.createEmptyBorder(5, 5, 5, 5));
                        jPanel2.setLayout(new BorderLayout(5, 5));
                        jPanel2.setBorder(
                                BorderFactory.createTitledBorder(
                                BorderFactory.createEtchedBorder(
                                        EtchedBorder.RAISED, Color.GRAY
                                        , Color.DARK_GRAY), "Nebenmenü"));
                    this.add(jPanel2);

                    System.out.println("Yes Check-In");

                }   
                if(jLabel4.toString().equals(b2.toString()))
                {
                    System.out.println("Yes Check-Out");
                    this.SetBorders(jLabel4);
                }

                if(jLabel5.toString().equals(b2.toString()))
                {
                    System.out.println("Yes Alle guests");
                    this.SetBorders(jLabel5);
                }
                if(jLabel6.toString().equals(b2.toString()))
                {
                    System.out.println("Yes Zahlungsstand");
                }
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }
        }

"didn't work"? Means whay exactly?
Error message? Listener not called? Listener called but no panel visible? Panel visible but looks wrong?

Come on now, give us some info to work with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.