Found 133 Articles for Swing

What is the importance of the Container class in Java?

raja
Updated on 07-Feb-2020 12:50:43

7K+ Views

ContainerA Container class can be described as a special component that can hold the gathering of the components.There are two types of Swing Containers, they are top-level containers and low-level containers.Top-Level containers are heavyweight containers such as JFrame, JApplet, JWindow, and JDialog.Low-Level containers are lightweight containers such as JPanel.The most commonly used containers are JFrame, JPanel and JWindow.The important methods of the Container class are add(), invalidate() and validate().Exampleimport java.awt.*; import javax.swing.*; public class ContainerTest extends JFrame { // top-level container    JPanel panel; // low-level container    JTextField field;    JButton btn;    public ContainerTest() {       setTitle("Container Test");     ... Read More

What is the importance of the CardLayout class in Java?

raja
Updated on 07-Feb-2020 10:53:30

113 Views

The functionality of CardLayout arranges the components in a sequential manner and only one component is visible at one time and each component will be treated as one card.CardLayoutThe CardLayout is different from the other layouts where the other layout managers attempt to display all the components within the container at once, the CardLayout displays only one component at a time.In the CardLayout, cards are usually placed in a container such as a JPanel. The components are placed into the card queue in the order in which they are added.The important methods of CardLayout are first(), last(), next(), previous() and show().Exampleimport java.awt.*; ... Read More

What is the importance of the GridBagConstraints class in Java?

raja
Updated on 07-Feb-2020 10:57:12

2K+ Views

A GridBagLayout is a very flexible layout manager that allows us to position the components relative to one another using constraints. Each GridBagLayout uses a dynamic rectangular grid of cells with each component occupying one or more cells called its display area. Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that specifies how the component is laid out within its display area. GridBagConstraintsWe can customize a GridBagConstraints object by setting one or more of its public instance variables. These variables specify the component location, size, growth factor, anchor, inset, filling, and padding.gridx: An int value that specifies the leftmost cell that the ... Read More

How can we implement the paintComponent() method of a JPanel in Java?

raja
Updated on 07-Feb-2020 11:02:38

5K+ Views

A JPanel is a lightweight container and it is an invisible component in Java. A JPanel's default layout is FlowLayout. After the JPanel has been created, other components can be added to the JPanel object by calling its add() method inherited from the Container class.paintComponent()This method is needed to draw something on JPanel other than drawing the background color. This method already exists in a JPanel class so that we need to use the super declaration to add something to this method and takes Graphics objects as parameters. The super.paintComponent() which represents the normal the paintComponent() method of the JPanel which can only handle the background of the panel must be called in the ... Read More

How can we disable the maximize button of a JFrame in Java?

raja
Updated on 07-Feb-2020 11:00:08

2K+ Views

A JFrame is a class from javax. swing package and it can extend java.awt.frame class. It is a top-level window with a border and a title bar. A JFrame class has many methods that can be used to customize it.After setting the size of a JFrame we can still change the size by putting the cursor at the corners and dragging it or if we press resize option next to close at the top right corner, it will maximize to the size of a full screen. This happens because resize is set to true by default for JFrame class. We can make it ... Read More

How can we implement different borders using the BorderFactory in Java?

raja
Updated on 07-Feb-2020 11:04:23

1K+ Views

The BorderFactory is a Factory class which provides different types of borders in Java.Types of BordersBevelBorder: This border draws raised or lowered beveled edges.EmptyBorder:  It doesn’t do any drawing, but does take up space.EtchedBorder: A Lowered etched border gives an appearance of a rectangle and a Raised etched border looks like a surface of the screen.LineBorder: Draws a simple rectangle around a component. We can specify the color and width of the line in the LineBorder constructor.MatteBorder: We can create a MatteBorder with a certain color and specify the size of the border on the left, top, right, and bottom of the component. A MatteBorder also allows us to ... Read More

How can we implement line wrap and word wrap text inside a JTextArea in Java?

raja
Updated on 07-Feb-2020 11:06:02

647 Views

A JTextArea is a multi-line text component to display text or allow the user to enter the text and it will generate a CaretListener interface when we are trying to implement the functionality of the JTextArea component. A JTextArea class inherits the JTextComponent class in Java.In the below example, we can implement a JTextArea class with a user can select either word wrap or line wrap checkboxes using the ItemListener interface.Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*; public class JTextAreaTest {    public static void main(String[] args ) {       EventQueue.invokeLater(new Runnable() {          @Override          public void run() {   ... Read More

What are the differences between a JScrollBar and a JScrollPane in Java?

raja
Updated on 07-Feb-2020 11:09:46

2K+ Views

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling. A JScrollBar cannot have a JScrollPane whereas a JScrollPane can have a JScrollBar.JScrollBarThe object of the JScrollBar class is used to add horizontal and vertical scrollbar which allow the user to select items between a specified minimum and maximum values.A JScrollBar class is an implementation of a scrollbar and inherits the JComponent class.Syntaxpublic class JScrollBar extends JComponent implements Adjustable, AccessibleExampleimport javax.swing.*; import java.awt.*; public class JScrollBarTest extends JFrame{    JScrollBarTest() {       setTitle("JScrollBar Test");       JScrollBar jsb = new JScrollBar();   ... Read More

How many types of JDialog boxes can be created in Java?

raja
Updated on 07-Feb-2020 11:11:20

102 Views

A JDialog is a subclass of Dialog class and it does not hold minimize and maximize buttons at the top right corner of the window. We can create two types of JDialog boxes.in JavaModal DialogNon-Modal DialogModal JDialogIn Java, When a modal dialog window is active, all the user inputs are directed to it and the other parts of the application are inaccessible until this model dialog is closed.Non-Modal JDialogIn Java, When a non-modal dialog window is active, the other parts of the application are still accessible as normal and inputs can be directed to them, while this non-modal dialog window doesn't need to be ... Read More

How can we implement a JToggleButton in Java?

raja
Updated on 07-Feb-2020 11:13:52

664 Views

JToggleButtonA JToggleButton is an extension of AbstractButton and it can be used to represent buttons that can be toggled ON and OFF.When JToggleButton is pressed for the first time, it remains pressed and it can be released only when it is pressed for the second time.A JToggleButton generates an ActionEvent each time it is pressed.A JToggleButton can also generate an ItemEvent, this event is used by those components that support the concept of selection. When a JToggleButton is pressed in, it is selected. When it is popped out, it is deselected.To handle item events, you must implement the ItemListener interface. This interface defines the itemStateChanged( ) method that is ... Read More

Advertisements