Developing a professional-looking login screen in Java Swing often requires moving beyond default components. By combining custom UI delegates, manual layout management, and refined visual properties, you can create a unique user experience. This example demonstrates how to structure a login window with custom input fields, interactive buttons, and a themed progress bar.
Core UI Setup
To create a frameless, centered window, we initialize the JFrame with setUndecorated(true) and calculate the screen coordinates manually. The components are placed using a absolute layout (null layout) for precise positioning on a custom background panel.
public class AuthWindow extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JProgressBar statusIndicator;
private final Color primaryTheme = new Color(27, 67, 103);
public AuthWindow() {
super("Login");
setSize(787, 589);
setUndecorated(true);
setLayout(null);
centerWindow();
initializeComponents();
}
private void centerWindow() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getWidth()) / 2, (screen.height - getHeight()) / 2);
}
}
Interactive Password Field
To improve usability, you can implement a toggle visibility feature for password fields. By adding a listener to a trailing icon component, you can dynamically swap the echo character.
// Inside component initialization
passwordField = new JPasswordField();
JButton toggleButton = new JButton("Show");
toggleButton.addActionListener(e -> {
if (passwordField.getEchoChar() == '*') {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*');
}
});
passwordField.putClientProperty("JTextField.trailingComponent", toggleButton);
Themed Progress Bar
You can create a dynamic progress bar by extending BasicProgressBarUI. This allows you to override the paintDeterminate method to change the color based on the current progress percentage, providing real-time visual feedback.
public class DynamicProgressBarUI extends BasicProgressBarUI {
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
int val = progressBar.getValue();
Color statusColor = (val < 50) ? Color.ORANGE : Color.GREEN;
progressBar.setForeground(statusColor);
super.paintDeterminate(g, c);
}
}
Advanced UI Customization
To achieve a polished aesthetic, the TextBorderUtlis class (extending LineBorder) utilizes Graphics2D and RenderingHints to enable anti-aliasing for rounded input fields. This ensures that the borders appear smooth on high-resolution displyas. Similarly, button transparency is managed by setting setContentAreaFilled(false) and setBorderPainted(false), allowing for custom background icons and rollover effects using a standard MouseAdapter.