Found 133 Articles for Swing

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

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

4K+ 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

532 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

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

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

919 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

1K+ 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

12K+ 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

Advertisements