Write, Run & Share Java Swing code online using OneCompiler's Java Swing online compiler for free. It's one of the robust, feature-rich online compilers for Swing, running on Java 21. The window your code creates is streamed live into the browser — no JDK install, no display server, no project scaffolding. The editor shows sample boilerplate code when you choose language as Java Swing and start coding.
Swing is Java's classic desktop GUI toolkit, introduced in JDK 1.2 in 1998. Unlike its predecessor AWT, Swing widgets are "lightweight" — they're drawn by Java itself rather than handed off to the OS, which means a Swing app looks identical on Windows, macOS, and Linux. It's the toolkit behind IntelliJ IDEA, NetBeans, JDownloader, and a generation of enterprise desktop software. Two decades on, Swing is still the easiest path from public static void main to a working window with buttons and forms.
Every Swing window starts with a JFrame. Set a size, a default close operation, and call setVisible(true). setLocationRelativeTo(null) centres the window on the screen.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("My App");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello, Swing!", SwingConstants.CENTER);
label.setFont(new Font("SansSerif", Font.BOLD, 20));
frame.add(label);
frame.setVisible(true);
}
}
Swing's component library covers everything you'd expect from a desktop toolkit — buttons, text fields, checkboxes, combo boxes, lists, tables, trees, sliders, progress bars.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Components");
frame.setSize(360, 320);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(new JLabel("Name:"));
frame.add(new JTextField(15));
frame.add(new JButton("Submit"));
frame.add(new JCheckBox("Remember me"));
JComboBox<String> combo = new JComboBox<>(new String[]{"Python", "Java", "Go"});
frame.add(combo);
frame.add(new JSlider(0, 100, 50));
frame.add(new JProgressBar(0, 100) {{ setValue(70); setStringPainted(true); }});
frame.setVisible(true);
}
}
Buttons and other components fire events that you listen for with an ActionListener (or, in modern Java, a lambda).
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Counter");
frame.setSize(260, 160);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel count = new JLabel("0");
count.setFont(new Font("SansSerif", Font.BOLD, 32));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
int[] value = {0};
plus.addActionListener(e -> count.setText(String.valueOf(++value[0])));
minus.addActionListener(e -> count.setText(String.valueOf(--value[0])));
frame.add(minus);
frame.add(count);
frame.add(plus);
frame.setVisible(true);
}
}
A JFrame's default layout is BorderLayout (north / south / east / west / center). For anything more structured, use BoxLayout, GridLayout, FlowLayout, or GridBagLayout.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Layouts");
frame.setSize(360, 240);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// BorderLayout — five regions
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("South"), BorderLayout.SOUTH);
frame.add(new JButton("East"), BorderLayout.EAST);
frame.add(new JButton("West"), BorderLayout.WEST);
// Centre uses a GridLayout (2 rows x 2 cols)
JPanel grid = new JPanel(new GridLayout(2, 2, 4, 4));
grid.add(new JButton("1"));
grid.add(new JButton("2"));
grid.add(new JButton("3"));
grid.add(new JButton("4"));
frame.add(grid, BorderLayout.CENTER);
frame.setVisible(true);
}
}
Combine a few text fields and a button into a working "submit and react" form.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Login");
frame.setSize(320, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel form = new JPanel(new GridLayout(3, 2, 6, 6));
form.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JTextField email = new JTextField();
JPasswordField password = new JPasswordField();
form.add(new JLabel("Email:")); form.add(email);
form.add(new JLabel("Password:")); form.add(password);
JButton submit = new JButton("Sign in");
submit.addActionListener(e -> JOptionPane.showMessageDialog(
frame, "Welcome, " + email.getText() + "!"));
form.add(new JLabel()); form.add(submit);
frame.add(form);
frame.setVisible(true);
}
}
JOptionPane is the one-liner for message boxes, confirmations, and simple input prompts.
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Saved!");
int choice = JOptionPane.showConfirmDialog(
null, "Delete this file?", "Confirm", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) System.out.println("Deleting…");
String name = JOptionPane.showInputDialog("What's your name?");
System.out.println("Hello, " + name);
}
}
Override paintComponent(Graphics g) on a JPanel to draw your own shapes, images, or animations. Cast to Graphics2D for anti-aliasing and richer paint operations.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Canvas");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(50, 100, 220));
g2.fillRect(40, 40, 120, 80);
g2.setColor(new Color(220, 50, 50));
g2.fillOval(200, 40, 100, 100);
g2.setColor(Color.BLACK);
g2.setFont(new Font("SansSerif", Font.BOLD, 18));
g2.drawString("Hello, Graphics2D!", 40, 200);
}
});
frame.setVisible(true);
}
}
Swing is not thread-safe — every UI update should happen on the Event Dispatch Thread (EDT). For real apps, wrap your main body in SwingUtilities.invokeLater(...) so the UI is built on the EDT from the start, and use SwingWorker to run long tasks off the EDT without freezing the window.
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("EDT");
frame.setSize(280, 140);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Built on the EDT", SwingConstants.CENTER));
frame.setVisible(true);
});
}
}