Found 7442 Articles for Java

What happens when JDialog is set with Modality type MODELESS in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

131 Views

Modeless dialog boxes are on the screen and are available for use. The following is an example to set JDialog with Modality type MODELESS:Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(new Dimension(600, 400));       JDialog dialog = new JDialog(frame, "New", ModalityType.MODELESS);       dialog.setSize(300, 300);       frame.add(new JButton(new AbstractAction("Click to generate") {          @Override          public void actionPerformed(ActionEvent ... Read More

Java Program to convert Stream to List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

234 Views

Declare and initialize an Integer array:Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};Now, create a stream with the above elements:Stream stream = Arrays.stream(arr);To convert the above stream to list, use Collectors.toList():stream.collect(Collectors.toList()The following is an example to convert Stream to List:Exampleimport java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};       Stream stream = Arrays.stream(arr);       System.out.println("Stream = "+stream.collect(Collectors.toList()));    } }OutputStream = [50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000]

Java Program to retrieve a Stream from a List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

165 Views

Let us first create a List:List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);Now, create a stream from the List:Stream stream = list.stream(); Arrays.toString(stream.toArray()));The following is an example to retrieve a Stream from a ListExampleimport java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);       System.out.println("List elements...");       for (int res : list)       {          System.out.println(res);       }       Stream stream = list.stream();       System.out.println("Stream = "+Arrays.toString(stream.toArray()));    } }OutputList elements... 25 50 100 200 250 300 400 500 Stream = [25, 50, 100, 200, 250, 300, 400, 500]

Can we disable JComboBox arrow button in Java?

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

634 Views

Yes, we can do that using removeArrow() method.The following is an example to disable JComboBox arrow button:Exampleimport java.awt.Component; import java.awt.Container; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       String[] strValues = {"One", "Two"};       JComboBox comboBox = new JComboBox(strValues);       removeArrow(comboBox);       JOptionPane.showMessageDialog(null, comboBox);    }    private static void removeArrow(Container container) {       Component[] c = container.getComponents();       for (Component res : c) {          if (res instanceof AbstractButton) {             container.remove(res);          }       }    } }Output

How to handle action event for JComboBox in Java?

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

827 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);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             ... Read More

How to add items in a JComboBox on runtime in Java

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();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {         ... Read More

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

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 letter, else FALSE.The following is an example to check if any String in the list starts with a letter:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List myList = new ArrayList();       myList.add("pqr");       myList.add("stu"); ... Read More

Java program to get the reverse of an Integer array with Lambda expressions

Krantik Chavan
Updated on 29-Oct-2024 18:51:17

758 Views

In this article, we will learn to get the reverse of an Integer array with Lambda expressions in Java. By utilizing the Arrays.sort() method along with a custom comparator defined as a lambda expression, we can efficiently reorder the elements of the array in descending order. Lambda expressions are a concise way to represent functional interfaces, allowing you to write cleaner and more readable code. Problem Statement Write a program in Java to get the reverse of an Integer array with Lambda expressions − Input arr = {20, 50, 75, 100, 120, 150, 170, 200} Output Integer Array elements... 20 50 ... Read More

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

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

321 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() - str1.length());The following is an example to sort array of strings by their lengths with longest to shortest pattern in Java:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" };     ... Read More

Java program to display a webpage in JEditorPane

Krantik Chavan
Updated on 13-Nov-2024 12:20:34

778 Views

In this article, we will learn how to display a webpage in JEditorPane using Java. The program will load a specified webpage, and display it within a GUI window. If the connection to the webpage fails, a message will appear indicating the connection issue. This setup can be useful for embedding simple web content within Java applications. Steps to display a webpage in JEditorPaneFollowing are the steps to display a webpage in JEditorPane −We will import the necessary classes from the java.io package and javax.swing package.Create a JEditorPane object to serve as the component that will display the webpage.Use the setPage() ... Read More

Advertisements