Krantik Chavan has Published 278 Articles

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

Krantik Chavan

Krantik Chavan

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

896 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

811 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

117 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

123 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

198 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

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

315 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 check if any String in the list starts with a letter

Krantik Chavan

Krantik Chavan

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

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

627 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

How to add items in a JComboBox on runtime in Java

Krantik Chavan

Krantik Chavan

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

2K+ Views

The following is an example to add items on runtime on a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       ... Read More

How to handle action event for JComboBox in Java?

Krantik Chavan

Krantik Chavan

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

816 Views

The following is an example to handle action event for JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ... Read More

Advertisements