How to Change setVisible() on Panels in different Java Class

1.9k Views Asked by At
package com.spiralfive.inventory;

import java.awt.Font;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.BorderLayout;

import javax.swing.JApplet;
import javax.swing.JButton;

public class MainFile extends JApplet implements MouseListener {

// Declare the initial components
private JButton btnNewInventory;
private JButton btnMarkDown;
private JButton btnProcessSales;
private JButton btnActive;
private JButton btnStats;
private Panel mainPanel;
private Panel menuPanel;
private Panel inventoryPanel;
private Panel setactivePanel;

// Call the Initializer...
public void init() {
    initComponents();
}

// Initialize the Main Components - Mostly Menu Items
private void initComponents() {

    // Main Form Components
    mainPanel = new Panel();
    menuPanel = new Panel();
    inventoryPanel = new Panel();
    setactivePanel = new Panel();
    btnNewInventory = new JButton("New Inventory");
    btnActive = new JButton("Set Active");
    btnMarkDown = new JButton("Mark Down");
    btnProcessSales = new JButton("Process Sale");
    btnStats = new JButton("Statistics");

    // Enable Mouse Interactivity
    btnNewInventory.addMouseListener(this);
    btnMarkDown.addMouseListener(this);
    btnProcessSales.addMouseListener(this);
    btnActive.addMouseListener(this);
    btnStats.addMouseListener(this);

    // Set the Fonts
    Font buttonFont = new Font("Arial", Font.PLAIN, 20);

    // Apply Fonts To Menu
    btnNewInventory.setFont(buttonFont);
    btnMarkDown.setFont(buttonFont);
    btnProcessSales.setFont(buttonFont);
    btnActive.setFont(buttonFont);
    btnStats.setFont(buttonFont);

    // Set Panel Layout For Menu Items
    menuPanel.setLayout(new java.awt.GridLayout(1,6));

    // Add the Components
    menuPanel.add(btnNewInventory);
    menuPanel.add(btnActive);
    menuPanel.add(btnMarkDown);
    menuPanel.add(btnProcessSales);
    menuPanel.add(btnStats);

    // Add The Menu To the Main Panel
    mainPanel.add(BorderLayout.NORTH, menuPanel);

    // Make Inventory Load by Default
    createPanels();
    makeInvVisible();

    // Set It All Visible
    add(mainPanel);

}

// Create the Panels Used in this applet
public void createPanels() {
    NewInventory inventoryPanel = new NewInventory();
    inventoryPanel.setLayout(new java.awt.GridLayout(6,2));
    mainPanel.add(BorderLayout.CENTER, inventoryPanel);

    SetActive setactivePanel = new SetActive();
    setactivePanel.setLayout(new java.awt.GridLayout(6,2));
    mainPanel.add(BorderLayout.CENTER, setactivePanel);

}

public void makeInvVisible() {
    inventoryPanel.setVisible(true);
    mainPanel.repaint();
}

public void makeInvDisappear() {
    inventoryPanel.setVisible(false);
    mainPanel.repaint();
}

public void makeActiveVisible() {
    setactivePanel.setVisible(true);
    mainPanel.repaint();
}

public void makeActiveDisppear() {
    setactivePanel.setVisible(false);
    mainPanel.repaint();
}

public void mouseClicked(MouseEvent e) {

    // Determine Which Button Has Been Clicked
    JButton currentButton = (JButton)e.getComponent();

    // New Inventory Button
    if(currentButton == btnNewInventory) {
        makeInvVisible();
        makeActiveDisppear();
    }

    // Set Active Button
    else if(currentButton == btnActive) {
        makeActiveVisible();
        makeInvDisappear();
    }
}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

}

I am using the code above attempting to make a new Panel() appear and the existing panel disappear when a button is clicked by using setVisible. However, I cannot get the Panel() to change. Nothing happens when the buttons are clicked, which are set to change setVisible to false on the current Panel and true on the requested Panel.

I am fairly certain it is because the panels are set a private in a different class (initComponents). How do I access the setVisible property on the panels? Or, is this even what's wrong?

1

There are 1 best solutions below

1
On

Add

this.validate();

in the makeActiveVisible() method.

Swing components have a default state of being invalid and won't be painted to the screen unless validated (by calling the .validate() method on either the component itself or on one of the parent containers).

See also

repaint() in Java