Found 112 Articles for AWT

What are the differences between a MouseListener and a MouseMotionListener in Java?

raja
Updated on 07-Feb-2020 07:22:45

897 Views

We can implement a MouseListener interface when the mouse is stable while handling the mouse event whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.Mouse ListenerA MouseEvent is fired when we press, release or click (press followed by release) a mouse button (left or right button) at the source object or position the mouse pointer at (enter) and away (exit) from the source object.A MouseListener interface declares the following five abstract methodsSyntaxpublic void mouseClicked(MouseEvent evt) public void mousePressed(MouseEvent evt) public void mouseReleased(MouseEvent evt) public void mouseEntered(MouseEvent evt) public void mouseExited(MouseEvent evt)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public ... Read More

What are the different types of JOptionPane dialogs in Java?

raja
Updated on 07-Feb-2020 07:28:45

9K+ Views

The JOptionPane is a subclass of JComponent class which includes static methods for creating and customizing modal dialog boxes using a simple code. The JOptionPane is used instead of JDialog to minimize the complexity of the code. The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.JOptionPane class is used to display four types of dialog boxesMessageDialog -  dialog box that displays a message making it possible to add icons to alert the user.ConfirmDialog  -  dialog box that besides sending a message, enables the user to answer a question.InputDialog     ... Read More

What is the importance of SwingUtilities class in Java?

raja
Updated on 07-Feb-2020 07:31:53

947 Views

In Java, after swing components displayed on the screen, they can be operated by only one thread called Event Handling Thread. We can write our code in a separate block and can give this block reference to Event Handling thread. The SwingUtilities class has two important static methods, invokeAndWait() and invokeLater() to use to put references to blocks of code onto the event queue.Syntaxpublic static void invokeAndWait(Runnable doRun) throws InterruptedException, InvocationTargetException public static void invokeLater(Runnable doRun)The parameter doRun is a reference to an instance of Runnable interface. In this case, the Runnable interface will not be passed to the constructor of a Thread. The Runnable interface is simply ... Read More

How can we make JTextField accept only numbers in Java?

raja
Updated on 11-Feb-2020 10:41:56

11K+ Views

By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int.In the below example, JTextField only allows entering numeric values.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextFieldValidation extends JFrame {    JTextField tf;    Container container;    JLabel label;    public JTextFieldValidation() {       container = getContentPane();       setBounds(0, 0, 500, 300);       tf = new JTextField(25);       setLayout(new FlowLayout());       container.add(new JLabel("Enter the number"));   ... Read More

Explain the architecture of Java Swing in Java?

raja
Updated on 24-Feb-2020 11:08:53

2K+ Views

Java Swing is a set of APIs that provides a graphical user interface (GUI) for the java programs. The Java Swing was developed based on earlier APIs called Abstract Windows Toolkit (AWT). The Java Swing provides richer and more sophisticated GUI components than AWT. The GUI components are ranging from a simple level to complex tree and table. The Java Swing provides the pluggable look and feels to allow look and feel of Java programs independent from the underlying platform.Features of Java SwingThe Java Swing is platform independent and follows the MVC (Model View and Controller) framework.Pluggable look and feel − The Java ... Read More

What are the differences between JRadioButton and JCheckBox in Java?

raja
Updated on 07-Feb-2020 07:35:53

2K+ Views

Both JRadioButton and JCheckBox components can extend JToggleButton class, the main difference is that JRadioButton is a group of buttons in which only one button can be selected at a time whereas JCheckBox is a group of checkboxes in which multiple items can be selected at a time.JRadioButtonA JRadioButton is a component that represents an item with a state selected or unselected. Usually, a group of radio buttons is created to provide options to the user, but only one option can be selected at a time.JRadioButton will generate an ActionListener, ChangeListener, and ItemListener interfaces.The radio buttons are often used in a group to display multiple options, therefore, they are used ... Read More

What is an Event Handling and describe the components in Event Handling in Java?

raja
Updated on 07-Feb-2020 06:34:45

3K+ Views

The GUI in Java processes the interactions with users via mouse, keyboard and various user controls such as button, checkbox, text field, etc. as the events. These events are to be handled properly to implement Java as an Event-Driven Programming.Components in Event HandlingEventsEvent SourcesEvent Listeners/HandlersEventsThe events are defined as an object that describes a change in the state of a source object.The Java defines a number of such Event Classes inside java.awt.event packageSome of the events are ActionEvent, MouseEvent, KeyEvent, FocusEvent,  ItemEvent and etc.Event SourcesA source is an object that generates an event.An event generation occurs when an internal state of that object ... Read More

What are the differences between JTextField and JTextArea in Java?

raja
Updated on 07-Feb-2020 06:48:30

3K+ Views

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.JTextFieldA JTextFeld is one of the most important components that allow the user to an input text value in a single line format.A JTextField will generate an ActionListener interface when we trying to enter some input inside it.The JTextComponent is a superclass of JTextField that provides a common set of methods used by JTextfield.The important methods in the JTextField class are setText(), getText(), setEnabled(), etc.Exampleimport javax.swing.*; import java.awt.*; public class JTextFieldTest {    public static ... Read More

What are the differences between JFrame and JDialog in Java?

raja
Updated on 07-Feb-2020 06:54:28

1K+ Views

JFrameThe components added to the frame are referred to as its contents, these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane instead.A JFrame contains a window with title, border, (optional) menu bar and user-specified components.A JFrame can be moved, resized, iconified and it is not a subclass of JComponent.By default, JFrame is displayed in the upper-left corner of the screen. To display a frame at a specified location, we can use the setLocation(x, y) method in the JFrame class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JFrameDemo {    public static void main(String s[]) {       ... Read More

What are the differences between GridLayout and GridBagLayout in Java?

raja
Updated on 07-Feb-2020 06:09:22

5K+ Views

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells with each component occupying one or more cells called Component display area.GridLayoutA GridLayout arranges the components in a rectangular grid. It arranges component in the cells and each cell has the same size. Components are placed in columns and rows. GridLayout(int rows, int columns) takes two parameters that are a column and ... Read More

Advertisements