Found 2616 Articles for Java

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

How do we close resources automatically in Java?

Smita Kapse
Updated on 07-Feb-2020 06:45:19

426 Views

You can close resources automatically using the try-with-resources in JDBC.Syntaxtry(Declaration of resource){    body..... } catch (SQLException e) {    e.printStackTrace(); }It is a try statement with one or more resources declared at try. where resource is an object which should be closed once it is no more required.You can declare multiple resources in this and all those will be closed at the end of the statement automatically.The objects/resources we declare in this should implement java.lang.AutoCloseable or java.io.Closeable, interfaces or, extend the java.lang.AutoCloseable class.In JDBC we can use java.sql.CallableStatement, Connection, PreparedStatement, Statement, ResultSet, and RowSet in try-with-resources statement.ExampleLet us create ... Read More

How to convert a CLOB type to String in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

12K+ Views

CLOB stands for Character Large Object in general, an SQL Clob is a built-in datatype and is used to store large amount of textual data. Using this datatype, you can store data up to 2, 147, 483, 647 characters.The java.sql.Clob interface of the JDBC API represents the CLOB datatype. Since the Clob object in JDBC is implemented using an SQL locator, it holds a logical pointer to the SQL CLOB (not the data).MySQL database provides support for this data type using four variables namely, TINYTEXT, TEXT, MEDIUMTEXT and, LONGTEXT.To convert CLOB data type to stringRetrieve the Clob value from a ... 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

How to set local date/time in a table using LocalDateTime class in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

3K+ Views

The java.time package of Java8 provides a class named LocalDateTime is used to get the current value of local date and time. Using this in addition to date and time values you can also get other date and time fields, such as day-of-year, day-of-week and week-of-year.Setting the Local time to a columnTo set the local date and time value to a column in a table −Obtain the LocalDateTime object − You can obtain the LocalDateTime object by invoking the static method now() as −//Getting the LocalDateTime object LocalDateTime localDateTime = LocalDateTime.now();Get the LocalDate and LocalTime objects from the above obtained LocalDateTime ... Read More

How can we implement a splash screen using JWindow in Java?

raja
Updated on 07-Feb-2020 06:51:51

488 Views

A JWindow is a container that can be displayed anywhere on the user desktop. It does not have the title bar, window management buttons,  etc like a JFrame.The JWindow contains a JRootPane as its only child class. The contentPane can be the parent of any children of the JWindow. Like a JFrame, a JWindow is another top-level container and it is as an undecorated JFrame. It does not have features like a title bar, windows menu, etc. A JWindow can be used as a splash screen window that displays once when the application is launched and then automatically disappears after a few seconds.Exampleimport javax.swing.*; import java.awt.*; public class ... Read More

What are the differences between JFrame and JDialog in Java?

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

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

How many ways are there to register a driver in Java?

Smita Kapse
Updated on 07-Feb-2020 07:01:31

1K+ Views

To connect with a database using JDBC you need to select get the driver for the respective database and register the driver. You can register a database driver in two ways −Using Class.forName() method − The forName() method of the class named Class accepts a class name as a String parameter and loads it into the memory, Soon the is loaded into the memory it gets registered automatically.Class.forName("com.mysql.jdbc.Driver");ExampleFollowing the JDBC program establishes a connection with the MySQL database. Here, we are trying to register the MySQL driver using the forName() method.import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class RegisterDriverExample { ... 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

How to Write/create a JSON array using Java?

Smita Kapse
Updated on 06-Sep-2023 21:23:55

41K+ Views

A JSON array is an ordered collection of values that are enclosed in square brackets i.e., it begins with '[' and ends with ']'. The values in the arrays are separated by ', ' (comma).Sample JSON array{    "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] }The JSON-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.JSON-Simple maven dependencyFollowing is the maven dependency for the JSON-simple library −           com.googlecode.json-simple       json-simple       ... Read More

Advertisements