Found 112 Articles for AWT

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

640 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 can we implement a JToggleButton in Java?

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

649 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

How can we limit the number of characters inside a JTextField in Java?

raja
Updated on 07-Feb-2020 11:17:14

3K+ Views

A JTextFeld is one of the most important components that allow the user to an input text value in a single line format. We can restrict the number of characters that the user can enter into a JTextField can be achieved by using a PlainDocument class.In the below example, we can implement the logic by using a PlainDocument class, hence we can allow a user to enter a maximum of 10 characters, it doesn't allow if we enter more than 10 characters.Exampleimport java.awt.*; import javax.swing.*; import javax.swing.text.*; class JTextFieldLimit extends PlainDocument {    private int limit;    JTextFieldLimit(int limit) {       super();   ... Read More

What are the differences between a JComboBox and a JList in Java?

raja
Updated on 07-Feb-2020 07:12:40

2K+ Views

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.JComboBoxA JComboBox can be editable or read-only.An ActionListener, ChangeListener or ItemListener interfaces can be used to handle the user actions on a JComboBox.A getSelectedItem() method can be used to get the selected or entered item from a combo box.A setEditable() method can be used to turn on or turn off the text input part of a combo box.We can create a ... Read More

What is the importance of a SwingWorker class in Java?

raja
Updated on 11-Feb-2020 10:36:25

513 Views

A SwingWorker class enables us to perform an asynchronous task in a worker thread (such as a long-running task) then update Swing components from the Event Dispatch Thread (EDT) based on the task results. It was introduced in Java 1.6 Version.SwingWorker classThe java.swing.SwingWorker class is a task worker, which performs time-consuming tasks in the background.A SwingWorker instance interacts with 3 threads, Current thread, the Worker thread, and the Event Dispatch thread(EDT).The Current thread calls the execute() method to kick off the task to the background and returns immediately.The Worker thread executes our own version of the doInBackground() method continuously in the background.The Event Dispatch Thread (EDT) wakes up from time to ... Read More

Advertisements