3

So let's start with some background this is for a first year collage project. the GUI is built so one class, the interface-handler calls all the JPanels and puts them in the right place. This is for structure and so it's easy to maintain.

the problem is the panel holding the menu does't show it's components after it's added. i'll include some image's so it's easier to understand.

The panel in question is called MenuPanel.

so here is the login button which currently only clears the contentpanel and then adds the menupanel.

Login page where the button is located

and here is the menu panel empty

here is the menu panel all empty

the code The interface handler you can see me messing around with the .repaint() method in the menu method already. public class InterfaceHandler {

private static InterfaceHandler singleton;

/**
 * Checks if there is already a instance running of InterfaceHandler if so
 * return the instance if not create a instance.
 *
 * @return Instance
 */
public static InterfaceHandler instance() {
    if (singleton == null) {
        singleton = new InterfaceHandler();
    }

    return singleton;
}

private ContentPanel contentPanel;
private LoginPanel loginPanel;
private MainFrame mainFrame;
private MainPanel mainPanel;
private MenuPanel menuPanel;

/**
 * Initializes default Interface
 */
public InterfaceHandler() {
    initComponents();

}

/**
 * Initializes GUI Components that will be needed now or in the future.
 */
public void initComponents() {
    mainFrame = new MainFrame();
    mainPanel = new MainPanel();
    contentPanel = new ContentPanel();
    loginPanel = new LoginPanel();
    menuPanel = new MenuPanel();

    contentPanel.setBounds(100, 100, 860, 700);
    menuPanel.setBounds(10, 10, 80, 400);
    loginPanel.setBounds(280, 250, 300, 200);

    mainFrame.add(mainPanel);
    mainPanel.add(contentPanel);

    mainFrame.setVisible(true);
    contentPanel.add(loginPanel);
}

/**
 * Clears the ContentPanel of all components
 */
public void clear() {
    System.out.println("Clear Ran");
    contentPanel.removeAll();
    contentPanel.repaint();
}

/**
 * Adds the Login Screen.
 */
public void login() {
    System.out.println("Login Ran");
    contentPanel.add(loginPanel);
    contentPanel.repaint();
}

/**
 * Adds the menu.
 */
public void menu() {
    System.out.println("Menu Ran");
    mainPanel.sidePanel.add(menuPanel);
    menuPanel.repaint();
    mainPanel.repaint();
    mainPanel.sidePanel.repaint();
}

}

And here is the code for the panel, Most of it is autogenerated by the netbeans design editor. so it's ugly (i'm sorry)

public class MenuPanel extends javax.swing.JPanel {

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

    jLabel1 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    jLabel2 = new javax.swing.JLabel();
    baggageButton = new javax.swing.JLabel();

    jLabel1.setBackground(new java.awt.Color(204, 204, 204));
    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Menu");

    jSeparator1.setBackground(new java.awt.Color(204, 204, 204));

    baggageButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    baggageButton.setText("Baggage");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSeparator1)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, 0)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
}// </editor-fold>                        

// Variables declaration - do not modify                     
private javax.swing.JLabel baggageButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration                   
}

just to clarify, the method menu is called from a different class.

MagicNumbers
  • 45
  • 1
  • 6
  • For better help sooner, provide an [MCVE](http://stackoverflow.com/help/mcve). Though I can say that what you appear to be trying to do is more properly done with a [`CardLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). The way you are doing it (swapping panels manually) is sketchy and prone to anomalous layout/painting issues. – Radiodef Nov 10 '14 at 22:00

2 Answers2

9

If you are adding components to an already visible panel, you need to call validate().

public void menu() {
    System.out.println("Menu Ran");
    mainPanel.sidePanel.add(menuPanel);

    mainPanel.sidePanel.validate();  // try this

    menuPanel.repaint();
    mainPanel.repaint();
    mainPanel.sidePanel.repaint();
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • 3
    Note, using `revalidate()` might be [more correct](http://stackoverflow.com/questions/859493/remove-swing-component-using-validate-or-revalidate). – martinez314 Nov 10 '14 at 22:19
0

(This answer is wrong but the ensuing comments do help understand why @whiskeyspider's answer is right)

In this code:

mainFrame.add(mainPanel);
mainPanel.add(contentPanel);

mainFrame.setVisible(true);
contentPanel.add(loginPanel);

You're not adding menuPanel anywhere. It is added (too late) in the menu() method, but that's not being called.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • I'm sorry maybe i should have been more clear, when the login button is pressed in the loginpanel it runs this code(it's at the bottom of InterfaceHandler). public void menu() { System.out.println("Menu Ran"); mainPanel.sidePanel.add(menuPanel); menuPanel.repaint(); mainPanel.repaint(); mainPanel.sidePanel.repaint(); } – MagicNumbers Nov 10 '14 at 21:56
  • Are you sure? I can see the `menu()` method defined, but I cannot see it being called anywhere. – Paul Hicks Nov 10 '14 at 21:58
  • it's being called from a different class. once the button is pressed it calls InterfaceHandler.instance.menu() – MagicNumbers Nov 10 '14 at 22:00
  • Ah. Does that class also `pack()` the JFrame? – Paul Hicks Nov 10 '14 at 22:02
  • Nope, i never used pack() because that seemed to mess some stuff up.. should i have? – MagicNumbers Nov 10 '14 at 22:04
  • Only if your mainPanel needs to move. It may be that your menuPanel is being drawn, but outside the bounds of your current JFrame, and thus, it's invisible. – Paul Hicks Nov 10 '14 at 22:06
  • it's having all sorts of weird issues, minimizing and maximizing the frame seems to load the components of the menupanel, even thogh the size is the same. i guess ill look up a different way to do this with a layout manager. – MagicNumbers Nov 10 '14 at 22:14
  • Sounds like that's exactly the problem: your existing code adds the new stuff off-screen, and it's not until the JFrame auto-repacks itself that it's being drawn. A better layout manager is exactly what you need. – Paul Hicks Nov 10 '14 at 22:18