AWT Components in Java

Create MenuBar, Menu Items, Menus Java Example

Menus Java are a number of pull-down combo boxes (In Java called as Choice) placed at single place for easy selection by the user. To create menus, the java.awt package comes with mainly four classes – MenuBar, Menu, MenuItem and CheckboxMenuItem. All these four classes are not AWT components as…

What is Java Choice with Example?

Java comes with many GUI components placed in java.awt package. One component is Java Choice (in other GUI technologies, it is known as combo box). 1. What is Java Choice component? 2. When to use Choice? Choice is replacement to radio buttons. When radio buttons are many to display, they…

Radio button and Radio button group

1. What is Radio button? Infact, there is no radio button class in Java. Radio buttons are nothing but checkboxes only but grouped as one unit. 2. What the advantage of grouping checkboxes? Once grouped, in the group if one checkbox is selected, the other gets automatically deselected. This is…

Java Button Example with ActionListener Event Handling

A simple Java Button Example given to understand how to create components and link to event handling mechanism. User interface (communication-point) with a running Java program can be in two ways – using keyboard input and the other with GUI. Java supports GUI environment through GUI components like button, check…

Mouse Double Click Example Java

The code explains how to handle double clicks of mouse key. You even can handle triple clicks also, if required. Example on Mouse Double Click Example

Output screen on Mouse Double Click Example btn.addMouseListener(new MyListener()); Instead of button btn, it can be any GUI component like Checkbox etc. Just…

Mouse Right Click Example Java

This Java code on Mouse Right Click Example explains how to handle the right clicks of mouse key. Mouse Right Click Example

Output screen on Mouse Right Click Example Other way of event handling is: if(e.isMetaDown()) { System.out.println(“You right clicked on the button”); } The static constant modifier MouseEvent.BUTTON3_MASK…

Java AWT GridBagLayout Manager

This is the king of all layout managers because it can handle almost any type of layout arrangement. It is more flexible and at the same time more complex to use. This is an extension of GridLayout manager. Both grid layout and grid bag layout use the concept of grid…

Java AWT CardLayout Manager

After learning the basic layout managers (Flow, Border and Grid), let us study the advanced layout managers – CardLayout and GridBagLayout. These two managers are used in specific cases, but not often, where basic layout managers with panel combinations fail to fulfill the layout requirements. Layout management is the procedure…

Java Create Random Color

We know using java.awt.Color class, different colors (nearly 16 million shades) can be created and applied to graphics or components. Using java.util.Random class, various colors can be produced randomly and applied to any component. In the following program, each button click gives different color to the frame. For generating colors…

Java Frame Desktop Position

By default the frame, you create, is placed on the left top corner of the monitor by JVM. But you can dictate the position. In the following program, the frame is positioned in the center of the monitor. The task here is, you must get the screen (monitor) size and…

TextField Applet Adding Two Numbers

TextField Applet Adding Two Numbers

After validating user name and password using TextField in an application (application extends frame), let us rewrite the same program using applets but with a different functionality. We take two numbers from the user and display the sum of two numbers. The following program comes with 3 text fields. In…

All AWT Components – At a Glance

AWT includes many components where a novice confuses a lot. For him, it is like in a deep forest without compass. To have all components at a single place and to focus on special programs, this topic is meant. Check out All AWT Components – At a Glance Frame Closing…

Java Placing Frame Icon

Java Placing Frame Icon Summary: Learn in this tutorial "Java Placing Frame Icon", how to place your own icon on the frame (in place of default coffee cup) title bar. In the following program, our own choice of icon (bird) is placed on the frame title bar. It replaces the…

Java Button Applet

The GUI components can be placed in a frame or an applet (as both are containers). We have seen many programs with frame and now let us write an applet with GUI, say button. While developing applet GUI, the class should extend Applet instead of Frame. The constructor is replaced…

HeadlessException

HeadlessException Summary: By the end of this tutorial you will come to know the possibility of raising HeadlessException by JVM. It falls under the category unchecked exception. This runtime exception is thrown when the code that is dependent on the hardware like keyboard, display, or mouse is called in an…

Java Component Preferred Size

To develop user friendly and good look environment, the layout of the components in a container is very important to the programmer like the selection of colors and fonts for graphics. The user should feel free and comfortable to stay some time in GUI. To give attractive graphical user interface…

Java AWT Containers

After knowing what a component is, let us go to the list of containers Java supports. For this, the container hierarchy is separated from the earlier component hierarchy and displayed here. Infact, the Container is a component as it is a subclass of Component class. It can use all the…

Borderless Frame pack()

Borderless Frame pack() Summary: By the end of this tutorial "Borderless Frame pack()", you will understand how to create a borderless frame and pack() method. 1. Creating a borderless window Frame is known as top-level window as it contains border and title bar (panel is not a top-level window; it…

Java AWT Label Alignment

Label component displays text just like a drawString(). The difference is Label gets the status of a component so that it can be added in position format using layout manager. Label displays text in one line only. User cannot edit the text of the label. It is meant only for…