Java Swing Setup - potatoscript/JavaSwing GitHub Wiki
⚙️ Java Swing Setup 🖥️
🎯 What is Java Swing?
Java Swing is a GUI (Graphical User Interface) framework that lets you build desktop applications with Java. To start creating Swing applications, you need to:
✅ Install Java Development Kit (JDK)
✅ Set up an Integrated Development Environment (IDE)
✅ Create a new Java project with Swing components
📚 Step 1: Prerequisites
📝 1. Install Java Development Kit (JDK)
-
🔍 Check if JDK is already installed:
- Open Command Prompt or Terminal.
- Type:
java -version
- If JDK is installed, it will display something like:
java version "17.0.1" 2023-10-12 LTS
- If not, proceed to download and install JDK.
-
📥 Download JDK:
- Go to Oracle JDK Download Page.
- Select the latest JDK version for your operating system (Windows, macOS, or Linux).
- Follow the installation instructions to complete the setup.
-
🔗 Set Environment Variables (if necessary):
- Add the
JAVA_HOME
andPATH
variables to the system. - Windows:
- Open Control Panel → System → Advanced System Settings.
- Click Environment Variables and add the JDK path to the
PATH
variable.
- Mac/Linux:
- Add the following lines to
~/.bashrc
or~/.zshrc
:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk export PATH=$PATH:$JAVA_HOME/bin
- Add the following lines to
- Add the
🎨 2. Choose Your IDE
✅ Recommended IDEs:
- 🖥️ IntelliJ IDEA – Ideal for Java beginners and professionals.
- 🔥 Eclipse IDE – Popular and feature-rich IDE.
- 🚀 NetBeans IDE – Good for GUI projects with Swing.
🧑💻 Step 2: Setting Up IntelliJ IDEA
📥 1. Download IntelliJ IDEA
- Go to IntelliJ IDEA Download Page.
- Download the Community Edition (Free).
🏗️ 2. Install IntelliJ IDEA
- Run the downloaded installer and follow the installation steps.
- Select the following options:
- ✅ Java Support
- ✅ Build Tools – Maven/Gradle (Optional)
- ✅ Version Control – Git Support
- Finish the installation and launch IntelliJ IDEA.
🎛️ 3. Create a New Java Project
- Open IntelliJ IDEA.
- Select New Project from the welcome screen.
- Choose Java from the project options.
- Configure the project:
- Project Name:
MySwingApp
- Project Location: Choose your desired location.
- JDK: Select the installed JDK from the list.
- Project Name:
- Click Finish to create your project.
📝 4. Add Swing Support
- Right-click on
src
and select New → Java Class. - Name the class
HelloSwing
. - Add the following code to test your setup:
import javax.swing.*;
public class HelloSwing {
public static void main(String[] args) {
// Create a new JFrame
JFrame frame = new JFrame("Hello Swing!");
// Create a label with text
JLabel label = new JLabel("🎉 Hello, Swing World!", SwingConstants.CENTER);
// Add label to frame
frame.add(label);
// Set frame size and default close operation
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make the frame visible
frame.setVisible(true);
}
}
▶️ 5. Run Your Application
- Click on the Run icon ▶️ next to
HelloSwing
or pressShift + F10
. - If the setup is correct, a window will pop up with:
+----------------------------------+
| |
| 🎉 Hello, Swing World! |
| |
+----------------------------------+
🔥 Step 3: Setting Up Eclipse IDE
📥 1. Download Eclipse
- Go to Eclipse Download Page.
- Select Eclipse IDE for Java Developers.
- Download the appropriate version for your operating system.
🏗️ 2. Install Eclipse
- Run the installer and follow the installation instructions.
- Choose a workspace folder to store your projects.
- Launch Eclipse.
🎛️ 3. Create a New Java Project
- Click File → New → Java Project.
- Name your project
MySwingApp
. - Select the appropriate JDK under JRE.
- Click Finish.
📦 4. Create a New Java Class
- Right-click on
src
and choose New → Class. - Name the class
HelloSwing
. - Copy and paste the following code:
import javax.swing.*;
public class HelloSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing!");
JLabel label = new JLabel("👋 Hello, Swing World!", SwingConstants.CENTER);
frame.add(label);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
▶️ 5. Run Your Application
- Click Run → Run As → Java Application.
- Your GUI window should appear with:
+----------------------------------+
| |
| 👋 Hello, Swing World! |
| |
+----------------------------------+
📝 Step 4: Understanding Basic Swing Components
📚 Core Swing Components
Component | Description | Code Example |
---|---|---|
JFrame |
Main application window | new JFrame("Title") |
JLabel |
Displays text and images | new JLabel("Hello") |
JButton |
Button to trigger actions | new JButton("Click Me") |
JTextField |
Single-line text input | new JTextField(20) |
JTextArea |
Multi-line text input | new JTextArea(5, 20) |
JCheckBox |
Checkbox to toggle options | new JCheckBox("Accept Terms") |
JComboBox |
Dropdown list for selection | new JComboBox(options) |
🎨 Step 5: Run Swing Applications from Command Line
- Open Command Prompt/Terminal.
- Navigate to your Java project directory.
cd /path/to/your/project
- Compile your Java file:
javac HelloSwing.java
- Run the application:
java HelloSwing
🎁 Bonus: Setting Look and Feel
To make your application look like the native OS, add the following code before creating Swing components:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}