JAVA Gpu
JAVA Gpu
Java Swing is a GUI toolkit and a part of JFC (Java Foundation Class) helpful in developing window-
based applications.
Java Swing is lightweight and platform-independent that contains various components and container
classes.
It provides a wide range of components, such as buttons, text fields, and menus, that can be used to create
attractive and functional GUIs.
In Java Swing, there are several components like a scroll bar, button, text field, text area, checkbox, radio
button, etc. These components jointly form a GUI that offers a rich set of functionalities and allows high-
level customization.
Java Swing is a set of graphical user interface (GUI) components that are part of the Java
platform. Swing components are built on top of the Abstract Window Toolkit (AWT), but they provide a
number of advantages over AWT components. For example, Swing components are more lightweight and
efficient, and they are platform-independent.
To install the JDK, go to the Oracle website and download the latest version of the JDK for your
operating system. Once the JDK is installed, you can start creating Swing GUIs.
Platform Independent
Customizable
Extensible
Configurable
Lightweight
JFC is an abbreviation for Java Foundation classes, which encompass a group of features for
building Graphical User Interfaces(GUI) and adding rich graphical functionalities and interactivity to Java
applications.
Features of JFC
JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout.
JPanel is descended directly from JComponent.
JFrame : JFrame is Swing's version of Frame and is descended directly from Frame class. The
component which is added to the Frame, is refered as its Content.
JWindow : This is Swing's version of Window and hass descended directly from Window class.
Like Window it uses BorderLayout by default.
JLabel : JLabel descended from Jcomponent, and is used to create text labels.
JButton : JButton class provides the functioning of push button. JButton allows an icon, string or both
associated with a button.
Creating a JFrame
Swing components
Swing provides a wide range of components that can be used to create GUIs. Swing Framework contains
a large set of components which provide rich functionalities and allow high level of customization. All
these components are lightweight components. They all are derived from JComponent class.Some of the
most commonly used components include:
Buttons
Text fields
Labels
Menus
Panels
Layout managers
JButton
JButton class provides functionality of a button. The button can include some display text or
images. It yields an event when clicked and double-clicked. JButton class has three constuctors,
JButton(Icon ic)
JButton(String str)
JButton(String str, Icon ic)
It allows a button to be created using icon, a string or both. JButton supports ActionEvent. When a button
is pressed an ActionEvent is generated.
Syntax:
EXAMPLE:
import javax.swing.*;
b.setBounds(85,110,105,50);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
} }
JLabel
JLabel is a Java Swing component. JLabel can show text, images, or a combination of the two. JLabel does not
respond to input events like mouse or keyboard focus.
Syntax:
Exаmple:
import javax.swing.*;
class Main
JLabel label1,label2;
label1.setBounds(50,50, 100,30);
label2.setBounds(50,100, 100,30);
myframe.add(label1); myframe.add(label2);
myframe.setSize(300,300);
myframe.setLayout(null);
myframe.setVisible(true); } }
JTextField
The JTextField renders an editable single-line text box. Users can input non-formatted text in the
box. We can initialize the text field by calling its constructor and passing an optional integer parameter.
This parameter sets the box width measured by the number of columns. Also, it does not limit the number
of characters that can be input into the box. It has three constructors:
Syntax:
Exаmple:
import javax.swing.*;
class Main
JTextField text1,text2;
text1.setBounds(50,100, 200,30);
text2.setBounds(50,150, 200,30);
myframe.add(text1); myframe.add(text2);
myframe.setSize(400,400);
myframe.setLayout(null);
myframe.setVisible(true);
} }
JCheckBox:
The JCheckBox renders a check-box with a label. The check-box has two states, i.e., on and off. On
selecting, the state is set to "on," and a small tick is displayed inside the box.
Syntax:
Constructors of JCheckBox
JCheckBox() - Initially, an unselected check box button is created with no text or icon.
JCheckBox(Action a) - This constructor creates a checkbox where the properties are taken from the
Action supplied.
JCheckBox(Icon icon) - This constructor initially creates an unselected checkbox with an icon.
JCheckBox(Icon icon, boolean selected) - It creates a checkbox with an icon that specifies if it is
initially selected or unselected.
JCheckBox(String text) - This constructor creates an unselected checkbox with the text.
JCheckBox(String text, boolean selected) - A checkbox with text is created and specifies if it is initially
selected or unselected.
JCheckBox(String text, Icon icon) - An unselected checkbox is created with the specified icon and text.
JCheckBox(String text, Icon icon, boolean selected) - A checkbox is created with text and icon and
specifies if it is initially selected or unselected.
Exаmple:
import java.awt.*;
import javax.swing.*;
static JFrame f;
public static void main(String[] args)
f.setLayout(new FlowLayout());
p.add(c1);
p.add(c2);
f.add(p);
f.setSize(400, 400);
f.show();
}}
JRadioButton
Radio button is a group of related button in which only one can be selected. JRadioButton class is
used to create a radio button in Frames.
Syntax:
Exаmple:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public Test()
{
JRadioButton jcb = new JRadioButton("A"); //creating JRadioButton.
add(jcb);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new Test();
}}
JComboBox
Combo box is a combination of text fields and drop-down list.JComboBox component is used to
create a combo box in Swing.
Syntax:
Exаmple:
import javax.swing.*;
ComboBoxExample(){
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
new ComboBoxExample();
}}
JTabbedPane
The JTabbedPane is another beneficial component that lets the user switch between tabs in an application.
It is a handy utility as it allows users to browse more content without navigating to different pages.
Syntax:
The above code creates a two-tabbed panel with headings Tab_1 and Tab_2.
JMenuBar, JMenu and JMenuItem
In Java, the Swing toolkit contains a JMenuBar, JMenu, and JMenuItem class. It is under package
javax.swing.JMenuBar, javax.swing.JMenu and javax.swing.JMenuItem class. The JMenuBar class is
used for displaying menubar on the frame. The JMenu Object is used to pull down the menu bar's
components. The JMenuItem Object is used for adding the labeled menu item.
JToggleButton
A JToggleButton is a button with two states. Selected and unselected are the two statuses. This
class is subclassed by the JRadioButton and JCheckBox classes. The toggle button toggles between being
pressed and unpressed when the user presses it. JToggleButton is a button that allows you to choose from
a list of options.
Constructors
JToggleButton(): Creates a toggle button with no text or image that is initially unselected.
JToggleButton(Action a): This method creates a toggle button with properties based on the Action.
JToggleButton(Icon icon): Creates a toggle button with the provided image but no text that is initially
unselected.
JToggleButton(Icon icon, boolean selected): Creates a toggle button with the provided image and
selection status, but no text.
JToggleButton(String text): Creates an unselected toggle button with the provided text.
JToggleButton(String text, boolean selected): Creates a toggle button with the supplied text and
selection status.
JToggleButton(String text, Icon icon): This method creates an unselected toggle button with the
provided text and image.
JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the supplied text,
image, and selection status.
JAVA JSCROLLPANE
A JscrollPane is used to make scrollable view of a component. When screen size is limited, we use a
scroll pane to display a large component or a component whose size can change dynamically.
Constructors
JScrollPane()
JScrollPane(Component)
JScrollPane(int, int)
JList
JList in Java is one of the Java Swing components. These components include buttons, sliders, list
boxes, checkboxes, etc. JList is defined in Java in the ‘java.swing’ package. JList is a component that
shows a list of objects and allows the user to choose one or more of them. JList is inherited from the
JComponent class. JComponent is an abstract class that serves as the foundation for all Swing
components.
Class Declaration
JList(ary[] listData) Creates a JList that displays the elements in the specified array.
JList(ListModel<ary> dataModel) Creates a JList that displays elements from the specified, non-
null, model.
JDialog
The JDialog control represents a top level window with a border and a title used to take some
form of input from the user. It inherits the Dialog class.
Constructors:
JDialog() - It is used to create a modeless dialog without a title and without a specified Frame
owner.
JDialog(Frame owner) - It is used to create a modeless dialog with specified Frame as its owner
and an empty title.
JDialog(Frame owner, String title, boolean modal) - It is used to create a dialog with the specified
title, owner Frame and modality.