0

I've a problem with a JPanel. It's a CardLayout and two sub-panel that change. I need to do a button which switch from subpanel 1 to 2 and display selected Product.

ISSUE: When I select a product card, JPanel changes but it's blank. If I step over the window with the mouse , the buttons appear, but not the JLabels. And If I resize the windows, it's empty again. What is the problem?

EDIT: Here the code that return same error.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.*;

import javax.swing.*;

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    // private String title;
    // private Dimension d;

    public MainFrame(String title, Dimension d) {

        // LoginPanel template = new LoginPanel(this);
        // RegisterPanel template = new RegisterPanel(this);
        CustomerPanel template = new CustomerPanel(this);
        this.setTitle(title);
        this.setSize(d);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.getContentPane().add(template);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new MainFrame("Fubars", new Dimension(800, 650));
        });
    }
}

@SuppressWarnings("serial")
class CustomerPanel extends JPanel {

    MainFrame mf;

    JPanel one, two;
    JPanel panel;

    public CustomerPanel(MainFrame mf) {
        this.mf = mf;
        mf.getContentPane().setLayout(new CardLayout(0, 0));

        JPanel container = new JPanel();
        mf.getContentPane().add(container, "name_36743208542992");
        container.setLayout(new BorderLayout(0, 0));

        JPanel back = new JPanel();
        container.add(back, BorderLayout.NORTH);
        back.setLayout(new FlowLayout(FlowLayout.LEFT));

        JButton btnControl = new JButton("<");
        back.add(btnControl);

        panel = new JPanel();
        container.add(panel, BorderLayout.CENTER);
        CardLayout cl = new CardLayout(0, 0);
        panel.setLayout(cl);

        one = new OnePanel(this.mf);
        two = new JPanel();
        panel.add(one, "1");
        panel.add(two, "2");

        btnControl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                two = new LoginPanel(mf);
                cl.next(panel);
            }
        });
    }
}

@SuppressWarnings("serial")
class LoginPanel extends JPanel {

    // private MainFrame mf;
    private JTextField textField;
    private JPasswordField passwordField;

    public LoginPanel(MainFrame mf) {

        // this.mf = mf;
        mf.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        mf.getContentPane().add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 100, 55, 72, 171, 0 };
        gbl_panel.rowHeights = new int[] { 69, 22, 22, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        JLabel lblNewLabel = new JLabel("Email");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.gridx = 1;
        gbc_lblNewLabel.gridy = 1;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.anchor = GridBagConstraints.NORTH;
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.gridx = 3;
        gbc_textField.gridy = 1;
        panel.add(textField, gbc_textField);
        textField.setColumns(15);

        JLabel lblNewLabel_1 = new JLabel("Password");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
        gbc_lblNewLabel_1.gridx = 1;
        gbc_lblNewLabel_1.gridy = 2;
        panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

        passwordField = new JPasswordField();
        passwordField.setColumns(15);
        GridBagConstraints gbc_passwordField = new GridBagConstraints();
        gbc_passwordField.anchor = GridBagConstraints.NORTH;
        gbc_passwordField.gridx = 3;
        gbc_passwordField.gridy = 2;
        panel.add(passwordField, gbc_passwordField);

        JPanel panel_1 = new JPanel();
        mf.getContentPane().add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));

        JButton btnNewButton = new JButton("LOGIN");
        panel_1.add(btnNewButton);
        btnNewButton.setActionCommand("login");

        JButton btnRegistration = new JButton("REGISTER");
        panel_1.add(btnRegistration);
        btnRegistration.setActionCommand("registration");

    }

}

@SuppressWarnings("serial")
class OnePanel extends JPanel {

    // private MainFrame mf;

    public OnePanel(MainFrame mf) {
        // this.mf = mf;

        mf.getContentPane().setLayout(new CardLayout(0, 0));

        JPanel container = new JPanel();
        mf.getContentPane().add(container, "name_36743208542992");
        container.setLayout(new BorderLayout(0, 0));

        JPanel image = new JPanel();
        container.add(image, BorderLayout.CENTER);

        JButton btnImageBack = new JButton("<");
        image.add(btnImageBack);

        JLabel imageContainer = new JLabel("Images");
        image.add(imageContainer);
        imageContainer.setBounds(new Rectangle(100, 100, 100, 100));
        imageContainer.setHorizontalTextPosition(SwingConstants.CENTER);
        imageContainer.setHorizontalAlignment(SwingConstants.CENTER);
        imageContainer.setAlignmentX(Component.CENTER_ALIGNMENT);
        imageContainer.setIconTextGap(3);
        imageContainer.setIcon(null);

        JButton btnImageForward = new JButton(">");
        image.add(btnImageForward);
        btnImageForward.setAlignmentY(Component.BOTTOM_ALIGNMENT);
        btnImageForward.setAlignmentX(Component.CENTER_ALIGNMENT);

        JPanel info = new JPanel();
        container.add(info, BorderLayout.EAST);
        GridBagLayout gbl_info = new GridBagLayout();
        gbl_info.columnWidths = new int[] { 0, 0, 63, 0, 0, 0 };
        gbl_info.rowHeights = new int[] { 0, 0, 25, 0, 0, 0, 0 };
        gbl_info.columnWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
        gbl_info.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
        info.setLayout(gbl_info);

        JLabel lblTitle = new JLabel("Title");
        GridBagConstraints gbc_lblTitle = new GridBagConstraints();
        gbc_lblTitle.anchor = GridBagConstraints.NORTHWEST;
        gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
        gbc_lblTitle.gridx = 2;
        gbc_lblTitle.gridy = 1;
        info.add(lblTitle, gbc_lblTitle);

        JLabel lblDescription = new JLabel("Description");
        GridBagConstraints gbc_lblDescription = new GridBagConstraints();
        gbc_lblDescription.anchor = GridBagConstraints.WEST;
        gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
        gbc_lblDescription.gridx = 2;
        gbc_lblDescription.gridy = 2;
        info.add(lblDescription, gbc_lblDescription);

        JLabel lblPrice = new JLabel("Price");
        GridBagConstraints gbc_lblPrice = new GridBagConstraints();
        gbc_lblPrice.anchor = GridBagConstraints.WEST;
        gbc_lblPrice.insets = new Insets(0, 0, 5, 5);
        gbc_lblPrice.gridx = 2;
        gbc_lblPrice.gridy = 3;
        info.add(lblPrice, gbc_lblPrice);
        lblPrice.setToolTipText("Price");

        JButton btnAddCart = new JButton("Add to Cart");
        GridBagConstraints gbc_btnAddCart = new GridBagConstraints();
        gbc_btnAddCart.insets = new Insets(0, 0, 5, 5);
        gbc_btnAddCart.anchor = GridBagConstraints.WEST;
        gbc_btnAddCart.gridx = 2;
        gbc_btnAddCart.gridy = 4;
        info.add(btnAddCart, gbc_btnAddCart);

    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Stek
  • 57
  • 1
  • 8
  • 1
    ISSUE: No [mcve] code post for us to test directly, making finding a hidden bug all the more difficult. Please create this type of program and post its code in with your question. – Hovercraft Full Of Eels Sep 01 '18 at 14:14
  • 1
    Side note: When adding components to a CardLayout-using container, I pass the component first and then the CardLayout String constant second. I'm not sure if this makes a difference, and perhaps it doesn't, but just note that this is how it is usually done. – Hovercraft Full Of Eels Sep 01 '18 at 14:18
  • @HovercraftFullOfEels I've updated! I hope it's enough! There's not difference between(name,component) or (component,name) – Stek Sep 01 '18 at 15:23
  • 1
    Thank you for the attempt, but so far, no joy for me as there still is too much unrelated code, and also (which might seem paradoxical) not enough code. You have references to files, images, databases, and other classes (ProductEntity, BasketEntity, Utility, ProductListener, ... etc) that we have no access to. Please have another look at the [MCVE](https://stackoverflow.com/help/mcve) to see the type of code that helps us best -- one that is very small, that we can compile and run, that shows the problem directly for us. – Hovercraft Full Of Eels Sep 01 '18 at 17:18
  • ok, I understand, but I think it's not problem of other classes. It's a issue about displaying component. It's a big project and if i should upload the minimum classes for compiling, it's a massacre. Entity classes are Model of database, utility class has a simple method of resize image, ecc.. Thanks @HovercraftFullOfEels! – Stek Sep 01 '18 at 17:49
  • 1
    That may be your problem in a nutshell -- if your classes are too complex and too tightly coupled, then debugging can be much more difficult for both you (and by extension, for us). If you can create a valid MCVE, you may also end up de-coupling some of these connections, allowing you to find your bug yourself. But as for "it's not a problem of other classes..." until you know exactly where the problem is, you don't want to make assumptions. As for us, it is a problem of other classes because we simply cannot compile nor run this code. – Hovercraft Full Of Eels Sep 01 '18 at 17:52
  • 1
    *"It's a big project and if i should upload the minimum classes for compiling, it's a massacre."* Start from the other direction, with [this example](http://stackoverflow.com/a/5786005/418556) then add a line at a time (towards making it the same as your code) until it breaks. That last code line change will be the problem. Good luck with it! – Andrew Thompson Sep 02 '18 at 00:46
  • 1
    BTW - this.. `this.setSize(d); this.setVisible(true); .. this.getContentPane().add(template);` should instead be `this.getContentPane().add(template); this.pack(); this.setVisible(true);`.. Always add all the components before calling pack, which in turn leads to setting the GUI visible. **Don't** call `setSize(..)` - it's just a guess how large the GUI should be. – Andrew Thompson Sep 02 '18 at 00:50
  • 1
    I would start with the example from the Swing tutorial on [How to Use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Unlike examples in the forum, the tutorial gives an explanation on what the code is doing. Also, you are still using the "obsolete" add(string, component) method. It was suggested to use `add(component, constraint)`. We don't know if it will make a difference but we have been using the proper method since JDK1.1 so we know it works. – camickr Sep 02 '18 at 00:55
  • @AndrewThompson Why should I use pack() method? If I do this, the jpanel (LoginPanel) inside MainFrame doesn't show JLable and JTextField, but only JButton. I don't why! – Stek Sep 03 '18 at 11:54
  • @camickr Sorry, I don't see where talks about `add(component,constraint)`, but only `add(component,name)`. Where is? – Stek Sep 03 '18 at 11:57
  • 1
    *"If I do this, the .. (GUI breaks)"* I'm unwilling to investigate further until/unless an [edit] is made to the question to include the MCVE (suggested by @HovercraftFullOfEels in the first comment). – Andrew Thompson Sep 03 '18 at 12:17
  • @Stek, You are adding the "card panels" to a parent panel that is using a CardLayout. A JPanel is a Container. Read the API for the Container class. You will fine the `add(String, Component)` which is obsolete and you will find the `add(Component, Object)` method which is the way you should be specifying the constraint when you add a component to the panel. – camickr Sep 03 '18 at 13:55
  • @HovercraftFullOfEels I edited the post, I hope it'll be useful.✌ – Stek Sep 04 '18 at 11:35

0 Answers0