Found 4336 Articles for Java 8

How to filter String stream and map to lower case in Java? Perform sort as well.

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Let’s say the following is String array, which we have converted to List −Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")Now filter String stream and map to lower case −.stream() .filter(a-> a.startsWith("V")) .map(String::toLowerCase)To sort now, use the sorted() and display using forEach().The following is an example to filter string stream and map to lowercase −Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) throws Exception {       Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")       .stream()       .filter(a-> a.startsWith("V"))       .map(String::toLowerCase)       ... Read More

How to filter String list by starting value in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

Let’s first create a String list −List list = new ArrayList(); list.add("wxy"); list.add("zabc"); list.add("ddd2"); list.add("def"); list.add("ghi"); list.add("wer"); list.add("uij"); list.add("wqy");To filter String list by starting value, use filter() and startsWith() −list.stream().filter((b) -> b.startsWith("w"))The following is an example to filter string list by starting value −Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = new ArrayList();       list.add("wxy");       list.add("zabc");       list.add("ddd2");       list.add("def");       list.add("ghi");       list.add("wer");       list.add("uij");       list.add("wqy");   ... Read More

How to add tooltip to JLabel in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

798 Views

Tooltip is visible whenever you will place the mouse cursor on the label. Use the setToolTipText() method to add tooltip to JLabel −label.setToolTipText("This is a demo tooltip");The following is an example to add tooltip to JLabel −Exampleimport java.awt.Color; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Demo Label!");       label.setFont(new Font("Verdana", Font.PLAIN, 14));       label.setToolTipText("This is a demo tooltip");       Border border = BorderFactory.createLineBorder(Color.ORANGE);     ... Read More

How to add line border to JLabel in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

446 Views

Use the createLineBorder() method to ad line border to JLabel −Border border = BorderFactory.createLineBorder(Color.ORANGE); label.setBorder(border);Above, we have set the line border to color orange.The following is an example to add line border to JLabel −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Demo Label!", JLabel.RIGHT);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       Border border = BorderFactory.createLineBorder(Color.ORANGE);       label.setBorder(border);       frame.add(label);   ... Read More

Java Program to set alignment for text in JLabel

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

6K+ Views

JLabel Left AlignedThe following is an example to set left alignment for JLabel −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Demo");       JLabel label;       label = new JLabel("Left aligned!", JLabel.LEFT);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       frame.add(label);       frame.setSize(500, 300);       frame.setVisible(true);    } }OutputJLabel Center AlignedThe following is an example to set center alignment for JLabel −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) ... Read More

How to create JLabel to hold multiline of text using HTML in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

736 Views

To hold multiline of text, set HTML under JLabel −JLabel = new JLabel("" + "Line1Line2",JLabel.LEFT);The above will create multiline text in the JLabel −Line1 Line2The following is an example to create JLabel to hold multiline of text −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("" + "Line1       Line2",JLabel.LEFT);       label.setBounds(100, 100, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       frame.add(label);       frame.setSize(500,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

How to change text font for JLabel with HTML in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

728 Views

To change text font, you can use the setFont() method of JLabel −label.setFont(new Font("Verdana", Font.PLAIN, 12));The following is an example to change text font for JLabel with HTML −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("" + "ABC");       label.setBounds(50, 50, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 12));       frame.add(label);       frame.setSize(500,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

Java Program to change JLabel text after creation

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

At first, set a text for JLabel −JLabel label; label = new JLabel("First Label");Now change the above JLabel text using setText() −// changing text label.setText("Updated text");Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("First Label");       label.setBounds(50, 50, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       // changing text       label.setText("Updated text");       frame.add(label);       frame.setSize(500,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

How to change JLabel font in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

11K+ Views

To change JLabel font, use the setFont() method −JLabel lable = label.setFont(new Font("Verdana", Font.PLAIN, 18));Examplepackage my; import java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("First Label");       label.setBounds(50, 50, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 18));       frame.add(label);       frame.setSize(300,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

Java Program to draw a line on a JFrame in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

The following is an example to draw a line on a JFrame −Examplepackage my; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JFrame {    public SwingDemo() {       JPanel panel = new JPanel();       getContentPane().add(panel);       setSize(550, 300);    }    public void paint(Graphics gp) { super.paint(gp); Graphics2D graphics = (Graphics2D) gp;       Line2D line = new Line2D.Float(200, 150, 150, 220);       graphics.draw(line);    }    public static void main(String[] args) {       SwingDemo demo = new SwingDemo();       demo.setVisible(true);    } }Output

Advertisements