Found 2616 Articles for Java

Why AWT components are heavy-weight while Swing components are light-weight in Java?

raja
Updated on 06-Feb-2020 10:46:30

2K+ Views

AWT stands for Abstract Window ToolKit and it supports Java GUI programming. It is a portable GUI library for Stand-alone Java applications/applets. The AWT provides the connection between our application and the native GUI while Java Swing implements a set of GUI components that build on AWT technology and it can provide a pluggable look and feel. Java Swing is implemented entirely in the Java programming language.First of all, by a heavy-weight, it means the code will take comparatively more time to load and it will consume more System resources. AWT is considered to be heavy-weight because its components are dependent ... Read More

Java Program to set the extent in JSlider

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

167 Views

To set the extent of the slider, use the setExtent() method. It sets the size of the range covered by the knob −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70); slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setExtent(20);The following is an example to set the extent in JSlider −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame with Slider");       JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70);       slider.setMinorTickSpacing(5);       slider.setMajorTickSpacing(20);       slider.setPaintTicks(true);   ... Read More

How to use the SimpleDateFormat class to convert a Java Date to a formatted String?

raja
Updated on 21-Nov-2023 16:11:34

115 Views

The Java SimpleDateFormat class provides convert between a Java String to a Date or Date to String. Example import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendar; public class SimpleDateFormatTest { public static void main(String[] args) { // get today's date Date today = Calendar.getInstance().getTime(); // create a date "formatter" SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss"); // create a new String using the date format String formattedDate = formatter.format(today); // prints converted date to string format System.out.println(" Date is:" + formattedDate); } } Output Date is:2023-11-21-10.39.16

What are the differences between an application and an applet in Java?

raja
Updated on 06-Feb-2020 10:13:32

1K+ Views

A Java program can be classified into two types, one is an Application and another is an Applet.ApplicationAn application is a stand-alone java program that runs with the support of a virtual machine in a client or server-side.A java application is designed to perform a specific function to run on any Java-compatible virtual machine regardless of the computer architecture.An application is either executed for the user or for some other application program.Examples of java applications include database programs, development tools, word processors, text and image editing programs, spreadsheets, web browsers, etc.Examplepublic class Demo {    public static void main(String args[]) ... Read More

How to resolve a NullPointerException in Java?

raja
Updated on 21-Nov-2023 16:37:50

2K+ Views

A NullPointerException is a runtime exception thrown by the JVM when our application code, other referenced API or the middleware encounters the following conditions. Attempting to invoke an instance method of a null object. Attempting to access or modify a particular field of a null object. Attempting to obtain the length of a null object as an array. Steps to resolve NullPointerException Review the java.lang.NullPointerException stack trace and determine where the Exception is triggered (Application code, third-party API, middleware software and extract the line). If the problem is at the application code then a code walk-through will be required. If the problem is found from third-party ... Read More

What are the differences between printStackTrace() method and getMessage() method in Java?

raja
Updated on 17-Nov-2023 14:35:43

4K+ Views

There are two ways to find the details of the exception, one is the printStackTrace() method and another is the getMessage() method. printStackTrace() method This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class. This method will display the name of the exception and nature of the message and line number where an exception has occurred. Example public class PrintStackTraceMethod { public static void main(String[] args) { try { int a[]= new int[5]; ... Read More

What are the differences between StackOverflowError and OutOfMemoryError in Java?

raja
Updated on 06-Feb-2020 10:17:07

814 Views

Whenever we run a java program, the operating system allocates some memory to JVM. JVM divides this memory into two parts. One is Stack memory and another is Heap memory. A stack is used for the execution of methods and heap is used to store the objects. When the Stack becomes full, JVM throws java.lang.StackOverflowError and when the heap becomes full, JVM throws java.lang.OutOfMemoryError.StackOverflowErrorA stack is used for the execution of methods. For every method call, one block is created in the stack memoryThe data related to the method like parameters, local variables or references to objects are stored in ... Read More

Can we have a return statement in the catch or, finally blocks in Java?

raja
Updated on 17-Nov-2023 16:33:50

12K+ Views

Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions. If we return a value in the catch block and we can return a value at the end of the method, the code will execute successfully. If we return a value in the catch block and we can write a statement at the end of the method after return a value, the code will not execute so it became unreachable ... Read More

Can we have a try block without a catch block in Java?

raja
Updated on 21-Nov-2023 10:08:40

26K+ Views

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.exit() it will execute always. Example 1 public class TryBlockWithoutCatch { public static void main(String[] args) { try { System.out.println("Try Block"); } finally { System.out.println("Finally Block"); } } } Output Try Block Finally Block A ... Read More

What are the differences between an Exception class and an Error class in Java?

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

589 Views

Both an Exception class and an Error class are subclasses of java.lang.Throwable class, we can able to handle the exceptions at runtime but the errors we cannot handle.Exceptions are the objects representing the logical errors that occur at the run time and makes JVM enters into the state of "ambiguity".The objects which are automatically created by the JVM for representing these run time errors are known as an Exception. An Error is a subclass of Throwable class that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.If an exception occurs we ... Read More

Advertisements