Krantik Chavan has Published 308 Articles

How to change font size with HTML in Java Swing JEditorPane?

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

699 Views

Use HTMLEditorKitt to change the font size with HTML. With that, use the JEditorPane setText() method to set HTML:HTMLEditorKit kit = new HTMLEditorKit(); editorPane.setEditorKit(kit); editorPane.setSize(size); editorPane.setOpaque(true); editorPane.setText(" This is a demo text with a different font!");The following is an example to change font size with HTML in Java Swing JEditorPane:Exampleimport ... Read More

LocalDate minusYears() Method in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

565 Views

An immutable copy of the LocalDate where the years are subtracted from it can be obtained using the minusYears() method in the LocalDate class in Java. This method requires a single parameter i.e. the number of years to be subtracted and it returns the instant with the subtracted years.A program ... Read More

The addAll() method of Java AbstractCollection class

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

83 Views

The addAll() method of the AbstractCollection class in Java is used to add all of elements in the specified collection to this collection. It returns TRUE if the elements are successfully appendedThe syntax is as follows:public boolean addAll(Collection

How to approach String as int stream in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

84 Views

Let’s say we have the following string:String str = "YuBM787Nm";Now to display it as IntStream, use filter() and map() as shown below:int res = str.chars() .filter(Character::isDigit) .map(ch → Character.valueOf((char) ch)).sum();The following is an example to display string as IntStream:Examplepublic class Demo {    public static void main(String[] args) {   ... Read More

LocalDate getDayOfWeek() method in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

154 Views

The day of the week for a particular LocalDate can be obtained using the getDayOfWeek() method in the LocalDate class in Java. This method requires no parameters and it returns the day of the week.A program that demonstrates this is given as followsExample Live Demoimport java.time.*; public class Demo {   ... Read More

Java Program to display a webpage in JEditorPane

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

505 Views

Use the setPage() method of the JEditorPane to display a webpage:JEditorPane editorPane = new JEditorPane(); editorPane.setPage("https://www.tutorialspoint.com");The following is an example to display a webpage in JEditorPane:Exampleimport java.io.IOException; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String[] args) {       JEditorPane editorPane = ... Read More

How to sort array of strings by their lengths following longest to shortest pattern in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

248 Views

At first, let us create and array of strings:String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" }Now, for longest to shortest pattern, for example ABCDEFGHIJ, ABCDEFG, ABCDEF, etc.; get the length of both the string arrays and work them like this:Arrays.sort(strArr, (str1, str2) → str2.length() - ... Read More

Java Program to get the reverse of an Integer array with Lambda Expressions

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

418 Views

Let us first declare and initialize an Integer array:Integer[] arr = {20, 50, 75, 100, 120, 150, 170, 200};To find the reverse of the Integer array with Lambda, use sort and the Lamda:Arrays.sort(arr, (Integer one, Integer two) -> { return (two- one); });The following is an example to reverse an Integer ... Read More

Java Program to check if any String in the list starts with a letter

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

4K+ Views

First, create a List with String elements:List myList = new ArrayList(); myList.add("pqr"); myList.add("stu"); myList.add("vwx"); myList.add("yza"); myList.add("bcd"); myList.add("efg"); myList.add("vwxy");Use the startsWith() method to check if any of the above string in the myList begins with a specific letter:myList.stream().anyMatch((a) -> a.startsWith("v"));TRUE is returned if any of the string begins with the specific ... Read More

LocalDate getDayOfMonth() method in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:25

424 Views

The day of the month for a particular LocalDate can be obtained using the getDayOfMonth() method in the LocalDate class in Java. This method requires no parameters and it returns the day of the month which can be in the range of 1 to 31.A program that demonstrates this is ... Read More

Advertisements