package org.kodejava.util;
import java.util.LinkedList;
import java.util.List;
public class LinkedListToArray {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Blue");
list.add("Green");
list.add("Purple");
list.add("Orange");
// Converting LinkedList to array can be done by calling the toArray()
// method of the List;
String[] colors = new String[list.size()];
list.toArray(colors);
for (String color : colors) {
System.out.println("color = " + color);
}
}
}
How do I convert an XML persistence to Java Bean?
In the previous example you can see how to convert a bean into an XML persistence. Now we’ll do the opposite, converting the XML back to a bean. For the BeanToXML class use in this example please refer to How do I convert a bean to XML persistence? example.
package org.kodejava.bean;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.util.Objects;
public class XmlToBean {
public static void main(String[] args) {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
Objects.requireNonNull(XmlToBean.class.getResourceAsStream("/Bean.xml"))));
// Reads the next object from the underlying input stream.
BeanToXML bean = (BeanToXML) decoder.readObject();
decoder.close();
System.out.println("ID = " + bean.getId());
System.out.println("Item Name = " + bean.getItemName());
System.out.println("Item Colour = " + bean.getItemColour());
System.out.println("Item Quantities = " + bean.getItemQuantities());
}
}
Here is our Bean.xml persistence file:
<?xml version="1.0" encoding="UTF-8"?>
<java version="17" class="java.beans.XMLDecoder">
<object class="org.kodejava.bean.BeanToXML">
<void property="id">
<long>1</long>
</void>
<void property="itemColour">
<string>Dark Red</string>
</void>
<void property="itemName">
<string>T-Shirt</string>
</void>
<void property="itemQuantities">
<int>100</int>
</void>
</object>
</java>
The result are:
ID = 1
Item Name = T-Shirt
Item Colour = Dark Red
Item Quantities = 100
How do I convert a bean to XML persistence?
package org.kodejava.bean;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXML {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
public static void main(String[] args) {
BeanToXML bean = new BeanToXML();
bean.setId(1L);
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(100);
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("Bean.xml")));
// Write an XML representation of the specified object to the output.
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
The XML persistence will be like:
<?xml version="1.0" encoding="UTF-8"?>
<java version="17" class="java.beans.XMLDecoder">
<object class="org.kodejava.bean.BeanToXML">
<void property="id">
<long>1</long>
</void>
<void property="itemColour">
<string>Dark Red</string>
</void>
<void property="itemName">
<string>T-Shirt</string>
</void>
<void property="itemQuantities">
<int>100</int>
</void>
</object>
</java>
How do I create a multiline tool tips in Swing?
package org.kodejava.swing;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
public class MultilineToolTip {
public static void main(String[] args) {
JFrame frame = new JFrame("Tool Tip Demo");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hover on me!");
// Setting tool tip for our Swing JLabel component using an html
// formatted string so that we can create a multi lines tool tip.
label.setToolTipText(
"<html>Lorem Ipsum is simply dummy text of the printing and<br/>" +
"typesetting industry. Lorem Ipsum has been the industry's <br/>" +
"standard dummy text ever since the 1500s, when an unknown<br/>" +
"printer took a galley of type and scrambled it to make a<br/>" +
"type specimen book.</html>");
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
How do I disable/enable application tool tips?
package org.kodejava.swing;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
public class DisableToolTip extends JFrame {
public DisableToolTip() throws HeadlessException {
initComponent();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new DisableToolTip().setVisible(true));
}
private void initComponent() {
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
JButton disable = new JButton("DISABLE");
disable.setToolTipText("Application tool tip will be disabled.");
disable.addActionListener(e -> {
// Disable tool tip for the entire application
ToolTipManager.sharedInstance().setEnabled(false);
});
JButton enable = new JButton("ENABLE");
enable.setToolTipText("Application tool tip will be enabled.");
enable.addActionListener(e -> {
// Enable tool tip for the entire application
ToolTipManager.sharedInstance().setEnabled(true);
});
getContentPane().add(enable);
getContentPane().add(disable);
}
}
How do I set a tool tip for Swing components?
package org.kodejava.swing;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
public class ToolTipExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Tool Tip Demo");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hover on me!");
// Setting tool tip for our Swing JLabel component
label.setToolTipText("My JLabel Tool Tip");
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
How do I create an undecorated JFrame?
This code give you an example of how to create a frame without the title bar, and the frame icons such as maximize, minimize and close.
package org.kodejava.swing;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class UndecoratedFrame {
private static Point point = new Point();
public static void main(String[] args) {
final JFrame frame = new JFrame();
// Disables or enables decorations for this frame. By setting undecorated
// to true will remove the frame's title bar including the maximize,
// minimize and the close icon.
frame.setUndecorated(true);
// As the the frame's title bar removed we need to close out frame for
// instance using our own button.
JButton button = new JButton("Close Me");
button.addActionListener(e -> System.exit(0));
// The mouse listener and mouse motion listener we add here is to simply
// make our frame draggable.
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
}
});
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(button, BorderLayout.NORTH);
frame.getContentPane().add(new JLabel("Drag Me", JLabel.CENTER),
BorderLayout.CENTER);
frame.setVisible(true);
}
}
How do I get system functions supported by database?
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class SystemFunction {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData meta = connection.getMetaData();
// Get system functions supported by database
String[] functions = meta.getSystemFunctions().split(",\\s*");
for (String function : functions) {
System.out.println("Function = " + function);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Here are MySQL database supported system functions.
Function = DATABASE
Function = USER
Function = SYSTEM_USER
Function = SESSION_USER
Function = PASSWORD
Function = ENCRYPT
Function = LAST_INSERT_ID
Function = VERSION
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
How do I get date time functions supported by database?
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class DateTimeFunction {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData meta = connection.getMetaData();
// Get date and time functions supported by database
String[] functions = meta.getTimeDateFunctions().split(",\\s*");
for (String function : functions) {
System.out.println("Function = " + function);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Date and time functions supported by MySQL database.
Function = DAYOFWEEK
Function = WEEKDAY
Function = DAYOFMONTH
Function = DAYOFYEAR
Function = MONTH
Function = DAYNAME
Function = MONTHNAME
Function = QUARTER
Function = WEEK
Function = YEAR
Function = HOUR
Function = MINUTE
Function = SECOND
Function = PERIOD_ADD
Function = PERIOD_DIFF
Function = TO_DAYS
Function = FROM_DAYS
Function = DATE_FORMAT
Function = TIME_FORMAT
Function = CURDATE
Function = CURRENT_DATE
Function = CURTIME
Function = CURRENT_TIME
Function = NOW
Function = SYSDATE
Function = CURRENT_TIMESTAMP
Function = UNIX_TIMESTAMP
Function = FROM_UNIXTIME
Function = SEC_TO_TIME
Function = TIME_TO_SEC
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
How do I get numeric functions supported by database?
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class NumericFunction {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData meta = connection.getMetaData();
// Get numeric functions supported by database
String[] functions = meta.getNumericFunctions().split(",\\s*");
for (String function : functions) {
System.out.println("Function = " + function);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Here are the numeric functions supported by MySQL database.
Function = ABS
Function = ACOS
Function = ASIN
Function = ATAN
Function = ATAN2
Function = BIT_COUNT
Function = CEILING
Function = COS
Function = COT
Function = DEGREES
Function = EXP
Function = FLOOR
Function = LOG
Function = LOG10
Function = MAX
Function = MIN
Function = MOD
Function = PI
Function = POW
Function = POWER
Function = RADIANS
Function = RAND
Function = ROUND
Function = SIN
Function = SQRT
Function = TAN
Function = TRUNCATE
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>




