0

I'm trying to implement a mouse click event to Jlabel using card layout. What I want is the label menu should be left side when I click on label, another panel should be displayed on right side how to do this I don't want to use buttons here.

Presently I'm getting the exception like this:

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to        layout: constraint must be a string
    at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)
    at java.awt.Container.addImpl(Container.java:1068)
    at java.awt.Container.add(Container.java:352)
    at Remainder.<init>(Remainder.java:44)
    at Remainder.main(Remainder.java:110)

This is my code:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Remainder implements MouseListener{
    JPanel panelchange1 = new JPanel();
    JPanel panelchange2 = new JPanel();
    CardLayout cardlayout = new CardLayout();
    JPanel p2 = new JPanel(new CardLayout());
    JLabel personal = new JLabel("Personal");
    JLabel personal2 = new JLabel("panel1");
    JLabel personal3 = new JLabel("panel2");
    JLabel d1 = new JLabel("Dairy");

    public  Remainder() {
        JSeparator sp1 = new JSeparator(JSeparator.VERTICAL);
        JFrame fm = new JFrame();
        //fm.addMouseListener(this);
        fm.getContentPane();
        //fm.setLayout(new BoxLayout());
        fm.setSize(880, 320);
        //fm.setResizable(false);
        fm.setVisible(true);
        fm.setLocationRelativeTo(null);

        JPanel p1 = new JPanel(new BorderLayout());
        fm.add(p1,BorderLayout.WEST);

        p2.setPreferredSize(new Dimension(850,300));
        fm.add(p2,BorderLayout.CENTER);
        p2.add(panelchange1);
        p2.add(panelchange2);
        p1.add(personal);

        personal.addMouseListener(this);
        p1.add(d1);

        d1.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {
        if (e.getSource()==d1){
            cardlayout.next(p2);
            //JOptionPane.showMessageDialog(null, "yeah it works now");
        }
        else if(e.getSource()==personal){
            panelchange2.add(personal2);
           //JOptionPane.showMessageDialog(null, "yeah it works too");
        }
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public static void main(String args[]) {
        Remainder obj1 = new Remainder();
        //obj1.pane();
    }
}

How to do this, I'm getting the blank frame displayed right now?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
RaviRokz
  • 1
  • 1

3 Answers3

3

Firstly

You never assign cardlayout to anything

CardLayout cardlayout = new CardLayout();
JPanel p2 = new JPanel(new CardLayout());

This will cause an error when you try and switch cards. Instead, apply the instance of the CardLayout you created to the panel...

CardLayout cardlayout = new CardLayout();
JPanel p2 = new JPanel(cardlayout);

Secondly

CardLayout requires you to supply a name for each card...

Change

p2.add(panelchange1);
p2.add(panelchange2);

To something like...

p2.add(panelchange1, "panel1");
p2.add(panelchange2, "panel2");

Thirdly

Take a look at

For more details...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Read the section from the Swing tutorial on How to Use a Card Layout.

You want panels in the left/right and the tutorial uses panels in the top/bottom, but the concept is the same.

i don't want to use buttons here.

You can use a JButton and use the setBorderPainted(false) method. Then the button will look like a label, but you can still use an ActionListener to handle the event instead of using a MouseListener.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

== can only compare primitives. The Object class ─ thus, all non-primitive objects, as well ─ has a function .equals() that compares two objects. So, in your actionPerformed methods, you'll want to look up the triggering object using e.getSource().equals(comparedObject).