Java Programming

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 Java 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 Java component class.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

The container class:

Containers are special Java components that contain other Java 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 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 Java 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 Java 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.

The abstract class Java JComponent:

The abstract class JComponent serves as the base class for all Swing Java components with the exception of the top-level container. According to the class hierarchy inherits JComponent from Container (and thus from Java 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 of them are:

public boolean isOpaque()

returns true if the Java 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 Java component.

public void setToolTipText(String text)

defines text as tooltip text for the Java 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 Java 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 Java component with the mouse pointer. This makes it possible automatically provides users with assistance on the functionality of Java components to offer. For example, consider a small modification of ours Label sample program

Java Component

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button