1. Home
  2. /
  3. Cheerpj
  4. /
  5. Java Swing Label Update

Java Swing Label Update - Cheerpj Typing CST Test

Loading…

Java Swing Label Update — Cheerpj Code

Updates a JLabel when a button is clicked.

# cheerpj/demo/LabelUpdate.java
import javax.swing.*;
import java.awt.event.*;

public class LabelUpdate {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Label Update");
		JLabel label = new JLabel("Initial Text");
		JButton button = new JButton("Update");
		button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		label.setText("Updated Text");
		}
		});
		frame.setLayout(new java.awt.FlowLayout());
		frame.add(label);
		frame.add(button);
		frame.setSize(300,100);
		frame.setVisible(true);
	}
}

Cheerpj Language Guide

CheerpJ is a WebAssembly-based Java-to-JavaScript/WebAssembly compiler and runtime that allows running unmodified Java applications directly inside the browser without plugins. It converts Java bytecode into WebAssembly + JavaScript, enabling full client-side execution of existing JVM applications, applets, and libraries.

Primary Use Cases

  • ▸Running legacy Java Swing/AWT desktop apps in the browser
  • ▸Migrating Java Applets to modern WebAssembly environments
  • ▸Enterprise internal tools requiring Java libraries
  • ▸Embedding Java libraries inside JS/Wasm web apps
  • ▸Preserving old JVM-based simulations, tools, and educational software

Notable Features

  • ▸Convert Java bytecode directly into Wasm + JavaScript
  • ▸Full Java runtime emulation in browser
  • ▸Supports Swing and AWT UI rendering
  • ▸Works without plugins or local JVM
  • ▸Can run unmodified .jar files client-side

Origin & Creator

CheerpJ is developed by Leaning Technologies, known for compiling traditional languages to modern web targets like WebAssembly.

Industrial Note

CheerpJ is heavily used by enterprises wanting to preserve legacy Java Swing/AWT applications or Java applets and make them run in modern browsers without rewriting them.

Quick Explain

  • ▸CheerpJ lets you run Java applications in the browser without needing JVM installation.
  • ▸It converts Java bytecode (.class, .jar) into a mix of JS and Wasm.
  • ▸Provides a browser-based JVM emulation to run complex Java apps.
  • ▸Supports Java AWT/Swing applications via HTML5 canvas rendering.
  • ▸Allows enterprise legacy Java applications to be ported to the web with minimal changes.

Core Features

  • ▸Ahead-of-time Java-to-Wasm/Javascript compiler
  • ▸CheerpJ Runtime Environment (CJRE)
  • ▸Browser-based JVM with classloader/Web APIs
  • ▸Swing/AWT rendering stack mapped to HTML5
  • ▸Secure sandbox execution

Learning Path

  • ▸Refresh core Java fundamentals
  • ▸Understand CheerpJ runtime model
  • ▸Load simple JAR in browser
  • ▸Port Swing/AWT app
  • ▸Optimize loading and rendering

Practical Examples

  • ▸Running legacy educational Java applets
  • ▸Java-based CAD tools inside browser
  • ▸Internal enterprise Swing dashboards
  • ▸Java crypto or math libraries in JS projects
  • ▸Browser-based code editors or simulators

Comparisons

  • ▸CheerpJ vs TeaVM: CheerpJ runs *unmodified* bytecode; TeaVM requires source-level transformation
  • ▸CheerpJ vs GWT: CheerpJ is JVM-compatible; GWT is Java->JS transpiler
  • ▸CheerpJ vs Go-WASM: CheerpJ targets legacy Java preservation; Go-WASM targets new apps
  • ▸CheerpJ vs WebAssembly Java Runtimes: CheerpJ has strongest AWT/Swing support
  • ▸CheerpJ vs Emscripten: Different languages; Emscripten better for C/C++ performance

Strengths

  • ▸Run unmodified Java applications in browser
  • ▸No need to rewrite Java -> JS manually
  • ▸Great for preserving legacy enterprise tools
  • ▸Stable runtime with good browser compatibility
  • ▸Strong support for complex UI frameworks

Limitations

  • ▸Performance lower than native JVM
  • ▸Binary payload size can be large
  • ▸Not ideal for highly interactive or real-time apps
  • ▸Limited debugging compared to JVM tools
  • ▸Not suitable for low-latency games

When NOT to Use

  • ▸High-performance gaming
  • ▸Real-time graphics rendering
  • ▸Apps requiring JVM-level performance tuning
  • ▸Projects better rewritten in JS/TS
  • ▸Tiny footprint browser widgets

Cheat Sheet

  • ▸<script src='cheerpj.js'></script>
  • ▸cheerpjInit();
  • ▸cheerpjRunMain('com.example.Main');
  • ▸Place JAR files next to HTML
  • ▸Use canvas for Swing rendering

FAQ

  • ▸Can CheerpJ run any Java app?
  • ▸Most - especially Swing, AWT, and console apps.
  • ▸Does it require rewriting Java?
  • ▸No - it runs unmodified Java bytecode.
  • ▸Is performance like a JVM?
  • ▸No - slower due to browser constraints.
  • ▸Does it support JavaFX?
  • ▸Limited - Swing/AWT best supported.
  • ▸Can it access local files?
  • ▸Only via virtual filesystem.

30-Day Skill Plan

  • ▸Week 1: Java basics & packaging
  • ▸Week 2: CheerpJ runtime usage
  • ▸Week 3: Browser integration techniques
  • ▸Week 4: UI/Canvas optimization
  • ▸Week 5: Enterprise deployment

Final Summary

  • ▸CheerpJ allows unmodified Java applications to run inside modern browsers.
  • ▸It converts Java bytecode into WebAssembly and JS.
  • ▸Strong support for Swing/AWT and enterprise legacy apps.
  • ▸Ideal for preserving old Java tools and migrating applets.
  • ▸A practical solution for browser-based JVM compatibility.

Project Structure

  • ▸index.html - main launcher
  • ▸cheerpj.js - runtime loader
  • ▸cheerpj-*-wasm.js - Wasm modules
  • ▸app.jar - your Java program
  • ▸assets/ - Additional Java resources

Monetization

  • ▸Legacy app migration services
  • ▸SaaS delivery of Java desktop apps
  • ▸Enterprise web tools
  • ▸Education software preservation
  • ▸Commercial internal dashboards

Productivity Tips

  • ▸Keep JAR dependencies light
  • ▸Use AOT compilation for speed
  • ▸Profile startup and reduce class loading
  • ▸Use browser caching for Wasm runtime
  • ▸Test frequently in multiple browsers

Basic Concepts

  • ▸Bytecode Translation - convert Java to WebAssembly/JS
  • ▸CJRE - browser-hosted Java Runtime Environment
  • ▸Swing/AWT Emulation - map Java UI to HTML5
  • ▸ClassLoader - dynamic class loading via web
  • ▸Filesystem Emulation - Java file APIs mapped to virtual FS

Official Docs

  • ▸https://cheerpj.com/docs
  • ▸https://leaningtech.com/cheerpj

More Cheerpj Typing Exercises

Simple CheerpJ Java ProgramCheerpJ Java Button ClickCheerpJ Java Input DialogCheerpJ Java Conditional OutputCheerpJ Java Loop ExampleCheerpJ Java Timer ExampleCheerpJ Java Dialog ExampleCheerpJ Java Array LoopCheerpJ Java Nested Loops

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher