Swing Java Component with example

Swing Java component:

abstract class Java Component:

Located at the top of the class hierarchy, represents the abstract class Java component base methods available, all AWT and Swing components can use together. We briefly present some of these methods here in front:

public Color getBackground():

supplies the background color of the Java component.

public font getFont()

supplies the font used in the Java component.

public Color getForeground()

supplies the foreground color (font color) of the Java component.

public int getHeight()

returns the height of the Java component.

public int getWidth()

returns the width of the Java component.



public boolean isEnabled()

returns true if the Java component is activated (react to user actions can), otherwise false.

public boolean isVisible()

returns true if the Java component is visible, otherwise false.

public void setBackground(Color c)

sets the background color of the Java component to color c.

public void setEnabled(boolean b)

activated (if b has the value true) or deactivated (if b has the value false hat) the Java component for user actions.

public void setFont(Font f)

sets the font used in the Java component.

public void setForeground(Color c)

sets the Java component’s foreground color to color c.

public void setLocation(int x, int y)

puts the Java component in the specified position. Where x is the horizontal and y is the vertical pixel coordinate (measured from the top left) of the upper left corner of the Java component.

public void setSize(int width, int height)

sets the width and height(in pixels) of the component.

public void setVisible(boolean b)

switches the Java component visible(if b has the value true) or invisible (if b has the value false).

As you can see, some of these methods use the Color and Font classes, whose objects can display special colors and fonts. Two of the methods mentioned here should look familiar to set the size of our frame (setSize) and to make our frame visible (setVisible). So, as you can see, inherit Both the Frame class and the JFrame class use these methods of the component class.


Java Container class:

Containers are special Java components that contain other components can. For this reason the class Container (replaces that of the class Component inherited and partially overwritten methods) also special Methods are available that allow adding, managing, and removing Enable components.

The most important container class methods are:

public Component add(Component comp)

adds the comp component to the container.

public Component add(Component comp, int index)

adds the comp component to the container. In doing so, index sets the Insert index in the list of inserted components.

public void add(Component comp, Object constraints)

adds in compliance with the layout condition specified in constraints add the comp Java component to the container.

public Component [] getComponents()

supplies a list of all inserted components as a field with component type component.

public void remove(Component comp)

removes the comp component from the container.

public void setLayout(LayoutManager mgr)

sets the layout of the container.

The Java components of a container are kept in a list, with the Order of the list elements by default by the order of the added Calls is set. This list is also used to arrange the Java components according to the specified layout. With the second variant of the method, add can change the position of the component in relation to the selected layout can be specified.

The setLayout method expects a layout manager. It is about an object of a class that implements the LayoutManager interface. There we also find out how the variant of the add method with layout condition is used.

abstract class Java JComponent:

The abstract class JComponent serves as the base class for all Swing components with the exception of the top-level container. According to the class, hierarchy inherits JComponent from Container (and thus from the component) and fits by overwriting some of the inherited methods for their purposes. Furthermore, some methods are made available which are specially designed for swing Java components are important.


The most important JComponents methods are:

public boolean isOpaque()

returns true if the component has an opaque background, otherwise false.

public void setOpaque(boolean b)

switches the background of the component opaque (if b is the value has true) or transparent (if b has the value false).

public string getToolTipText()

supplies the current tooltip text of the component.

public void setToolTipText(String text)

defines text as tooltip text for the component.

Some Swing Java components (e.g. JLabel) come standard with a transparent one Background equipped. For example, if you change the background color of a label, one finds that this has no effect if one does not at the same time ensure that the label has the status “opaque” or non-transparent.

Each Swing component can be equipped with a tooltip. It acts it is a note that is intended for the user of a graphical interface is always displayed when he or she for a short Time lingers over the component with the mouse pointer. This makes it possible to automatically provide users with assistance on the functionality of components to offer. For example, consider a small modification of our Label sample program.

Exmaple java component: How to create ToolTips in java using swing Java JComponent:

Java Component



Related Article:

Java Hello World First Program And Java Vm Error Fixing

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: