Cardlayout, problems switching panels with low opacity

120 views Asked by At

I used a JFrame adding a same sized JPanel with a specific Background Image. Then I added 3 smaller Jpanels on that Background using the CardLayout.

I defined the opacity with 50% of the Panels on the CardLayout to see the Background of the mainJPanel on the Panels with low opacity as well. I hope you guys understood so far.

My problem is now, when I show the first panel everthing works well. But when I switch to the next Panel on the Cardlayout and return to the first with a back Button, the second panel can be seen through the first panel because of the opacity.

But I want to see the background and not the second panel in the background. Is there any possibility to fix that? I tried nearly everything but no solution. Thanks for helping!

package GUIElements;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import java.awt.CardLayout;

import javax.swing.JButton;

import net.proteanit.sql.DbUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class MainFrameSoftware extends JFrame implements CardLayoutSwitch{
private GUIOrgaCustomer customer;
private CardLayout c1;
private JPanel panelCard;
private JPanel panel1;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;

private JPanel panelMainContent;
private JPanel showCustomer;
private JPanel background;
private JButton btnNewCustomer;
private JButton btnShowAllCustomer;
private JButton btnUpdateCustomer;
private JButton btnDeleteCustomer;
private JButton backbtn;
private JButton backbtn2;
private JTable table;
private JScrollPane scrollPane;
private JLabel label;
private JLabel label_1;

public  MainFrameSoftware() {

    this.setSize(1037,666);
    this.setLocationRelativeTo(null);
    this.setResizable(false);

    background = new BackgroundPanel(new ImageIcon("\\\\I-INTRA-03\\IIS-Lehre\\DAPRO-WF4.informatik.hs-ulm.de\\15\\group_together\\GUI_graphics\\background.jpg"));
    getContentPane().add(background);
    background.setLayout(null);

    c1 = new CardLayout();
    panelCard = new JPanel();
    panelCard.setBounds(348, 26, 665, 577);
    panelCard.setLayout(c1);
    background.add(panelCard);

    panel1 = new JPanel();
    panel1.setBackground(new Color(0,0,0,50));
    panel1.setBounds(348, 26, 665, 577);
    panel1.setLayout(null);

    b1 = new JButton("Manage Customer");
    b1.setBounds(12, 45, 274, 90);
    panel1.add(b1);

    b2 = new JButton("Manage Employee");
    b2.setBounds(12, 170, 274, 90);
    panel1.add(b2);

    b3 = new JButton("Manage Equipment");
    b3.setBounds(12, 295, 274, 90);
    panel1.add(b3);

    b4 = new JButton("Manage Event");
    b4.setBounds(379, 45, 274, 90);
    panel1.add(b4);

    b5 = new JButton("Manage Task");
    b5.setBounds(379, 170, 274, 90);
    panel1.add(b5);

    b6 = new JButton("Manage Service Provider");
    b6.setBounds(379, 295, 274, 90);
    panel1.add(b6);




    //Panel Manage Customer
    panelMainContent = new JPanel();
    panelMainContent.setBackground(new Color(0, 191, 255));
    panelMainContent.setBorder(new EmptyBorder(5, 5, 5, 5));
    panelMainContent.setLayout(null);

    btnShowAllCustomer = new JButton("show all Customer");
    btnShowAllCustomer.setBounds(192, 50, 286, 104);
    panelMainContent.add(btnShowAllCustomer);

    btnNewCustomer = new JButton("add Customer");
    btnNewCustomer.setBounds(192, 167, 286, 104);
    panelMainContent.add(btnNewCustomer);

    btnUpdateCustomer = new JButton("update Customer");
    btnUpdateCustomer.setBounds(192, 286, 286, 104);
    panelMainContent.add(btnUpdateCustomer);

    btnDeleteCustomer = new JButton("delete Customer");
    btnDeleteCustomer.setBounds(192, 407, 286, 104);
    panelMainContent.add(btnDeleteCustomer);

    backbtn = new JButton("Back");
    backbtn.setBounds(489,514,148,50);
    panelMainContent.add(backbtn);

    //ende neu
    //Adding show all customer panel

    showCustomer = new JPanel();
    showCustomer.setLayout(null);
    backbtn2 = new JButton("Back");
    backbtn2.setBounds(489, 514,148,50);
    showCustomer.add(backbtn2);

    panelCard.add(panel1,"1");
    panelCard.add(panelMainContent,"2");
    panelCard.add(showCustomer,"3");

    JPanel panel = new JPanel();
    panel.setBackground(new Color(0,0,0,50));
    panel.setBounds(22, 56, 300, 358);
    background.add(panel);

    JPanel panel_1 = new JPanel();
    panel_1.setBackground(new Color(0,0,0,50));
    panel_1.setBounds(22, 447, 300, 130);
    background.add(panel_1);
}

@Override
public void showPanel(String panel) {
    System.out.println(panel);

    c1.show(panelCard, panel);
}

public void setActionListenerMainCustomerButton(ActionListener actionListener) {
    b1.addActionListener(actionListener);
}
public JButton getB1Button(){
    return b1;
}

public JPanel getCustomerPanel(){
    return customer;
}



//Init ActionListener Show all Customer called from Controller
public void setActionListenerCustomerHandle(ActionListener e) {
    btnShowAllCustomer.addActionListener(e);
}
// returns the JButton Show All Customer to the Controller Class
public JButton getBtnShowAllCust() {
    return btnShowAllCustomer;
}

public void showAllCustomer(ResultSet res){
    table = new JTable();
    table.setBounds(1048,899, 1, 1);
    scrollPane = new JScrollPane();
    scrollPane.setBounds(186, 38, 515, 227);
    showCustomer.add(scrollPane);
    scrollPane.setViewportView(table);
    table.setModel(DbUtils.resultSetToTableModel(res));
}
public void setActionListenerBackButton(ActionListener actionListener) {
    backbtn.addActionListener(actionListener);
}
public JButton getBackButton(){
    return backbtn;
}
}


 /*
 * Author: Christian Frindt
 * Description: Class Controller which passes all Data from GUI's to the             
 * */
package ControllerCollection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import addOns.Login;
import DAOCollection.CustomerDAOImpl;
import EntityClasses.Customer;
import GUIElements.GUILogin;
import GUIElements.GUIOrgaCustomer;
import GUIElements.GUI_Main;
import GUIElements.MainFrameSoftware;
import notesDBMySQL.DatabaseHandler;

public class Controller {

private GUILogin gui;
private DatabaseHandler handler;
private GUIOrgaCustomer customerGui;
private CustomerDAOImpl customer;
private MainFrameSoftware main;

public Controller() throws SQLException{

    gui = new GUILogin();
    handler = new DatabaseHandler();
    customerGui = new GUIOrgaCustomer();
    main = new MainFrameSoftware();
    controllActionListenerLogin();
    controllActionListenerBack();
    controllActionListenerMainFrameCustomer();
    controllActionListenerMainFrame();
    //controllActionListenerBack();
}
//ActionListener showall Customer
public void controllActionListenerMainFrameCustomer() {
    main.setActionListenerCustomerHandle(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()== main.getBtnShowAllCust()){
                customer = new CustomerDAOImpl();
                try {
                    ResultSet re = customer.getAllRows();
                    main.showAllCustomer(re);
                    main.showPanel("3");
                    System.out.println("panel shown");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }   
        }
    });

    /*customerGui.(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()==customerGui.getBtnaddCustomer()){

            }
        }

    });
    */
}

public void controllActionListenerBack(){
    main.setActionListenerBackButton(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent a) {
            if(a.getSource()== main.getBackButton()){
                main.showPanel("1");
            }
        }
    });
}

//ActionListener Loginbutton
public void controllActionListenerLogin(){

    gui.setActionListenerLogin(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
                if(e.getSource()==gui.getBtnLogin()){
                    try {
                        int result = new Login(getUser(),getPass()).checkLogin() ;

                        switch(result){

                        case 0 : gui.jLabelErrorsetText("Username or Password is missing!");
                            break;

                        case 1 : gui.jLabelErrorsetText("Error - Wrong Username or Password!");
                            break;

                        case 2 : gui.successColorJLabel();
                                 gui.jLabelErrorsetText(" ");
                                 gui.destroyFrame();
                                 main.setVisible(true);
                            break;

                        }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
}
//ActionListener ManageCustomer Button
public void controllActionListenerMainFrame(){

    main.setActionListenerMainCustomerButton(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()==main.getB1Button()){
                main.showPanel("2");
            }
        }
    });
}

public String getUser(){
    System.out.println(gui.getUsername());
    return gui.getUsername();
}
public char[] getPass(){
    return gui.getPassword();
}
public static void main(String[] args) throws SQLException{
    Controller control = new Controller();
}
}
0

There are 0 answers