Question

Im trying to add a NEW card to an existing JPanel(cardLayout) with the click of JButton and then goto that new card but i get Null exception as the new card is not registering.

Ive tried searching google but cant find and example and ive tried to make 'save' the layout using

.validate();
.refresh();
.repaint();

but nothing works, is this possible dynamically? and if so does anyone know of any example?

code below, jPanelSliding1.nextSlidPanel simply moves to the selected JPanel in CardLayout. New Panel created here is causing a null error as panel is not found. thanks:)

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package partnerships;

import components.ColorScheme;
import components.Utilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JSlidingPanel extends javax.swing.JPanel implements PropertyChangeListener {

    public PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    private ColorScheme scheme;

    public JSlidingPanel() {
        initComponents();
        pcs.addPropertyChangeListener(this);
        scheme = new ColorScheme();
        Object[][] object = {
            {pcs, "Offered", "BT details", scheme.green()},
            {pcs, "Sold", "BT details", scheme.pink()},
            {pcs, "Other", "BT details", scheme.purple()}
        };

        jPanelSliding1.add(makePanels(object));
    }

    private void setupGUI() {
    }

    private JPanel makePanels(Object[][] objects) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, objects.length));
        for (Object[] properties : objects) {
            panel.add(new ObjectPanel((PropertyChangeSupport) properties[0], properties[1].toString(), properties[2].toString(), (Color) properties[3]));
        }
        return panel;
    }

    private void newPanel(String str) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(new JButton());
        jPanelSliding1.add(panel);

        jPanelSliding1.refresh();
        jPanelSliding1.validate();
        jPanelSliding1.repaint();

        validate();
        repaint();
        Utilities.getTopFrame().pack();
        jPanelSliding1.NextSlidPanel(10, panel);
        jPanelSliding1.refresh();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (this.isVisible() == true) {
//            makePanels();
            newPanel(evt.getPropertyName());
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                JPanel panel = new JPanel();
                JSlidingPanel app = new JSlidingPanel();

                panel.add(app);
                frame.add(panel);
                frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
                frame.pack();
            }
        });
    }

    /**
     * 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() {

        jPanelSliding1 = new apps.JPanelSliding();

        setPreferredSize(new java.awt.Dimension(900, 600));
        setLayout(new java.awt.BorderLayout());

        jPanelSliding1.setLayout(new java.awt.CardLayout());
        add(jPanelSliding1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>                       
    // Variables declaration - do not modify                    
    private apps.JPanelSliding jPanelSliding1;
    // End of variables declaration                  
}
Was it helpful?

Solution 3

Found an answer to my issue, for some reason i had to name the panel after creating it otherwise it is never found. Thanks for the help.

     JPanel panel = new JPanel();
**panel.setName("abc");**
        panel.setLayout(new BorderLayout());

OTHER TIPS

if you are just dealing with changing card of your window on some event, following may help.

Oracle Tutorial example

I am not sure if this could work but:

What I see is that you are trying to "refresh/repaint/validate" the jPanelSliding value. Instead, what you should modify is the panel you are trying to click on. The jPanelSliding may be updated, but the panel (declared on main) is not being actualised.

So, why don't you try making a "validate" call to the panel declared in the main?

Can you try that and tell us if it works?

I was dealing with the same issue as well.
After some testing, retesting and a lot of failing my pseudo-algorithm is this:

parentPanel : contains the panel we want to remove
childPanel : panel we want to switch
parentPanelLayout : the layout of parentPanel
editParentLayout() : builds parentPanel with different childPanel and NEW parentPanelLayout every time

parentPanel.remove(childPanel); 
editParentLayout();
parentPanel.revalidate();
parentPanel.repaint();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top