2

I'm trying to call the method cardlayout.show to class below.

protected void makelabeltxt(final String text, GridBagLayout gridbag, GridBagConstraints c, int width, int height, final String panel) {
    final JLabel label = new JLabel(text);              
    Dimension d = new Dimension(width, height);
    gridbag.setConstraints(label, c);
    label.setFont(new Font("Sans Serif", 0, 11));
    label.setPreferredSize(d);


    label.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {                
            System.out.println(panel);
            fp.showPanel(panel);
            System.out.println("mouse clicked "+text);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            label.setText("  " + text);
            System.out.println("mouse pressed "+text);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            label.setText(" " + text);
            System.out.println("mouse release "+text);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            label.setText(" " + text);
            System.out.println("mouse entered "+text);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            label.setText(text);

        }
    });
    add(label);
}

In another class I've created method like this, this is the method i want to call. I want to swap jpanel, but in another jpanel: the method below is working but when cardlayout show script, it's nothing happen, the println script is fine

public void showPanel(String panel){
    System.out.println("panel : "+panel);       
    cl.show(mainInPanel, panel);
}

this is how i add JPanel to JPanel using cardlayout

public void initPanelFrm(JPanel panel, String gambar) {             
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setLayout(gridbag);
        panel.setPreferredSize(new Dimension(810, 280));
        panel.setBackground(Color.white);
        makelabelimg("com/reporting/image/"+gambar+".png", 444, 23, panel);     
        panel.setVisible(false);
        mainInPanel.add(panel, gambar); 
    }

    public void initInPanel(){
        rekappenjualanpsc = new JPanel();
        initPanelFrm(rekappenjualanpsc, "rekappenjualanpsc");
        rekapbastudenganmaskapai = new JPanel();
        initPanelFrm(rekapbastudenganmaskapai, "rekapbastudenganmaskapai");
        rekapitulasisetoranmaskapai = new JPanel();
        initPanelFrm(rekapitulasisetoranmaskapai, "rekapitulasisetoranmaskapai");
        rekappenjualanpscpermaskapai = new JPanel();
        initPanelFrm(rekappenjualanpscpermaskapai, "rekappenjualanpscpermaskapai");
        rekappungutanlangsungpsc = new JPanel();
        initPanelFrm(rekappungutanlangsungpsc, "rekappungutanlangsungpsc");
        laporandetailpetugas = new JPanel();
        initPanelFrm(laporandetailpetugas, "laporandetailpetugas");
        detailpembayaranpenumpang = new JPanel();
        initPanelFrm(detailpembayaranpenumpang, "detailpembayaranpenumpang");

    }

Yusuf1494
  • 252
  • 1
  • 5
  • 16
  • 1
    You should post the relevant code where you create the CardLayout object and where you add the panels to it. – Rempelos Oct 16 '12 at 11:02
  • Unrelated problem: don't use [`setPreferredSize`](http://stackoverflow.com/q/7229226/261156). – Catalina Island Oct 16 '12 at 13:01
  • Here's a related [example](http://stackoverflow.com/a/5655843/230513). – trashgod Oct 16 '12 at 13:40
  • i know the problem now, i've instace twice the class, so the first class is under the second class, thanks anyway to all who respon my question, i hope this post is help – Yusuf1494 Oct 17 '12 at 07:02

0 Answers0