Found 112 Articles for AWT

How to select one item at a time from JCheckBox in Java?

raja
Updated on 07-Feb-2020 11:41:15

1K+ Views

JCheckBoxA JCheckBox can extend JToggleButton and it can be a small box that is either checked or unchecked.When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically.A JCheckBox can generate an ActionListener or an ItemListener whenever the checkbox is changed.An isSelected() method is used to test if a checkbox is checked or not.By default, we can select all the checkbox items at a time, if we want to select only one item at a time by using ButtonGroup class.Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*; public class JCheckBoxGroupTest extends JFrame {    private ButtonGroup checkBoxGroup;   ... Read More

How can we add new tabs to JTabbedPane from a JMenu in Java?

raja
Updated on 07-Feb-2020 11:42:24

240 Views

JTabbedPaneA JTabbedPane is a component can extend JComponent class and it can provide easy access to more than one panel.Each tab is associated with a single component that can be displayed when the tab is selected.A JTabbedPane can generate a ChangeListener interface when a tab is selected.The important methods of JTabbedPane are add(), addTab(), fireStateChanged(), createChangeListener(), setSelectedIndex(), getTabCount() and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTabbedPaneTest extends JFrame implements ActionListener {    JTabbedPane tabbedPane;    int ntabs = 0;    public JTabbedPaneTest() {       getContentPane().setLayout(new BorderLayout());       tabbedPane = new JTabbedPane();   ... Read More

How can we implement right click menu using JPopupMenu in Java?

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

1K+ Views

A JPopupMenu appears anywhere on the screen when a right mouse button is clicked.JPopupMenuA popup menu is a free-floating menu which associates with an underlying component called the invoker.Most of the time, a popup menu is linked to a specific component to display context-sensitive choices.In order to create a popup menu, we can use the JPopupMenu class., we can add the JMenuItem to popup menu like a normal menu.To display the popup menu, we can call the show() method, normally popup menu is called in response to a mouse event.Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; public class JPopupMenuTest extends JFrame { ... Read More

What are the differences between a JTextPane and a JEditorPane in Java?

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

1K+ Views

A JTextPane is an extension of JEditorPane which provides word processing features like fonts, text styles, colors and etc. If we need to do heavy-duty text processing we can use this class whereas a JEditorPane supports display/editing of HTML and RTF content and can be extended by creating our own EditorKit.JTextPaneA JTextPane is a subclass of JEditorPane.A JTextPane is used for a styled document with embedded images and components.A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the default model.The important methods of JTextPane are addStyle(), getCharacterAttributes(), getStyledDocument(), setDocument(), setEditorKit(), setStyledDocument() and etc.Exampleimport java.awt.*; import ... Read More

What is the importance of a WindowListener interface in Java?

raja
Updated on 30-Jun-2020 13:32:30

192 Views

The class which process the WindowEvent needs to be implemented this interface and an object of this class can be registered with a component by using addWindowListener() method.Methods of WindowListener InterfaceThe WindowListener interface defines 7 methods for handling window eventsvoid windowActivated(WindowEvent we) − Invoked when a window is activated.void windowDeactivated(WindowEvent we) − Invoked when a window is deactivated.void windowOpened(WindowEvent we) − Invoked when a window is opened.void windowClosed(WindowEvent we) − Invoked when a window is closed.void windowClosing(WindowEvent we) − Invoked when a window is closing.void windowIconified(WindowEvent we) − Invoked when a window minimized.void windowDeiconfied(WindowEvent we) − Invoked when a window is restored.Syntaxpublic ... Read More

What is a LayoutManager and types of LayoutManager in Java?

raja
Updated on 19-Feb-2024 04:20:24

28K+ Views

The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers. Types of LayoutManager There are 6 layout managers in Java FlowLayout: It arranges the components in a container like the words on a page. It fills the top line from left to right and top to bottom. The components are arranged in the order as they are added i.e. first components appears at top left, if the container is not wide enough to display all the components, ... Read More

What is the use of setBounds() method in Java?

raja
Updated on 13-Sep-2023 03:56:20

31K+ Views

The layout managers are used to automatically decide the position and size of the added components. In the absence of a layout manager, the position and size of the components have to be set manually. The setBounds() method is used in such a situation to set the position and size. To specify the position and size of the components manually, the layout manager of the frame can be null.setBounds()The setBounds() method needs four arguments. The first two arguments are x and y coordinates of the top-left corner of the component, the third argument is the width of the component and the fourth argument is the ... Read More

What are the differences between an event listener interface and an event adapter class in Java?

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

4K+ Views

An EventListener interface defines the methods that must be implemented by an event handler for a particular kind of an event whereas an Event Adapter class provides a default implementation of an EventListener interface.Event ListenerThe Event Listeners are the backbone of every component to handle the events.Every method of a particular EventListener will have a single parameter as an instance which is the subclass of EventObject class.An EventListener interface needs to be extended and it will be defined in java.util package.A few EventListener interfaces are ActionListener, KeyListener, MouseListener, FocusListener, ItemListener and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyListenerTest implements KeyListener, ActionListener {    JFrame frame;    JTextField tf;   ... Read More

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

Advertisements