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)

  1. 🔍 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.
  2. 📥 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.
  3. 🔗 Set Environment Variables (if necessary):

    • Add the JAVA_HOME and PATH variables to the system.
    • Windows:
      • Open Control PanelSystemAdvanced 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
      

🎨 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

  1. Go to IntelliJ IDEA Download Page.
  2. Download the Community Edition (Free).

🏗️ 2. Install IntelliJ IDEA

  1. Run the downloaded installer and follow the installation steps.
  2. Select the following options:
    • Java Support
    • Build Tools – Maven/Gradle (Optional)
    • Version Control – Git Support
  3. Finish the installation and launch IntelliJ IDEA.

🎛️ 3. Create a New Java Project

  1. Open IntelliJ IDEA.
  2. Select New Project from the welcome screen.
  3. Choose Java from the project options.
  4. Configure the project:
    • Project Name: MySwingApp
    • Project Location: Choose your desired location.
    • JDK: Select the installed JDK from the list.
  5. Click Finish to create your project.

📝 4. Add Swing Support

  1. Right-click on src and select NewJava Class.
  2. Name the class HelloSwing.
  3. 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

  1. Click on the Run icon ▶️ next to HelloSwing or press Shift + F10.
  2. If the setup is correct, a window will pop up with:
+----------------------------------+
|                                  |
|      🎉 Hello, Swing World!      |
|                                  |
+----------------------------------+

🔥 Step 3: Setting Up Eclipse IDE


📥 1. Download Eclipse

  1. Go to Eclipse Download Page.
  2. Select Eclipse IDE for Java Developers.
  3. Download the appropriate version for your operating system.

🏗️ 2. Install Eclipse

  1. Run the installer and follow the installation instructions.
  2. Choose a workspace folder to store your projects.
  3. Launch Eclipse.

🎛️ 3. Create a New Java Project

  1. Click FileNewJava Project.
  2. Name your project MySwingApp.
  3. Select the appropriate JDK under JRE.
  4. Click Finish.

📦 4. Create a New Java Class

  1. Right-click on src and choose NewClass.
  2. Name the class HelloSwing.
  3. 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

  1. Click RunRun AsJava Application.
  2. 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

  1. Open Command Prompt/Terminal.
  2. Navigate to your Java project directory.
cd /path/to/your/project
  1. Compile your Java file:
javac HelloSwing.java
  1. 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();
}