Found 272 Articles for Java8

Create shaped windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 11:58:25

623 Views

With JDK 7, we can create a shaped window using swing very easily. Following are the steps needed to make a shaped window. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } });ExampleSee the example below of a shaped window.import java.awt.Color; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.RoundRectangle2D; import ... Read More

Create a simple calculator using Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:01:39

17K+ Views

Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. It is built on top of AWT API and acts as a replacement of AWT API since it has almost every control corresponding to AWT controls.Following example showcases a simple calculator application.import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator implements ActionListener {    private static JTextField inputBox;    Calculator(){}    public static void main(String[] args) {       createWindow();    } ... Read More

Sleeping for a while in Java

Samual Sam
Updated on 19-Jun-2020 12:04:25

603 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Create Toast Message in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:08:08

840 Views

A toast message is an alert which disappears with time automatically. With JDK 7, we can create a toast message similar to an alert on android very easily. Following are the steps needed to make a toast message.Make a rounded rectangle shaped frame. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } ... Read More

Create translucent windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 12:10:44

223 Views

With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester {    public static void main(String[] args)  {                     JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread          SwingUtilities.invokeLater(new Runnable() {          @Override          public void ... Read More

Are static local variables allowed in Java?

karthikeya Boyini
Updated on 18-Jun-2020 15:41:40

804 Views

Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {       static int a = 10;    } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression                    static int a = 10;

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

684 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Assigning long values carefully in java to avoid overflow

karthikeya Boyini
Updated on 18-Jun-2020 15:24:22

182 Views

In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.ExampleLive Demopublic class Tester { ... Read More

Copying file using FileStreams in Java

Samual Sam
Updated on 18-Jun-2020 15:26:45

224 Views

This example shows how to copy the contents of one file into another file using read & write methods of FileStreams classes.ExampleLive Demoimport java.io.*; public class Main {    public static void main(String[] args) throws Exception {       BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));       out1.write("string to be copied");       out1.close();       InputStream in = new FileInputStream(new File("srcfile"));       OutputStream out = new FileOutputStream(new File("destnfile"));       byte[] buf = new byte[1024];       int len;       while ((len = in.read(buf)) > 0) { ... Read More

Conversion of Stream To Set in Java

karthikeya Boyini
Updated on 18-Jun-2020 15:28:33

2K+ Views

We can convert a stream to set using the following ways.Using stream.collect() with Collectors.toSet() method - Stream collect() method iterates its elements and stores them in a collection.collect(Collector.toSet()) method.Using set.add() method - Iterate stream using forEach and then add each element to the set.ExampleLive Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors;   public class Tester {            public static void main(String[] args) {       Stream stream = Stream.of("a", "b", "c", "d");       // Method 1       Set set = stream.collect(Collectors.toSet());       set.forEach(data -> System.out.print(data + " ")); ... Read More

Previous 1 ... 4 5 6 7 8 ... 28 Next
Advertisements