Click here to Skip to main content
15,666,033 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have been making this project without drag and drop in Java Swing and I have been encountered with an issue where the panel behind my shown panel in card layout do not disposes, i.e : my panel is transparent , so the panel behind it is also showed. What can I do to dispose of the back panel. Moreover when i slightly resize the frame , the back panel gets invisible.

What I have tried:

CODE FOR ADMIN PANEL:
Java
<pre>package carsalessystem;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

public class adminPanel implements ActionListener {

    public static final Font MY_FONT = new Font("Ubuntu Mono", Font.BOLD, 20);
    JFrame frame = new JFrame();
    JPanel panel1 = new JPanel();
   static JPanel contPanel = new JPanel();
    JButton admin = new JButton("Administrator Login");
    JButton employee = new JButton("Employee Login");
    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
    ImageIcon image = new ImageIcon("C:\\Users\\faroo\\Desktop\\car sales stock\\ComputerImage.jpg");
    Image scaledImage = image.getImage().getScaledInstance(screensize.width, screensize.height, Image.SCALE_SMOOTH);
    JLabel backgroundImage;
    CardLayout card = new CardLayout();
    JButton[] buttons = new JButton[10];

    String[] dropDown = new String[]{
        "Hi", "Hello"
    };
    JList list = new JList(dropDown);

    adminPanel() {
        //Frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(screensize.width, screensize.height);
        frame.setLocation((screensize.width / 2) - (frame.getWidth() / 2), (screensize.height) / 2 - (frame.getHeight() / 2));
        frame.setLayout(null);

        //IMAGE BACKGROUND
        image = new ImageIcon(scaledImage);
        backgroundImage = new JLabel(image);
        backgroundImage.setBounds(0, 0, screensize.width, screensize.height);
        
        //PANEL 1
        panel1.setBounds(screensize.width / 2 - 480, 120, 960, 42);
        panel1.setBackground(new Color(223, 223, 223));
        panel1.setLayout(new FlowLayout(FlowLayout.LEADING));
        
        //contPanel
        contPanel.setBounds(panel1.getLocation().x,162,960,800);
        contPanel.setBackground(Color.red);
        contPanel.setLayout(card);
        contPanel.add(new addProduct().addProdPanel,"2");
        contPanel.add(new home().homePanel,"0");
        
        //BUTTON: HOME + ABOUT US + ADD PRODUCT + REMOVE PRODUCT +SALES DETAILS +SAlES ORDER
        buttons[0] = new JButton("HOME");
        buttons[1] = new JButton("ABOUT US");
        buttons[2] = new JButton("ADD PRODUCT");
        buttons[3] = new JButton("REMOVE PRODUCT");
        buttons[4] = new JButton("SALES ORDER");
        buttons[5] = new JButton("SALES DETAILS");
        buttons[6] = new JButton("LOG OUT");

        for (int i = 0; i <= 6; i++) {
            buttons[i].setFont(MY_FONT);
            buttons[i].setFocusable(false);
            buttons[i].setBackground(new Color(61, 94, 148));
            buttons[i].setForeground(new Color(223, 223, 223));
            panel1.add(buttons[i]);
            buttons[i].addActionListener(this);
        } 
        card.show(contPanel, "0");
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(contPanel);
        frame.getContentPane().add(backgroundImage);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i <= 6; i++) {
            if (e.getSource() == buttons[i]) {
                for (int j = 0; j <= 6; j++) {
                    
                    buttons[j].setBackground(new Color(61, 94, 148));
                }
                buttons[i].setBackground(new Color(69, 89, 119));
               
            }
        }
        if(e.getSource()==buttons[0]){
            card.show(contPanel, "0");
        }
        if(e.getSource()==buttons[2]){
            card.show(contPanel, "2");
            
        }
    
      

    }
}


CODE FOR THE PANEL HOME
Java
package carsalessystem;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class home {

   static JPanel homePanel =new JPanel();
    JLabel label =new JLabel("HELLO HI");
    
    home(){
        
        homePanel.setBackground(Color.red);
        homePanel.add(label);
        homePanel.setVisible(true);
    }
    
}

CODE FOR THE PANEL ADD PRODUCT
Java
package carsalessystem;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class addProduct {

   
    String name;
    String type;
    String company;
    int price;
    int quantity;
    String description;
    
    
    JPanel addProdPanel = new JPanel();
    String types[] = {"Select Type", "Laptops", "Laptop Accessories", "Desktop Computer", 
        "Gaming Console", "Camera" , "Computer Accessories" , "Other"};
    String companies[] = {"Select Company", "Dell", "Apple", "Samsung", "HP", "Acer",
        "Microsoft", "Lenovo", "Other"};
    JLabel label1 = new JLabel("PRODUCT NAME");
    JLabel label2 = new JLabel("SELECT TYPE");
    JLabel label3 = new JLabel("SELECT COMPANY");
    JLabel label4 = new JLabel("PRODUCT PRICE");
    JLabel label5 = new JLabel("PRODUCT QUANTITY");
    JLabel label6 = new JLabel("PRODUCT DESCRIPTION");
    Font newFont = new Font("Ubuntu Mono", Font.BOLD, 20);
    Font newFont2 = new Font("Ubuntu Mono", Font.PLAIN, 18);
    JTextField tx1 = new JTextField("e.g: Keyboard");
    JComboBox  cb2 = new JComboBox(types);
    JComboBox  cb3 = new JComboBox(companies);
    JTextField tx4 = new JTextField("e.g: 2000");
    JTextField tx5 = new JTextField("e.g: 150");
    JTextArea tx6 = new JTextArea("e.g: Lightening Keyboard,\n with Anti-Ghosting Mode.");
    JButton button1 =new JButton("ADD PRODUCT");
    
    addProduct() {
        addProdPanel.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));
        addProdPanel.setLayout(null);

        //LABEL 1: PRODUCT NAME
        label1.setFont(newFont);
        label1.setForeground(Color.white);
        label1.setBounds(20, 40, 200, 20);
        tx1.setBounds(20,72,250,30);
        tx1.setFont(newFont2);
        
        //LABEL 2 : SELECT TYPE + COMBO BOX
        label2.setFont(newFont);
        label2.setForeground(Color.white);
        label2.setBounds(20, 110, 200, 20);
        cb2.setBounds(20,142,250,30);
        cb2.setFont(newFont2);
        
        //LABEL 3 : SELECT COMPANY + COMBO BOX 
        label3.setFont(newFont);
        label3.setForeground(Color.white);
        label3.setBounds(20, 180, 200, 20);
        cb3.setBounds(20,212,250,30);
        cb3.setFont(newFont2);
        
        //LABEL 4 : PRODUCT PRICE
        label4.setFont(newFont);
        label4.setForeground(Color.white);
        label4.setBounds(20, 250, 200, 20);
        tx4.setBounds(20,282,250,30);
        tx4.setFont(newFont2);
        
        //LABEL 5 : PRODUCT QUANTITY
        label5.setForeground(Color.white);
        label5.setFont(newFont);
        label5.setBounds(20, 320, 200, 20);
        tx5.setBounds(20,352,250,30);
        tx5.setFont(newFont2);
        
        //LABEL 6 : PRODUCT DESCRIPTION + TEXT FIELD
        label6.setForeground(Color.white);
        label6.setFont(newFont);
        label6.setBounds(20,390,200,20);
        tx6.setBounds(20,422,250,60);
        tx6.setFont(newFont2);
        
        //BUTTON 1: ADD PROdUCT
        button1.setBounds(45,490,200,40);
        button1.setBackground(Color.darkGray);
        button1.setForeground(Color.white);
        button1.setFont(newFont);
        
        
        //EVENT LISTENERS
        tx1.addMouseListener(new textFieldListener());
        tx4.addMouseListener(new textFieldListener());
        tx5.addMouseListener(new textFieldListener());
        tx6.addMouseListener(new textFieldListener());
        button1.addActionListener(new buttonListener());
        
        
 

        

        addProdPanel.setBounds(0, 0, 100, 200);
        addProdPanel.add(label1);
        addProdPanel.add(label3);
        addProdPanel.add(label2);
        addProdPanel.add(label4);
        addProdPanel.add(label5);
        addProdPanel.add(label6);
        
        addProdPanel.add(tx1);
        addProdPanel.add(cb2);
        addProdPanel.add(cb3);
        addProdPanel.add(tx4);
        addProdPanel.add(tx5);
        addProdPanel.add(tx6);
        addProdPanel.add(button1);
        addProdPanel.setVisible(true);
        
    }
    
    public class textFieldListener implements MouseListener{
        
          @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource()==tx1 )
               tx1.setText("");
            if( e.getSource()==tx4 )
                tx4.setText("");
            if(e.getSource()==tx5)
                tx5.setText("");
            if(e.getSource()==tx6)
                tx6.setText("");
        }
        
        
       @Override
      public void mousePressed(MouseEvent e){}
        @Override
        public void mouseReleased(MouseEvent e) {}
        
         @Override
        public void mouseEntered(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}
           
    }
    
    public class buttonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
           
            if(e.getSource()==button1){
                
                name = tx1.getText();
                type = cb2.getSelectedItem().toString();
                company = cb3.getSelectedItem().toString();
                price = Integer.parseInt(tx4.getText());
                quantity =  Integer.parseInt(tx5.getText());
                description = tx6.getText();
                
                System.out.println(name);
                System.out.println(type);
                System.out.println(company);
                System.out.println(price);
                System.out.println(quantity);
                System.out.println(description);
                
                
                
            }
            
            
        }
        
        
        
    }

    
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900