Krantik Chavan has Published 308 Articles

Java Program to sort String Stream with reversed Comparator

Krantik Chavan

Krantik Chavan

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

268 Views

Let us first create a String List:List list = Arrays.asList("Tom", "Jack", "Ryan", "Kevin", "Loki", "Thor");Now compare each element for reverse:Comparator comp = (aName, bName) -> aName.compareTo(bName);Now, perform sort:list.stream().sorted(comp.reversed())The following is an example to sort String Stream with reversed Comparator:Exampleimport java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo {    public ... Read More

How to change display mode with Java Swings

Krantik Chavan

Krantik Chavan

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

962 Views

To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as:new DisplayMode(800, 600, 32, 60));Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.The following is an example ... Read More

Java Program to create Arrow Button positioning North

Krantik Chavan

Krantik Chavan

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

187 Views

To create Arrow Button at position north, use BasicArrowButton:BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);Above, we have set the arrow to NORTH. Now add it to Panel:panel.add(arrow, BorderLayout.NORTH);The following is an example to create Arrow Button positioning North:Exampleimport java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo extends JPanel {   ... Read More

How do you get the font metrics in Java Swing?

Krantik Chavan

Krantik Chavan

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

388 Views

To get the font metrics, use the FontMetrics class:Graphics2D graphics = (Graphics2D) gp.create(); String str = getWidth() + "(Width) x (Height)" + getHeight(); FontMetrics m = graphics.getFontMetrics();Now to display it:int xValue = (getWidth() - m.stringWidth(str)) / 2; int yValue = ((getHeight() - m.getHeight()) / 2) + m.getAscent(); graphics.drawString(str, xValue, yValue);The ... Read More

Make a custom cursor appear when the user moves the mouse over some text in a Java Swing JDialog

Krantik Chavan

Krantik Chavan

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

517 Views

At first set the label on which you want the custom cursor to be visible on hover:JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");Now, set cursor to be visible as Hand Cursor instead of the default Cursor:label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));The following is an example to make a custom cursor ... Read More

How to set fullscreen mode for Java Swing Application?

Krantik Chavan

Krantik Chavan

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

4K+ Views

To set fullscreen mode for your Java Swing application, use the setFullScreenWindow() method:GraphicsDevice device = graphics.getDefaultScreenDevice(); JFrame frame = new JFrame("Fullscreen"); device.setFullScreenWindow(frame);The following is an example to set fullscreen mode for Java Swing Application:Exampleimport java.awt.Color; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo {   ... Read More

Java Program to add Titled Border to Panel in Swing

Krantik Chavan

Krantik Chavan

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

173 Views

To set Titled Border to Panel, let us first create a Panel for our Java Swing Application:JPanel panel = new Jpanel(new BorderLayout());Now, set the titled border:panel.setBorder(new TitledBorder("Displaying Titled Border"));The following is an example to add Titled Border to Panel in Swing:Exampleimport java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; ... Read More

Java Program to create rounded borders in Swing

Krantik Chavan

Krantik Chavan

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

4K+ Views

Let us first create a Frame:JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);Now, create rounded borders:double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));The following is an example to create rounded borders in Swing:Exampleimport java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel ... Read More

Java Program to convert int array to IntStream

Krantik Chavan

Krantik Chavan

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

2K+ Views

To convert int array to IntStream, let us first create an int array:int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};Now, create IntStream and convert the above array to IntStream:IntStream stream = Arrays.stream(arr);Now limit some elements and find the sum of those elements in the stream:IntStream ... Read More

How to create IntSummaryStatistics from Collectors in Java?

Krantik Chavan

Krantik Chavan

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

204 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 ... Read More

Advertisements