Found 2616 Articles for Java

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

563 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

958 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

How can we call garbage collection (GC) explicitly in Java?

raja
Updated on 30-Jul-2019 22:30:26

1K+ Views

When there are no more references to an object, the object is finalized and when the Garbage Collection starts these finalized objects get collected this will done automatically by the JVM. We can call garbage collection directly but it doesn't guarantee that the GC will start executing immediately.We can call the Garbage Collection explicitly in two waysSystem.gc() methodRuntime.gc() methodThe java.lang.Runtime.freeMemory() method returns the amount of free memory in the Java Virtual Machine (JVM). Calling the gc() method may result in increasing the value returned by the freeMemory.ExampleLive Demopublic class GarbageCollectionTest {    public static void main(String args[]) {       System.out.println(Runtime.getRuntime().freeMemory());   ... Read More

Can we define a parameterized constructor in an abstract class in Java?

raja
Updated on 30-Jul-2019 22:30:26

4K+ Views

Yes, we can define a parameterized constructor in an abstract class.Conditions for defining a parameterized constructor in an abstract classWe need to make sure that the class which is extending an abstract class have a constructor and it can call the superclass parameterized constructor.We can call the superclass parameterized constructor in a subclass by using super() call.If we are not placing super() call in the subclass constructor, a compile-time error will occur.ExampleLive Demoabstract class AbstractClassTest {    AbstractClassTest(int a) { // Parameterized Constructor       System.out.println("Parameterized Constructor of an abstract class a="+ x);    } } public class Test extends AbstractDemo ... Read More

Advertisements