Found 4335 Articles for Java 8

Create JList and always display the scroll bar in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

356 Views

To always display the scroll bar in a JList, use the properties HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS:JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);The following is an example to create JList and display the scroll bar:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = new JList(sports);       ... Read More

Java Program to check if the second item is selected in Java JList

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

84 Views

To check if the second item is selected i.e. index 1, use the method isSelectedIndex():list.isSelectedIndex(1);Above, we have set the list with string values:String sports[]= { "Squash", "Fencing", "Cricket", "Football", "Hockey", "Rugby"}; JList list = new JList(sports);The following is an example to check if the second item is selected in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = ... Read More

How can I check if there are any selected items in Java JList

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

589 Views

To check if there are any selected items, use the following:boolean res = !list.isSelectionEmpty();The value of res would be TRUE IF we have a selected item in the JList.The following is an example to check if there are any selected items in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= {"Squash", ... Read More

How to filter non-null value in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

2K+ Views

Let’s say the following is our List with string elements:List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");Now, create a stream and filter elements that end with a specific letter:Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));Now, use Objects::nonnull for non-null values:List list = stream.filter(Objects::nonNull).collect(Collectors.toList());The following is an example to filter non-null value in Java:Exampleimport java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");       Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));       List list = ... Read More

How to filter empty string values from a Java List?

Nancy Den
Updated on 30-Jul-2019 22:30:26

757 Views

Let’s say we have a String List with an empty value. Here, we have empty array elements before Football and after Squash:List sports = Arrays.asList("", "Football", "Cricket", "Tennis", "Squash", "", "Fencing", "Rugby");Now filter the empty string values. At first, we have used Predicate to negate values:Stream stream = sports.stream(); Predicate empty = String::isEmpty; Predicate emptyRev = empty.negate(); stream.filter(emptyRev).collect(Collectors.toList()));The following is an example to filter empty string values from a List:Exampleimport java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List sports = Arrays.asList("", "Football", "Cricket", "Tennis", ... Read More

How to count element after filtering in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

358 Views

Let’s say the following is the String List:List list = new ArrayList(); list.add("Tom"); list.add("John"); list.add("David"); list.add("Paul"); list.add("Gayle"); list.add("Narine"); list.add("Joseph");Now, let’s say you need to filter elements beginning with a specific letter. For that, use filter() and startsWith():long res = list    .stream()    .filter((s) -> s.startsWith("J"))    .count();We have also counted the elements above after filtering using count().The following is an example to count element after filtering in Java:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = new ArrayList();       list.add("Tom");       list.add("John"); ... Read More

How to convert Stream to TreeSet in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

765 Views

Let us first create a Stream:Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");Now convert Stream to TreeSet:Set set = stream.collect(Collectors.toCollection(TreeSet::new));The following is an example to convert String to TreeSet in Java:Exampleimport java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");       Set set = stream.collect(Collectors.toCollection(TreeSet::new));       set.forEach(val -> System.out.println(val));    } }OutputArmenia Australia Canada India Poland UK USRead More

How to convert string Stream to join them in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

414 Views

Let us create string Stream:Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");Convert the above string stream and join them with Collectors:final String str = stream.collect(Collectors.joining(" "));The following is an example to convert string Stream to join them:Exampleimport java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");       final String str = stream.collect(Collectors.joining(" "));       System.out.println("Join result..."+str);    } }OutputJoin result... Bing Bang Theory Vampire Diaries Game of Thrones HomecomingRead More

Java Program to convert Stream to typed array

Nancy Den
Updated on 30-Jul-2019 22:30:26

209 Views

Let us first create a Stream:Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");Now convert the above stream to typed array:final String[] strArr = stream.toArray(String[]::new);The following is an example to convert Stream to typed array in Java:Exampleimport java.util.Arrays; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");       final String[] strArr = stream.toArray(String[]::new);       System.out.println("Array...");       Arrays.asList(strArr).forEach(n-> System.out.println(n));    } }OutputArray... Bing Bang Theory Vampire Diaries Game of Thrones HomecomingRead More

How to right align the text in a ComboBox in Java

Nancy Den
Updated on 30-Jul-2019 22:30:26

300 Views

To right align the text in a JComboBox, use the following:ComponentOrientation.RIGHT_TO_LEFTThe following is an example to right align the text in a ComboBox:Exampleimport java.awt.Component; import java.awt.ComponentOrientation; import javax.swing.DefaultListCellRenderer; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; public class SwingDemo extends JFrame {    public SwingDemo() {       JComboBox combo = new JComboBox();       combo.setRenderer(new MyListCellRenderer());       combo.addItem("One");       combo.addItem("Two");       combo.addItem("Three");       combo.addItem("Four");       combo.addItem("Five");       getContentPane().add(combo, "North");       setSize(600, 400);       setDefaultCloseOperation(EXIT_ON_CLOSE);    }    public static void main(String[] args) { ... Read More

Advertisements