Thursday, April 19, 2012

JPanel Transparency Demo

Program Description:

I've been surfing in the internet about JPanel transparency and I found out that there are lots of code versions out there on how to make JPanel background color transparent and to tell you, the codes are not so easy to digest which means very bad for beginners.

So today I created a very simple code on how to make a semi-transparent JPanel and a total transparent JPanel which is very easy to follow and understand. I always make sure to organize my codes into a uniform format so the errors can easily be tracked, modified, and understand.

Kindly post a comment if there is something in the code you didn't understand.

Output:



Code:

/**
* File: jpanelTransparency.java
* Tiltle: JPanel Transparency Demo
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class jpanelTransparency extends JFrame {

//Initializing JComponents
JPanel testSubject, top, buttonPanel;
JButton normal, semiTrans, transparent;

//Setting up GUI
public jpanelTransparency() {

//Setting up the Title of the Window
super("JPanel Transparency Demo");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(430,200);

//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Constructing testSubject JPanel
testSubject = new JPanel();

//Constructing top JPanel and setting up its background color property
top = new JPanel();
top.setBackground(Color.blue);

//Constructing buttonPanel JPanel and setting up its layout property
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,3));

//Constructing all 3 JButtons
normal = new JButton("Normal");
semiTrans = new JButton("Semi-Transparent");
transparent = new JButton("Transparent");

//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);

//Setting up testSubject JPanel properties
testSubject.setBorder(BorderFactory.createLineBorder(Color.black,1));
testSubject.setBackground(Color.WHITE);
testSubject.setPreferredSize(new Dimension(100,100));

//Adding the "testSubject" JPanel inside "top" JPanel
top.add(testSubject);

//Adding all 3 JButtons inside buttonPanel JPanel
buttonPanel.add(normal);
buttonPanel.add(semiTrans);
buttonPanel.add(transparent);

//Implemeting Even-Listener on JButton using ActionListener
normal.addActionListener(
new ActionListener() {

//Handle JButton event if Enter key is pressed or if mouse is clicked.
public void actionPerformed(ActionEvent event) {

//Creating a white color as normal background color to our testSubject panel
Color whiteColor = new Color (255, 255, 255, 255);
testSubject.setBackground(whiteColor);
}
}
);

//Implemeting Even-Listener on JButton using ActionListener
semiTrans.addActionListener(
new ActionListener() {

//Handle JButton event if Enter key is pressed or if mouse is clicked.
public void actionPerformed(ActionEvent event) {

//Creating a semi-transparent red backgroud color to our testSubject panel
Color transparentRed = new Color (255, 0, 0, 25);
testSubject.setBackground(transparentRed);
}
}
);

//Implemeting Even-Listener on JButton using ActionListener
transparent.addActionListener(
new ActionListener() {

//Handle JButton event if Enter key is pressed or if mouse is clicked.
public void actionPerformed(ActionEvent event) {

//Set the background color to empty
testSubject.setBackground(null);
}
}
);

//Adding JPanel top and buttonPanel to the container
pane.add(top, BorderLayout.NORTH);
pane.add(buttonPanel, BorderLayout.SOUTH);

/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}

//Main Method
public static void main (String[] args) {
jpanelTransparency jt = new jpanelTransparency();
}
}

Important Part of the Program:

//Implemeting Even-Listener on JButton using ActionListener
semiTrans.addActionListener(
new ActionListener() {

//Handle JButton event if Enter key is pressed or if mouse is clicked.
public void actionPerformed(ActionEvent event) {

//Creating a semi-transparent red backgroud color to our testSubject panel
Color transparentRed = new Color (255, 0, 0, 25);
testSubject.setBackground(transparentRed);
}
}
);

//Implemeting Even-Listener on JButton using ActionListener
transparent.addActionListener(
new ActionListener() {

//Handle JButton event if Enter key is pressed or if mouse is clicked.
public void actionPerformed(ActionEvent event) {

//Set the background color to empty
testSubject.setBackground(null);
}
}
);

No comments:

Post a Comment