Thursday, May 10, 2012

Transparent JTextField | The Simple and Basic way to do it

Program Description:

Today I have created a very simple Java Program on how to create a transparent JTextField. This is just a basic way on how to create a 100% transparent JTextField which means you won't see anything but can type a text or an input. Even if you change the background color still you won't see anything because it is totally transparent. To know how, see the code below and the sample screenshots.

Output:


Code:

/**
* File: simpleTransparentJTF.java
* Tiltle: Transparent JTextField | The Simple and Basic way to do it.
* Author: http://java-code-complete.blogspot.com/
*/

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

public class simpleTransparentJTF extends JFrame {

//Constructing JTextField
JTextField field = new JTextField("Type your Text Here...",20);

//Initiazlizing the class Font to set our JTextField text style
Font defualtFont;

//Setting up GUI
public simpleTransparentJTF() {

//Setting up the Title of the Window
super("Transparent JTextField");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(370,85);

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

//Constructing class Font property
defualtFont = new Font("Arial", Font.BOLD, 18);

//Setting JTextField Properties
field.setFont(defualtFont);
field.setPreferredSize(new Dimension(150,40));
field.setForeground(Color.BLACK);

//Step 1: Remove the border line to make it look like a flat surface.
field.setBorder(BorderFactory.createLineBorder(Color.white, 0));

//Step 2: Set the background color to null to remove the background.
field.setBackground(null);

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

//Change the background color of the container to see
//if the JTextField is really transparent.
pane.setBackground(Color.YELLOW);

//Adding JTextField to our container
pane.add(field);

//Setting up the container layout
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
pane.setLayout(flow);

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

//Disable Frame Size
setResizable(false);
}

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

To test if it is really transparent, you can change the background color of the window by just changing the color if this line below:

//Change the background color of the container to see
//if the JTextField is really transparent.
pane.setBackground(Color.YELLOW);

Important Part of the Program:

//Step 1: Remove the border line to make it look like a flat surface.
field.setBorder(BorderFactory.createLineBorder(Color.white, 0));

//Step 2: Set the background color to null to remove the background.
field.setBackground(null);

Note: This techniques won't give you a very nice output if you are going to put a graphic image at the back of the JTextField because it will just ignore the image. Do not use images as background of your window if you are going to use this technique. You can use different background colors using the class Color.

If you have any questions or comments about the program, feel free to post it here.

Wednesday, May 9, 2012

Resizing or Changing JTextField Size using simple Java Code

Program Description:

This program is just a simple java code to illustrate on how to re-size the JTextField in a simple way using simple code. Similar to JPanel, the way on how to change JTextField size is through the method setPreferredSize(); and the class Dimension.

Output:
Code:

/**
* File: resizeJTextField.java
* Tiltle: Simple way in Resizing JTextField
* Author: http://java-code-complete.blogspot.com/
*/

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

public class resizeJTextField extends JFrame {

//Constructing JTextFields
JTextField size1 = new JTextField("RESIZING JTEXTFIELD");
JTextField size2 = new JTextField("RESIZING JTEXTFIELD");
JTextField size3 = new JTextField("RESIZING JTEXTFIELD");
JTextField size4 = new JTextField("RESIZING JTEXTFIELD");

//Setting up GUI
public resizeJTextField() {

//Setting up the Title of the Window
super("Simple way in Resizing JTextField");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(500,435);

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

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

//Setting up the layout of the container
pane.setLayout(new FlowLayout(FlowLayout.CENTER));

//Setting the JTextField text to center
size1.setHorizontalAlignment(SwingConstants.CENTER);
size2.setHorizontalAlignment(SwingConstants.CENTER);
size3.setHorizontalAlignment(SwingConstants.CENTER);
size4.setHorizontalAlignment(SwingConstants.CENTER);

//Spicify the size of the JTextField using the method setPreferredSize() and the class Dimension
size1.setPreferredSize(new Dimension(450,150));
size2.setPreferredSize(new Dimension(450,100));
size3.setPreferredSize(new Dimension(450,75));
size4.setPreferredSize(new Dimension(450,50));

//add the JTextFields in the container
pane.add(size1);
pane.add(size2);
pane.add(size3);
pane.add(size4);

/**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) {
resizeJTextField pjtf = new resizeJTextField();
}
}

Important Part of the Program:

//Spicify the size of the JTextField using the method setPreferredSize() and the class Dimension
size1.setPreferredSize(new Dimension(450,150));
size2.setPreferredSize(new Dimension(450,100));
size3.setPreferredSize(new Dimension(450,75));
size4.setPreferredSize(new Dimension(450,50));

Sunday, May 6, 2012

Fixing CCB-A Problem in JCreator Pro 4.5 or any version or any software | The safe way to do it.

When you encounter CCB-A problem, a pop-up window that says

Your system clock appears to have been changed, possibly in an attempt to defeat the security system on this program. Please correct your system clock before trying to run this program again. If your clock is already correct, rebooting the system may fix this problem, otherwise contact the author of this program for instructions (report code CCB-A).
everytime you open an application. This can happen to any trial version software since it is used to protect trial version software from being abused by changing the system time so it won’t expire. This problem also exist if you are using trial version software then you apply a crack to make it a full version, so be careful with cracks.

There are lots of ways on how to solve this problem but most of them are too risky since it involves changing the registry which could damaged other software if it is not handled properly.

In my experience, I am using JCreator Pro 4.5 and installed it in my computer. I forgot to register it so I closed the application without registering. When I opened it again, I was shocked because a pop-up window saying that “My system clock appears to have changed and so on...” which was the CCB-A error. It was so annoying because I can’t register it anymore. I tried to change my system clock but still it didn’t worked. I tried uninstalling and installing it again but still it didn’t worked out. I did almost everything from registry to my CMOS but it didn’t change anything. The problem was still there. So I did these very simple steps as my last resort in solving the problem.

1.    I uninstall the JCreator Pro and install it again to have a fresh copy. (Note: Don’t open the application yet).
2.    I turned off the computer and remove the battery of my computer. The battery is used to make your system clock up to date.
3.    I turned on the computer without the battery and set the CMOS time manually to current date and time and saved it.
4.    As I was in the desktop, I changed the CLOCK in the TASKBAR and set it to current date and time.
5.    I opened the application and BOOMM!!! The solution was perfect. It runs perfectly and I was able to register the software.

I hope my solution works on your computer. Please give me some feedback if you have further questions or comments or if it works or not.

Thursday, May 3, 2012

My First JApplet | How to create an Applet

Program Description:

Today we are going to create our first basic java applet program using JApplet component. This is just a very short and simple java code that you can really understand.

JApplet is a subclass of Applet, a class in the java.applet package. An applet is a visual window inside a webpage that means they need to be run as part of a World Wide Web page.

In creating an Applet it involves two files the java file where our java codes resides of course and the HTML file where our java codes will be running using the ".class" file.

In creating our class it should always extends to JApplet. Example:

public class myFirstApplet extends JApplet {

}

then next to our java applet code is our HTML code where we embed our java code using the ".class" file. Here is the basic code on how to embed our java applet code in a webpage.




1. Starts with an opening HTML tag <html>
2. Followed by an opening Applet tag <applet>
3. specify your java applet code by locating the ".class" file which is usually created if you compile your java codes.
4. Specify the width and the height of your applet in the webpage.
5. Closing the applet tag </applet>
6. Closing the html tag </html>

Note: Compile your java code and your HTML code then run the HTML file. I am using JCreatorPro so I automatically load the HTML file, then compile, and run. See output below:

Output:
Code:

Java Code

/**
* File: myFirstApplet.java
* Tiltle: My First JApplet | How to create an Applet
* Author: http://java-code-complete.blogspot.com/
*/

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

public class myFirstApplet extends JApplet {

//Creating paint method so we can draw text on applet's background
public void paint(Graphics g) {

//Call the method paint
super.paint(g);

//Draw 3 strings at different locations
g.drawString("Welcome to my First Java Applet",30,30);
g.drawString("Welcome to Java Programming",30,50);
g.drawString("Thank you for Visiting: http://java-code-complete.blogspot.com",30,90);
}
}

HTML Code: