Open-source News

How to beautify your Java applications

opensource.com - Fri, 03/18/2022 - 15:00
How to beautify your Java applications Seth Kenlon Fri, 03/18/2022 - 03:00 Up Register or Login to like.

What's not to love about Java?

I love that Java lets me write applications on one platform and run them on other platforms. You don't have to mess around with platform-specific SDKs, using a different library for that one platform, or inserting little code hacks to make that other platform behave. To me, that's how easy all modern programming ought to be. There's great infrastructure around Java, too, like the Maven build system and SDKMan.

But there's one thing about Java I don't love: the look and feel of its default GUI toolkit, called Swing. While some people feel there's a charming nostalgia to Swing, for the modern computerist, it can look a little dated.

Fortunately, that's an easy problem to solve. Java is a programming language, so if you're writing an app in Java you can reprogram its look and feel using one of several Java theming libraries.

More on Java What is enterprise Java programming? Red Hat build of OpenJDK Java cheat sheet Free online course: Developing cloud-native applications with microservices arc… Fresh Java articles Default theme of Java Swing

The default theme of Java Swing hasn't changed much in the past decade. This is a screenshot of an example Java app from 2006—or is it 2022?

Hard to tell.

 

You can see a simpler version for yourself with this demo code:

package com.opensource.myexample2app;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import javax.swing.JRadioButton;

public class App extends JFrame {

    private void run() {
        var window = new JPanel();
        window.setLayout(new BoxLayout(window, BoxLayout.X_AXIS));
        window.setBorder(new EmptyBorder(new Insets(15, 15, 15, 15)));

        JButton btn_hello = new JButton("Hello");
        btn_hello.setSize(new Dimension(80, 20));
        window.add(btn_hello);
       
        window.add(Box.createRigidArea(new Dimension(10, 15)));

        JRadioButton rad_world = new JRadioButton("World");
        rad_world.setSize(new Dimension(80, 20));
        window.add(rad_world);

        add(window);
        pack();

        setTitle("Hello world");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            var app = new App();
            app.run();
            app.setVisible(true);
        });
    }
}

Run that in the Java IDE of your choice or with Java directly:

$ java App.java

The result is simple but demonstrative:

 

Look and Feel Java libraries

Java Swing gets its theme from Look and Feel (LAF) libraries.

The few that are bundled with Java suffice in a pinch, but newer ones are available, and you can bundle those libraries with your code to give your application a different theme. You can find LAF libraries by searching for "laf" on sites like Mvnrepository.com, or on popular coding sites like GitLab or GitHub.

My favorite is FlatLaf, but there are very good themes by NetBeans, IntelliJ, and many others under a variety of different licenses to fit any project.

The easiest way to use one of these libraries is to add it to Maven, then make minor modifications to your code to invoke the theme.

First, add the library to Maven:

<dependencies>
  <dependency>
    <groupId>com.formdev</groupId>
    <artifactId>flatlaf</artifactId>
    <version>2.0.1</version>
  </dependency>
</dependencies>

Next, import the Java UIManager library and the LAF theme you're using to your project.

import com.formdev.flatlaf.FlatLightLaf;
import javax.swing.UIManager;

In the class that creates your GUI, use UIManager to set the application's look and feel:

try {
    UIManager.setLookAndFeel( new FlatLightLaf() );
} catch( Exception ex ) {
    System.err.println( "Failed to initialize theme. Using fallback." );
}

That's it! Launch your application to see the new look and feel.

 

Flatlaf happens to have variant themes, so you can change FlatLightLaf to FlatDarkLaf for a dark theme:

 

Or use FlatIntelliJLaf for an IntelliJ-like look and feel:

 

Or FlatDarculaLaf for a dark IntelliJ look and feel:

 

Looking good, Java

Java Swing is an easy toolkit to use. It's been well maintained for two decades, and it provides a great desktop experience.

With look and feel libraries, Swing can look good, too.

Programming in Java doesn't have to be ugly. Follow these simple steps to spruce up Java Swing.

Java What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

How to Disable ‘su’ Access for Sudo Users

Tecmint - Fri, 03/18/2022 - 13:32
The post How to Disable ‘su’ Access for Sudo Users first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

The su command is a special Linux command that allows you to run a command as another user and group. It also allows you to switch to the root account (if run without any

The post How to Disable ‘su’ Access for Sudo Users first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Boston University sees success with new educational tools built with Red Hat OpenShift Data Science

Red Hat News - Fri, 03/18/2022 - 12:00

Introducing students to modern computing systems can be complicated and challenging. When Boston University faculty members Jonathan Appavoo, computer science (CS) professor, and Orran Krieger, electrical and computer engineering (ECE) professor, were looking for a new, simplified way to educate their students on the critical concepts of computing systems, they turned to Red Hat OpenShift Data Science.

Linux 5.18 To Bring Many Random Number Generator Improvements

Phoronix - Fri, 03/18/2022 - 12:00
WireGuard lead developer Jason Donenfeld has recently been spearheading many improvements to the Linux kernel's random number generator (RNG) code and building off the work found in Linux 5.17, the Linux 5.18 kernel will bring a lot more on this front...

Rust Patches For The Linux Kernel Updated A Fifth Time With New Features

Phoronix - Fri, 03/18/2022 - 02:38
Miguel Ojeda who has been leading the Rust programming language support for the Linux kernel today posted his fifth spin of this patch series providing the optional Rust integration for the Linux kernel that includes example driver code...

Linux 5.18 To Fix Thermal Policy Handling For Select Newer HP Omen Laptops

Phoronix - Fri, 03/18/2022 - 01:45
Since the end of last year with Linux 5.16 there has been support for setting the thermal/performance preference with newer HP Omen laptops having ACPI Platform Profile support. This allows for toggling between cool / balanced / performance modes. Now for Linux 5.18 the HP-WMI driver is being improved upon for handling some newer laptops that have a different thermal policy interface...

AMD Now Backing AlmaLinux As This Increasingly Popular RHEL/CentOS Alternative

Phoronix - Thu, 03/17/2022 - 21:30
AMD is now among the latest companies backing the AlmaLinux OS Foundation for that increasingly popular free build derived from the Red Hat Enterprise Linux sources now that CentOS 8 is end-of-life...

AMD FidelityFX Super Resolution 2.0 Debuts

Phoronix - Thu, 03/17/2022 - 21:16
Last year AMD announced FidelityFX Super Resolution for high performance, spatial upscaling for video games across platforms. Today ahead of GDC week AMD announced FidelityFX Super Resolution 2.0...

Pages