Found 9150 Articles for Object Oriented Programming

Can we disable JComboBox arrow button in Java?

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

631 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

824 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

757 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

318 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

770 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

How to approach String as int stream in Java

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

130 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) {       String str = "YuBM787Nm";       int res = str.chars() .filter(Character::isDigit) .map(ch -> Character.valueOf((char) ch)).sum();       System.out.println("String as IntStream = "+res);    } }OutputString as IntStream = 166

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

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

907 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 java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.text.html.HTMLEditorKit; public class SwingDemo extends JFrame {    public static void main(String[] args) {       SwingDemo s = new SwingDemo();       s.setSize(600, 300);       Container container = s.getContentPane();       s.demo(container, container.getSize());   ... Read More

How to create IntSummaryStatistics from Collectors in Java?

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

287 Views

Let us first create a List:List emp = Arrays.asList(    new Employee("John", "Marketing", 5),    new Employee("David", "Operations", 10));We have class Employee with name, department and rank of employees.Now, create summary for the rank like count, average, sum, etc:IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));The following is an example to create IntSummaryStatistics from Collectors in Java:Exampleimport java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo {    public static void main(String[] args) throws Exception {       List emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));       IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → ... Read More

Advertisements