Krantik Chavan has Published 278 Articles

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

Krantik Chavan

Krantik Chavan

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

924 Views

At first, let us create and array of strings:String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" };Now, for shortest to longest pattern, for example A, AB, ABC, ABCD, etc.; get the length of both the string arrays and work them like this:Arrays.sort(strArr, (str1, str2) -> str1.length() ... Read More

Sort String Array alphabetically by the initial character only in Java

Krantik Chavan

Krantik Chavan

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

2K+ Views

Here, we are sorting string array alphabetically by the initial character i.e. ‘J’ for ‘John’ will come after ‘Chris’ since the first character of ‘Chris’ is ‘C’.Let us first create a String array:String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };Now, sort the string array based on ... Read More

How to change display mode with Java Swings

Krantik Chavan

Krantik Chavan

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

1K+ 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

258 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

540 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

709 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

6K+ 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

260 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

6K+ 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

How to create IntSummaryStatistics from Collectors in Java?

Krantik Chavan

Krantik Chavan

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

285 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