- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4336 Articles for Java 8
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
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
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
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
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
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
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
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
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
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