Found 4336 Articles for Java 8

How to set fullscreen mode for Java Swing Application?

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 {    public static void main(String[] args) {       GraphicsEnvironment graphics =       GraphicsEnvironment.getLocalGraphicsEnvironment();       GraphicsDevice device = graphics.getDefaultScreenDevice();       JFrame frame = new JFrame("Fullscreen");       JPanel panel = new JPanel();       JLabel label = new JLabel("", JLabel.CENTER);     ... Read More

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

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

520 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 appear when the user moves the mouse over some text:Exampleimport java.awt.Cursor; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class SwingDemo extends JFrame {    private void ShowDialog() {       JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");       label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));   ... Read More

How do you get the font metrics in Java Swing?

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

389 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 following is an example to get the font metrics in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Font Metrics");       ... Read More

Java Program to create Arrow Button positioning North

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 {    public SwingDemo() {       setLayout(new BorderLayout());       JPanel panel = new JPanel(new BorderLayout());       add(panel, BorderLayout.EAST);       BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);       panel.add(arrow, BorderLayout.NORTH);    }    public static void main(String[] args) {       JFrame frame = ... Read More

How to change display mode with Java Swings

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

971 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 to change display mode with Java Swings:Exampleimport java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(800, 600);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       GraphicsDevice graphics = GraphicsEnvironment.getLocalGraphicsEnvironment()   ... Read More

Java Program to sort String Stream with reversed Comparator

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

271 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 static void main(String[] args) {       List list = Arrays.asList("Tom", "Jack", "Ryan", "Kevin", "Loki", "Thor");       System.out.println("Initial List = "+list);       System.out.println("Reverse...");       Comparator comp = (aName, bName) -> aName.compareTo(bName);       list.stream().sorted(comp.reversed()) .forEach(System.out::println);    } }OutputInitial List = [Tom, Jack, ... Read More

Sort String Array alphabetically by the initial character only in Java

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 the first character:Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));The following is an example to sort String Array alphabetically by the initial character only:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" }; ... Read More

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

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

607 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() - str2.length());The following is an example to sort array of strings by their lengths with shortest to longest pattern: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 sort Integer list in reversed order

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

146 Views

Following is our integer array:Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500};Now convert the above Integer array to List:List list = new ArrayList(Arrays.asList(arr));Now, to sort the above Integer list in reversed order:Comparator initialComp = Integer::compare; Comparator revComp = initialComp.reversed(); Collections.sort(list, revComp);The following is an example to sort Integer list in reversed order:Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo {    public static void main(String[] args) {       Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500};       List list = new ... Read More

Java Program to sort a list placing nulls in the end

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

335 Views

Let us create a list first with string elements. Some of the elements are null in the List:List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");Now, sort the above list and place nulls in the end with nullsLast:list.sort(Comparator.nullsLast(String::compareTo));The following is an example to sort a list placing nulls in the end:Exampleimport java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo {    public static void main(String... args) {       List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");       System.out.println("Initial List = "+list);       list.sort(Comparator.nullsLast(String::compareTo));       System.out.println("List placing nulls ... Read More

Advertisements