Jump to content

  • Log In with Google      Sign In   
  • Create Account






Java 8 very interesting

Posted by blewisjr, 16 July 2014 · 292 views

This is a rather short blog post. I have had some ideas for a project recently with some of the various endeavors I have been contemplating.

One of these endeavors is either a desktop application or web application not sure which but I think it makes more sense as a desktop application due to it's purpose.

When I was thinking about the project I new I would want it cross platform so my real choices would be either Java or C++. I never made a GUI application in C++ before so I said let me modernize my java install and upgrade to IntelliJ Idea 13.1. Oh by the way IntelliJ idea is worth every penny. If you develop in Java you should really spend the $200 and pick up a personal license which can be used for commercial applications. Really great IDE and I can't wait to see what they do with their C++ ide they are working on. Jetbrains makes amazing tools.

So I upgraded everything to Java 8 and decided to make a quick and simple GUI application and use Java 8 features. I will say one thing Java should have added Lambda's a long time ago... With this in mind the following Swing code turns from this...

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TestGui extends JFrame {
    private JButton btnHello = new JButton("Hello");

    public TestGui() {
        super("Test GUI");

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(btnHello);

        btnHello.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Hello World");
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 100);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestGui().setVisible(true);
            }
        });
    }
}

to this...

import javax.swing.*;
import java.awt.*;

public class TestGui extends JFrame {
    private JButton btnHello = new JButton("Hello");

    public TestGui() {
        super("Test GUI");

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(btnHello);

        btnHello.addActionListener(e -> System.out.println("Hello World"));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 100);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new TestGui().setVisible(true));
    }
}

So much more elegant and readable I think Oracle just really hooked me back on Java with just this one feature.




September 2014 »

S M T W T F S
 123456
78910111213
14151617181920
21 22 2324252627
282930    

Recent Comments

Recent Comments

PARTNERS